Matlab Help -- code to randomly select a file and a sentence in that file

  • Context: MATLAB 
  • Thread starter Thread starter tinglin
  • Start date Start date
  • Tags Tags
    Code File Matlab
Click For Summary
SUMMARY

The discussion centers on a MATLAB code snippet designed to randomly select a file and a sentence from that file. The user successfully generates a random file identifier and reads sentences from either 'fire.txt' or 'flood.txt'. However, the user encounters issues when attempting to store multiple randomly selected sentences within a loop, specifically receiving errors when trying to assign values to an array. The solution requires modifying the code to correctly accumulate the selected sentences during each iteration of the loop.

PREREQUISITES
  • Familiarity with MATLAB programming syntax and functions
  • Understanding of file handling in MATLAB, specifically fopen and fclose
  • Knowledge of random number generation in MATLAB using rand and randi
  • Experience with cell arrays and indexing in MATLAB
NEXT STEPS
  • Review MATLAB documentation on cell arrays and how to store multiple values
  • Learn about MATLAB's textscan function for reading text files
  • Investigate proper loop structures in MATLAB for accumulating results
  • Explore error handling in MATLAB to troubleshoot common coding issues
USEFUL FOR

MATLAB programmers, data analysts, and anyone interested in file manipulation and random selection techniques in MATLAB.

tinglin
Messages
1
Reaction score
0
clc
Matlab:
for ii=1:1:3  % start looping
  
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('fire.txt','r');% Open and read a file.
elseif (rand_id==2)
f_id_1=fopen('flood.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.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 can't 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 retruns 1, 2, 3 characters in the three loops.

Please help.
 
Last edited by a moderator:
Physics news on Phys.org
I am not content with this statement of yours:
tinglin said:
Code:
events_1=events_1{1}; % saving the text.
What are you trying to save and how? events_1 will have the text even after you are closing the file.
tinglin said:
I can't save all the picked sentences inside the loop.
Your loop is at the beginning of the code where you're not doing any read/write from text file. What other loop are you talking about?

I believe that this question cannot be worked upon unless more details are provided.
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 4 ·
Replies
4
Views
7K
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
6K
  • · Replies 29 ·
Replies
29
Views
5K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
Replies
1
Views
2K
Replies
1
Views
4K