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 Center および File ExchangeGet Started with MATLAB についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by