0
clc
clear all
ii=1;

S =cell(size(30,1)); % cell size.


for ii=1:1:3    
    rand_id= rand(1,1) *3; % Randomly generte a number between 1 to 3.

    if (rand_id<1)
        rand_id=1; % 0 is ommitted.
    else rand_id=floor(rand_id);
    end

% rand_id will be used to open a previously saved file randomly.

    if (rand_id==1)
        f_id_1=fopen('C1.txt','r'); % Open and read a file. 
    elseif (rand_id==2)
        f_id_1=fopen('C2.txt','r'); % Open and read a file. 
    end

% saning the file to read the text. 
    events_1=textscan(f_id_1, '%s', 'Delimiter', '\n');
    fclose(f_id_1);
    events_1=events_1{1}; % saving the text. 
    rand_event=events_1{randi(numel(events_1))}; % selects one text randomly.

    S{ii}=rand_event;
end

I wrote the above code to randomly select a file. The file contains number of sentences. My aim is to randomly pick a sentence . I did that. Now, my problem is I cant save all the picked sentences inside the loop.

When I declare S(ii)=rand_event It shows error. When I try S(ii)=rand_event(ii) It only returns 1, 2, 3 characters in the three loops.

Please help.

4
  • Please learn how to format the code part of the question. Why you removed the original question? It's hard to understand now what is it all about. If you have new information to discuss, or code changes base of answers, always add it to the question with UPDATE label. Commented Sep 5, 2010 at 6:07
  • @ yuk. I'm a newbee. I guess, I didnt understand how to update. I edited the code. I hope not do the same mistake. Thanks for poinging out. Commented Sep 5, 2010 at 12:58
  • I added the problem description back to give a context to the question (you can edit it as necessary, but don't delete it altogether) Commented Sep 6, 2010 at 3:28
  • @ Amro. Thanks so much. I'm actually trying ti edit the code here as I 'm doing in my workspace. But seems like, I'm deleting things in course. Happy that, now I could use updates. Commented Sep 6, 2010 at 3:51

1 Answer 1

2
S(ii)

is considered to be a matrix with well defined dimensions. I guess that your 'sentences' have different length. One solution might be to use a cell array.

S{ii}=rand_event

Cell arrays use curly braces.

Sign up to request clarification or add additional context in comments.

4 Comments

Zellus, Thanks. Yes my sentences have different dimensions. When I use S(ii)=rand_event(ii), it only stores character from the the sentences in accordance with ii. I tried S{ii}=rand_event. The error is "Cell contents assignment to a non-cell array object.". I trying to read a bit about cell_array. Any thoughts ?
Just tried, S =cell(size(rand_event)); S{ii}=rand_event; I'm getting the out put for ii- final iteration only. Why so ?
@user437493: I suppose the above shown code is put into a plain m-file. While executing your code variables are instantiated in the matlab 'base workspace'. Calling clc at the beginning of your file will clean your command window, but not clear your variables. Add a 'clear all' at the beginning of your m-file. I interpret the error message as S instantiated as matrix, but tried to be accessed as cell array.
@user437493: Update your code example to show your complete m-file. I suppose you reinitialize the cell inside your loop, clearing the 'sentences' for all iterations but the last.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.