How to import multiple indexed .txt files using a loop?
1 view (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
TAB
on 10 Jan 2012
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);
More Answers (0)
See Also
Categories
Find more on Characters and Strings in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!