MATLAB:遍历“谁"功能中的列表值 [英] MATLAB: Loop through the values of a list from 'who' function
问题描述
我的工作区中有一长串变量.
首先,我找到了可能对使用who
函数感兴趣的潜在变量.接下来,我想遍历此列表以查找每个变量的大小,但是who
仅将变量的名称输出为字符串.
I have a long list of variables in my workspace.
First, I'm finding the potential variables I could be interested in using the who
function. Next, I'd like to loop through this list to find the size of each variable, however who
outputs only the name of the variables as a string.
如何使用此列表引用变量的值,而不仅仅是名称?
How could I use this list to refer to the values of the variables, rather than just the name?
谢谢
list = who('*time*')
list =
'time'
'time_1'
'time_2'
for i = 1:size(list,1);
len(i,1) = length(list(i))
end
len =
1
1
1
推荐答案
如果您想要有关变量的详细信息,可以使用whos
代替,它将返回一个包含(除其他外)尺寸(size
)和存储空间大小(bytes
).
If you want details about the variables, you can use whos
instead which will return a struct that contains (among other things) the dimensions (size
) and storage size (bytes
).
就获得值而言,您可以使用eval
,但是不建议这样做,而应考虑使用带有动态值的单元格数组或结构字段名称而不是动态变量名称.
As far as getting the value, you could use eval
but this is not recommended and you should instead consider using cell arrays or structs with dynamic field names rather than dynamic variable names.
S = whos('*time*');
for k = 1:numel(S)
disp(S(k).name)
disp(S(k).bytes)
disp(S(k).size)
% The number of elements
len(k) = prod(S(k).size);
% You CAN get the value this way (not recommended)
value = eval(S(k).name);
end
这篇关于MATLAB:遍历“谁"功能中的列表值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!