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

In summary, the speaker is asking for help with a code they wrote that randomly selects a sentence from a file and saves it. They are having trouble saving all the picked sentences within the loop and are seeking clarification on how to do so. They also mention an error they encountered when trying to save the sentences.
  • #1
tinglin
1
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
  • #2
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.
 

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

1. How do I randomly select a file in Matlab?

In order to randomly select a file in Matlab, you can use the dir function to get a list of all files in a particular directory. Then, you can use the randperm function to generate a random index and use it to access a file from the list.

2. Can I specify a certain type of file to be randomly selected?

Yes, you can use the dir function with a wildcard (*) to specify a certain file type. For example, dir('*.txt') will only return files with the .txt extension.

3. How do I select a random sentence from a file?

You can use the randi function to generate a random number and use it as an index to access a sentence from the file. Alternatively, you can use the textscan function to read the file line by line and then use randi to select a line.

4. Can I ensure that the same file and sentence are not selected twice?

Yes, you can use the randperm function to generate a random permutation of the file list and then use it as an index to access the files and sentences in a random order without repetition.

5. Is there a way to limit the number of files and sentences to choose from?

Yes, you can use the dir function to specify a range of files to choose from, and then use randi to limit the number of sentences to choose from within each file. For example, randi([1,10]) will return a random number between 1 and 10, limiting the sentences to choose from to 10 in each file.

Back
Top