필터 지우기
필터 지우기

Random function from a text file?

조회 수: 4 (최근 30일)
Chris
Chris 2011년 12월 3일
Basically, I have a text file with a bunch of words on it. I'm not sure how I can use MATLAB to randomly select one word.

채택된 답변

Jan
Jan 2011년 12월 3일
  1. Read the file word by word.
  2. Use rand to determine which word to choose.
fid = fopen('FileName.txt', 'r');
if fid < 0, error('Cannot open file.'); end
CC = textscan(fid, '%s');
C = CC{1};
fclose(fid);
index = ceil(rand * numel(C));
chosen = C{index};
  댓글 수: 1
Chris
Chris 2011년 12월 3일
Nevermind! Thank you.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

the cyclist
the cyclist 2011년 12월 3일
There are many MATLAB functions that can read text from files. One possibility that might work for you is the textscan() function. That will read the file into a cell array. Then, you could randomly select one element of the cell array, perhaps using the randi() command.
>> doc textscan
>> doc randi
Those help files give the detailed syntax, and also point to other functions you might prefer to use.
  댓글 수: 1
Chris
Chris 2011년 12월 3일
Thank you still!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Text Files에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by