How to import data for multiple files using for loop?

조회 수: 72 (최근 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 CenterFile Exchange에서 Large 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