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

3 visualizzazioni (ultimi 30 giorni)
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

Risposta accettata

TAB
TAB il 10 Gen 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);

Più risposte (0)

Categorie

Scopri di più su Get Started with MATLAB in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by