How to import data for multiple files using for loop?

73 Ansichten (letzte 30 Tage)
kamal
kamal am 18 Jul. 2019
Kommentiert: Raj am 2 Aug. 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

Akzeptierte Antwort

Raj
Raj am 18 Jul. 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 Kommentare
kamal
kamal am 2 Aug. 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 am 2 Aug. 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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Large Files and Big Data finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by