How to import data for multiple files using for loop?

72 vues (au cours des 30 derniers jours)
kamal
kamal le 18 Juil 2019
Commenté : Raj le 2 Août 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

Réponse acceptée

Raj
Raj le 18 Juil 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 commentaires
kamal
kamal le 2 Août 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 le 2 Août 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

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Large Files and Big Data dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by