How to load .mat file which has a variable filename?

强颜欢笑 提交于 2019-12-02 07:51:10

问题


%select all .mat files

oar = dir('*oar.mat'); n = {oar.name};

%loop through files

for l=1:length(oar);

load pat_oar(l) %<---this is the .mat file with variable filename


clear ...

end

How do I write some Matlab script that will read in one .mat file after another...


回答1:


You file names are stored in n, so you should be able to do:

for l=1:length(oar)
    load(n{l})
end



回答2:


Use the functional form. Instead of:

load pat_oar{I}

Use

load(pat_oar{I})

Calling a Matlab command using the unix-style syntax (i.e. command arg1 arg2) is just syntax shorthand for the more verbose syntax of command('arg1','arg2'). It's a pretty common trick to use the more verbose syntax anytime an argument is stored in a variable.



来源:https://stackoverflow.com/questions/17727919/how-to-load-mat-file-which-has-a-variable-filename

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!