MATLAB eval
Function
Aug. 09, 2022
首先创建两个结构体
1
2
3
4
5
6
% Buid two structures
Stru1.Type = 'Integer';
Stru1.Value = 10;
Stru2.Type = 'Decimal';
Stru2.Value = 1.7;
想要通过循环的形式实现——将结构体的 Type 值作为变量名,并将结构体的 Value 值作为该变量的值,例如:
可以借助 eval
函数实现:
1
2
3
4
5
6
7
for i = 1:2
eval(['type = ', 'Stru', num2str(i), '.Type']);
% e.g. ['type = Stru1.Type']
eval([ type, ' = ','Stru', num2str(i), '.Value'])
% e.g. ['Integer = Stru1.Value']
end
eval
函数的基本语法是 eval(expression)
,一定要保证 experssion
是个字符串。
参考
[1] eval - MathWorks.