How to import multiple .m files

조회 수: 5 (최근 30일)
liu James
liu James 2016년 12월 9일
편집: Stephen23 2016년 12월 10일
Is there a simple loop or so to call multiple files and extract the sub structures from it where all .m files have the same substructure?
  댓글 수: 1
Star Strider
Star Strider 2016년 12월 9일
Are you asking about MATLAB script ‘.m’ files or binary ‘.mat’ files?

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

답변 (2개)

Park Joohyung
Park Joohyung 2016년 12월 9일
I`m answering assuming that you are mentioning '.mat' file. ('.m' file is merely a matlab script file..) If the data you want to load has regular name, like 'name1.mat', 'name2.mat'..., I recommend you to use 'eval' function with for loop.
for i=1:10
eval( ['load(''name' num2str(i) '.mat'')'] );
end
  댓글 수: 2
Stephen23
Stephen23 2016년 12월 10일
편집: Stephen23 2016년 12월 10일
Do not do this. This is pointless usage of eval, which serves no functional purpose at all in this code, and will only make this code slower, buggier, and harder to follow:
Instead of writing cargo-cult code using eval, simply call load directly:
S = load(sprintf('name%d.mat',k))
Park Joohyung
Park Joohyung 2016년 12월 10일
Thank you stephen, i admit that, your code looks much simpler.

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


Image Analyst
Image Analyst 2016년 12월 10일
See the FAQ for how to process a bunch of files: http://matlab.wikia.com/wiki/FAQ#How_can_I_process_a_sequence_of_files.3F.
In the middle of the loop, put Stephen's code to store all the structures pulled from your mat files.
matFullFileName = fullfile(pwd, sprintf('name%d.mat',k)); % Whatever.....
S{k} = load(matFullFileName) % Store this file's structure in our structure array.

카테고리

Help CenterFile Exchange에서 Variables에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by