필터 지우기
필터 지우기

How to save matrixes to files with similar names in loop?

조회 수: 2 (최근 30일)
MC3105
MC3105 2014년 10월 20일
댓글: Guillaume 2014년 10월 20일
Hello everyone,
with your help I was able to create a loop that loads different datasets and then runs through my whole code. The names of the original datasets are pretty similar: for example data_ID_200.mat and data_ID_304.mat I used the following command to load these data files:
l=dir('Z:\.....\data_ID_*.mat')
names={l.name}
for n=1:numel(names)
load('[Z:\...\' names{n}])
run('Code')
end
Now before the loop ends and starts the next iteration I want to save some matrixes that my code created. How can I specify the names for the matrixes I want to save? Preferably I would like to save these matrixes to matrix_ID_200.mat and matrix_ID_304.mat - so that they correspond directly to my original datasets and I can easily use them in further applications that require the ID of each dataset.
Is there anyway I could do this? Right now my list for names contains:
names = 'data_ID_200.mat' 'data_ID_304.mat'
Maybe I could kind of cut out the ID number and insert them into some kind of string??
Thanks a lot for your help!!!

채택된 답변

Guillaume
Guillaume 2014년 10월 20일
There are many ways you could do this, for example using a regular expression to extract the ID, but a simple way:
savename = strrep(names{n}, 'data', 'matrix');
save(savename, ...);
  댓글 수: 3
MC3105
MC3105 2014년 10월 20일
thanks! I did it wrong before, but I found out how to do it now :)
Guillaume
Guillaume 2014년 10월 20일
Use save with the savename, and the names of the variables you want to save. Use fullfile to build the full path:
save(fullfile(''Z:\.....\', savename), 'somevarname');

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

michael scheinfeild
michael scheinfeild 2014년 10월 20일
files=dir(fullfile("data","*.dat");
for(n=1:length(files))
d=files(n).name;
somename=strcat('a','bla',num2str(n));....
end

카테고리

Help CenterFile Exchange에서 Data Import from MATLAB에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by