How to import multiple indexed .txt files using a loop?

조회 수: 3 (최근 30일)
Emerson De Souza
Emerson De Souza 2012년 1월 10일
MOTIVATION: I want to: a) create, b)save and c) import .txt-tables using an index i within a loop.
EXAMPLE:
To create and save .txt-tables I wrote (for example) the following command lines:
for i=1:5;
V{i}=i-10:i:i+10;
fid = fopen(sprintf('Q_%d.txt',i),'wt');
fprintf(fid, [repmat('%g\t', 1, size(V{i},2)-1) '%g\n'], V{i}.');
fclose(fid);
end;
PROBLEM:
Now I want to import the Q_index.txt files also using a loop, such as:
for j=1:5;
fid =fopen(sprintf('Q_%d.txt',j),'wt');
QIMPORTED{j}=textscan(fid,'%f','TreatAsEmpty','NA','EmptyValue',NaN);
fclose(fid);
end;
but I obtain only empty cells.
QUESTION: I wonder if someone knows how to import multiple text files such as TABLE_index.txt using at once
using a loop command.
Thank you in advance for your attention
Emerson

채택된 답변

TAB
TAB 2012년 1월 10일
While reading the file, you are opening the file again in 'write mode'. This will empty the files.
Use 'r' instead of 'wt' when you are opening the file to for reading.
fid =fopen(sprintf('Q_%d.txt',j),'r');
QIMPORTED{j}=textscan(fid,'%f','TreatAsEmpty','NA','EmptyValue',NaN);
  댓글 수: 1
Emerson De Souza
Emerson De Souza 2012년 1월 10일
Tabrez
Thank you a lot for your suggestion.
wish you a nice day
Emerson

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by