Matlab的“保存”函数 - 如何将 .mat 文件内的变量命名为与文件名相同?

发布于 2024-09-26 10:28:04 字数 355 浏览 1 评论 0原文

我想将每个 case_noq 结果保存在 q_cases 相应字符串中作为 .mat 文件。根据我的 save(q_cases{case_no},'q') 语句,即使文件名作为 q_cases 的相应字符串出现,但是所有这些 .mat 文件包含与q同名的变量。当我打开这些 .mat 文件时,我会得到所有 3 个文件的一个名为 q 的变量。但是,我希望存储在这些文件中的变量名称与文件名称相同,分别为 q_aq_bq_c

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 get 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梅倚清风 2024-10-03 10:28:04

解决此问题的一种方法是使用 eval 分配变量名称。

编辑

通常不建议使用 Eval,因为它很难调试/维护。因此,您可以首先创建一个结构并使用 -struct-保存选项,如下所示:

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文