Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Matlab中使用字符串变量间接引用结构变量_Matlab_Variables_Call - Fatal编程技术网

在Matlab中使用字符串变量间接引用结构变量

在Matlab中使用字符串变量间接引用结构变量,matlab,variables,call,Matlab,Variables,Call,我试图创建一个变量,在这里我可以键入一个与结构中的变量相对应的字符串,这样我就可以创建一组图,而图之间的唯一区别就是变量 即目前: example(1:100).variable1=[matrix] example(1:100).variable2=[matrix] example(1:100).variable3=[matrix] for i=1:100 figure (i) surf(x,y,example(i).variable1) [Formatting

我试图创建一个变量,在这里我可以键入一个与结构中的变量相对应的字符串,这样我就可以创建一组图,而图之间的唯一区别就是变量

即目前:

example(1:100).variable1=[matrix]
example(1:100).variable2=[matrix]
example(1:100).variable3=[matrix]
for i=1:100
   figure (i)    
   surf(x,y,example(i).variable1)    
   [Formatting code]

   surf(x,y,example(i).variable2)
   [Formatting code]

   surf(x,y,example(i).variable3)    
   [Formatting code]
end
因为有相当多的格式化代码,而且我一次只关心一个变量,所以我想要一种更好的方法,而不是注释掉我想要/不想要的任何一个变量集

有没有办法做到以下几点

即理想:

example(1:100).variable1=[matrix]
example(1:100).variable2=[matrix]
example(1:100).variable3=[matrix]

stringVariable='variable1'

for i=1:100
   surf(x,y,example(i).stringVariable)    
   [Formatting code]
end

感谢您的建议。

在matlab中实现这一点的方法是在使用变量引用结构字段时将其封装在括号中

例如:

>> thisstruct.A = [1 1 1];
>> thisstruct.B = [0 0 0];
>> variable = 'A';
>> thisstruct.(variable)
ans =
 1     1     1
>> variable = 'B';
>> thisstruct.(variable)
ans =
     0     0     0

我完全忘了我在这里问过一个问题,但是谢谢你的回答!一年半后也没弄明白!