How to read the text files sequentially?

조회 수: 6 (최근 30일)
Muharrem Askin
Muharrem Askin 2012년 6월 21일
댓글: Stephen23 2017년 6월 26일
I have 15 text files such as CASE1,CASE2.... CASE15. I would like to read all these files sequentially and use for some calculations inside nested loops. I tried this code
for k=1:15
textFilename=['CASE' num2str(k) '.txt'];
id=fopen('C:\Documents and Settings\guestaskin\Desktop\model\textFilename','rt');
textdata=textscan(id,'%s%s');
fclose(id);
end
However, it gives an error like that .. ''Invalid file identifier. Use fopen to generate a valid file identifier.''
Could you tell me what is wrong in this code and how can I make it work ?
  댓글 수: 2
Image Analyst
Image Analyst 2012년 6월 25일
The FAQ will show you: http://matlab.wikia.com/wiki/FAQ#How_can_I_process_a_sequence_of_files.3F
Stephen23
Stephen23 2017년 6월 26일
See Image Analyst's answer for a good explanation and solution. Storing the filenames is not required.

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

채택된 답변

Richard
Richard 2012년 6월 21일
The main problem you have here is that you have not stored each of the text file names.
The code shown below should solve this:
textFilename = cell(1,15);
id = cell(1,15);
for k=1:15;
textFilename{k} = fullfile('C:\Documents and Settings\guestaskin\Desktop\model',['CASE' num2str(k) '.txt']);
id{k} = fopen(textFilename{k},'rt');
textdata{k} = textscan(id{k},'%s%s');
fclose(id{k});
end
So, here you create two empty cell arrays, the first stores the different file names, and the seconds stored the id. The remainder of the code should work as long as the files are stored in the files you specified. The error just states that you have not defined a correct file name. By creating the filename by parts (using fullfile) you are able to generate a different filename for each of the text files. Hope this helps.
  댓글 수: 2
Muharrem Askin
Muharrem Askin 2012년 6월 25일
thank you for your help. It works :)
Image Analyst
Image Analyst 2012년 6월 25일
He doesn't need to store each of the filenames. They can be temporary and reused during each iteration. Moreover, he doesn't need to store the file ID's. Why complicate things with storing those in a cell array when they don't need to be???

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

추가 답변 (1개)

Image Analyst
Image Analyst 2012년 6월 25일
The main problem is that you didn't construct the filename properly. You hard coded textFilename into the string instead of the contents of the textFilename variable into the string. Here's a better, more robust way of doing it (untested since I don't have your files)
folder = 'C:\Documents and Settings\guestaskin\Desktop\model';
for k=1:15
baseFilename = sprintf('CASE%d.txt', k);
fullFileName = fullfile(folder, baseFileName);
if exist(fullFileName, 'file')
id=fopen(\textFilename','rt');
textdata{k} = textscan(id,'%s%s');
fclose(id);
else
warningMessage = sprintf('Warning: file does not exist:\n%s', fullFileName);
uiwait(warndlg(warningMessage);
textdata{k} = warningMessage;
end
end

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by