MATLAB爱好者论坛-LabFans.com

MATLAB爱好者论坛-LabFans.com (https://www.labfans.com/bbs/index.php)
-   资料存档 (https://www.labfans.com/bbs/forumdisplay.php?f=72)
-   -   一次分配多个字段的巧妙方法? (https://www.labfans.com/bbs/showthread.php?t=26413)

poster 2019-12-14 20:13

一次分配多个字段的巧妙方法?
 
由于遗留函数调用,有时我不得不像这样写丑包装

function return = someWrapper(someField) a = someField.a; b = someField.b; % and so on, realistically it's more like ten variables that % could actually be grouped in a struct save('params.mat', 'a', 'b'); %etc. % then, on another machine, a function loads params.mat, does the calculations % and saves the result in result.mat containing the variables c,d,... load('result.mat', 'c', 'd'); return.c = c; return.d = d; % again, it's more than just two return values 因此,基本思想是使用与someField的字段名相同的名称创建变量,运行一个函数,并使用someFunction的返回变量的名称作为字段名来创建return结构。

有什么方法可以使用某些循环来简化此操作,例如在fieldnames(someField)吗?

还是我实际上应该使用其他方法?由于对someField和result做了一些进一步的处理, result我想继续使用结构,但是第二个问题可能是

可以save和load重定向变量名称吗?即例如可以使用someField.a作为值存储someField.a的变量a ,而不必首先分配a = someField.a ?



[B]回答:[/B]

为什么不这样呢?

如果是s:

sa=1 sb=2 sc=3 然后,此命令使用变量a,b,c创建一个名为“参数”的matfile:

save arguments.mat -struct s 然后此命令将matfiles变量加载到结构中

r = load('arguments.mat')

[url=https://stackoverflow.com/questions/5091284]更多&回答...[/url]


所有时间均为北京时间。现在的时间是 04:58

Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.