How to import data for multiple files using for loop?

78 ビュー (過去 30 日間)
kamal
kamal 2019 年 7 月 18 日
コメント済み: Raj 2019 年 8 月 2 日
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

採用された回答

Raj
Raj 2019 年 7 月 18 日
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 件のコメント
kamal
kamal 2019 年 8 月 2 日
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 2019 年 8 月 2 日
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

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLarge Files and Big Data についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by