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

3 visualizaciones (últimos 30 días)
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

Respuesta aceptada

TAB
TAB el 10 de En. de 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);
  1 comentario
Emerson De Souza
Emerson De Souza el 10 de En. de 2012
Tabrez
Thank you a lot for your suggestion.
wish you a nice day
Emerson

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Get Started with MATLAB en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by