Tob*_*ler 3 matlab struct loops wrapper
由于遗留函数调用,我有时会被迫编写像这样的丑陋包装器
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
Run Code Online (Sandbox Code Playgroud)
因此,基本思想是创建与someField字段名称相同的变量,运行函数并return使用someFunction返回变量的名称作为字段名创建结构.
是否有某种方法使用某些循环简化这个例如结束fieldnames(someField)?
或者我应该使用一些不同的方法?由于进行了一些进一步的处理,someField并且result我想继续使用结构,但也许第二个问题是
可以save和load重定向varibale名称?即,例如a,params.mat中的变量可以使用someField.aas值存储而不必先分配a = someField.a?
为什么不这样的?
如果这是s:
s.a=1
s.b=2
s.c=3
Run Code Online (Sandbox Code Playgroud)
然后,此命令创建一个名为"arguments"的matfile,其中包含变量a,b,c:
save arguments.mat -struct s
Run Code Online (Sandbox Code Playgroud)
此命令将matfiles变量加载到结构中
r = load('arguments.mat')
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3282 次 |
| 最近记录: |