MATLAB: Loop through the values of a list from 'who' function

让人想犯罪 __ 提交于 2019-11-27 04:54:11

问题


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?

Thank you,

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

回答1:


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).

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



回答2:


@Suever nicely explained the straightforward way to get this information. As I noted in a comment, I suggest that you take a step back, and don't generate those dynamically named variables to begin with.

You can access structs dynamically, without having to resort to the slow and unsafe eval:

timestruc.field = time;
timestruc.('field1') = time_1;
fname = 'field2';
timestruc.(fname) = time_2;

The above three assignments are all valid for a struct, and so you can address the fields of a single data struct by generating the field strings dynamically. The only constraint is that field names have to be valid variable names, so the first character of the field has to be a letter.

But here's a quick way out of the trap you got yourself into: save your workspace (well, the relevant part) in a .mat file, and read it back in. You can do this in a way that will give you a struct with fields that are exactly your variable names:

time = 1;
time_1 = 2;
time_2 = rand(4);
save('tmp.mat','time*'); % or just save('tmp.mat')
S = load('tmp.mat');

afterwards S will be a struct, each field will correspond to a variable you saved into 'tmp.mat':

>> S

S = 

      time: 1
    time_1: 2
    time_2: [4x4 double]



回答3:


An example writing variables from workspace to csv files:

clear;

% Writing variables of myfile.mat to csv files
load('myfile.mat');
allvars = who;
for i=1:length(allvars)
    varname = strjoin(allvars(i));
    evalstr = strcat('csvwrite(', char(39), varname, '.csv', char(39), ', ', varname, ')');
    eval(evalstr);
end


来源:https://stackoverflow.com/questions/40226214/matlab-loop-through-the-values-of-a-list-from-who-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!