开发者

Matlab's 'save' function - how to name the variable inside the .mat file same as file name?

开发者 https://www.devze.com 2023-01-19 01:34 出处:网络
I want to save the result of the q for each case_no in corresponding string of the q_cases as a .mat file. With my statement of save(q_cases{case_no},\'q\') even though the names of files are coming a

I want to save the result of the q for each case_no in corresponding string of the q_cases as a .mat file. With my statement of save(q_cases{case_no},'q') even though the names of files are coming as the corresponding string of q_cases, however all those .mat files contain variable with the same name of q. When I open those .mat files I g开发者_StackOverflow中文版et a variable with name q for all the 3 files. However, I want the names of the variables stored in those files same as the name of the files i.e. q_a, q_b and q_c respectively.


One way to solve this is to assign the variable name with eval.

EDIT

Eval is usually not recommended, since it is difficult to debug/maintain. Thus, you can instead create a structure first and use the -struct-option of save, like this:

for case_no=1:length(n)
       [q,S]=q_from_A(nModel,nModel_want,nCell,T,A{case_no},B{case_no},C{case_no},D{case_no},E{case_no},F{case_no});
    %# create structure for saving
    saveStruct = struct(q_cases{case_no},q,...
        S_cases{case_no},S);
    %# ... and save it
    save(q_cases{case_no},'-struct','saveStruct',q_cases{case_no});
    save(S_cases{case_no},'-struct','saveStruct',S_cases{case_no});
end
0

精彩评论

暂无评论...
验证码 换一张
取 消