How to import data for multiple files using for loop?

73 visualizaciones (últimos 30 días)
kamal
kamal el 18 de Jul. de 2019
Comentada: Raj el 2 de Ag. de 2019
I named files as 1.data,2.data,....
I want to import these data,process the data using functions,plot the processed data.
my code is
Capture.PNG
When i give nname in command window it is showing 1.data. but i am getting error as
Capture.PNG

Respuesta aceptada

Raj
Raj el 18 de Jul. de 2019
Here is a portion of code I use to read multiple files in cases like this:
for r=1:6 % Number of files
myfilename= sprintf('%i.data',r); %file names as 1.data, 2.data and so on.
filename = fullfile('C:\','Users','User','MATLAB',myfilename); % Put your file path here
fid = fopen(filename,'r'); % Open the file
Mydata = textscan(fid,'%f %f');% Assuming you have 2 columns of floating point data. Update as per your file accordingly.
fid = fclose(fid); % CLose the file
%%Delete temporary variables from workspace
%%Process the data
%%Plot the data
end
Hope this helps!!
  3 comentarios
kamal
kamal el 2 de Ag. de 2019
I want to save plot as 1.fig ,2.fig....
eval(['hgsave(''myfilename'');']);
All the files are saving in myfilename. Whereas myfilename is different for each loop as 1,2,3,,..6.
how to edit hgsave as to save in loop with different names as 1.fig,2.fig,...6.fig
Raj
Raj el 2 de Ag. de 2019
I don't think you need eval for this. Try something like this:
for r=1:6 % Number of files
myfilename= sprintf('%i.data',r); %file names as 1.data, 2.data and so on.
filename = fullfile('C:\','Users','User','MATLAB',myfilename); % Put your file path here
fid = fopen(filename,'r'); % Open the file
Mydata = textscan(fid,'%f %f');% Assuming you have 2 columns of floating point data. Update as per your file accordingly.
fid = fclose(fid); % CLose the file
%%Delete temporary variables from workspace
%%Process the data
%%Plot the data
figure(r) % this will give you a new figure for each set of data
plot(x,y) % Put your data here
file_name=sprintf('%d',r)
hgsave(file_name)
end

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Large Files and Big Data en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by