save specific files with MATLAB commands

人盡茶涼 提交于 2019-12-23 20:50:17

问题


I'm trying to save models in oldest MATLAB versions as below I look for each folder and subfolder to find any .mdl or .slx to save it as 2007b version

The problem I have is :

  1. it works if I just look for one extension whereas I'm wondering to do that on each .mdl and.slx .
  2. the save_system takes too much time

Do you know how could I get all .mdl and .slx and is there an optimized way to save ?

Thanks

rootPath = fullfile('M:\script\ytop','tables');
files = dir(rootPath );

for ii = 3:numel(files)

x = fullfile(rootPath ,files(ii).name);
cd(x);
mdl = { dir('*.mdl'),dir('*.slx')};  % here it works if only I set dir('*.mdl')
for jj = 1:numel(mdl)
    load_system(mdl(jj).name);
    save_system(mdl(jj).name,mdl(jj).name, 'SaveAsVersion','R2007b');
end   

end

回答1:


%here you used {} which created a cell array of two structs. cat creates a single struct which.
mdl=cat(1,dir('*.mdl'),dir('*.slx')); 
for jj = 1:numel(mdl)
    [~,sysname,~]=fileparts(mdl(jj).name);
    load_system(mdl(jj).name);
    %use only sysname without extension. R2007b is mdl only. You can't store files for R2007b in slx format
    save_system(sysname,sysname, 'SaveAsVersion','R2007b');
    %close system to free memory.
    close_system(sysname);
end   

Applying only the required fixes your code has one odd behaviour. For mdls the file is replaced with the original one, for slx a mdl is created next to the original one. You may want to add a delete(mdl(jj).name) after loading.



来源:https://stackoverflow.com/questions/28632020/save-specific-files-with-matlab-commands

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