开发者

String output in MATLAB

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))}; 开发者_StackOverflow% 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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜