Loop through list of variable in workspace to get and store in matrix

119 visualizaciones (últimos 30 días)
How can i loop through list of variable 'selected' in workspace to get and store in one matrix (h).
it only stores the last matrix using my code. please help
clear
A=[1, 2, 3]'
B=[3,4,5]'
C=[3,7,9]'
x=who
selected={'A', 'B' 'C'};
m=length(selected)
h=zeros(3,m)
for cv = x
y= eval(sprintf('%1$s', cv{m}))
h(:,cv) = y
end
%%Result
A=[1 3 3;2 4 7; 3 5 9]
  4 comentarios
joms
joms el 24 de En. de 2021
the data is from a matfile with saved signal, each has 1xn matrix. i just want to extract those data based on the signal list that i specify
selected={'A', 'B' 'C'};
and save it as new matrix to print as csv.i used this method because there are some signals in the list not available in the matfile and want to substitute by zeros. Do you have more efficient method of extracting specifice struct data from workspace? thank you.
Stephen23
Stephen23 el 24 de En. de 2021
Editada: Stephen23 el 24 de En. de 2021
"Do you have more efficient method of extracting specifice struct data from workspace? thank you."
NO, there is no efficient way to do this: once the data is in the workspace as lots of separate variables, you have forced yourself into writing slow, complex, inefficient, obfuscated code to try and access it. The best approach is to avoid needing to do this, which is easy to do.
"the data is from a matfile with saved signal, each has 1xn matrix."
Then you can easily write neat, simple, efficient code by loading into one variable when you load the data into the workspace, exactly as the MATLAB documentation shows:
It is much better to avoid the bad situation that you are getting yourself into, by simply writing simple robust code, e.g. by loading into an output variable:
S = load(..)
And storing that data in one variable, exactly as the documentation shows.

Iniciar sesión para comentar.

Respuesta aceptada

Walter Roberson
Walter Roberson el 24 de En. de 2021
selected={'A', 'B' 'C'};
filename = 'YourFile.mat';
vars_present = who('-file', filename);
present_mask = ismember(selected, vars_present);
present_idx = find(present_mask);
num_selected = length(selected);
datacell = cell(1, num_selected);
for cv = present_idx
varname = selected{cv};
datastruct = load(filename, varname);
datacell{cv} = datastruct.(varname);
end
if isempty(present_idx)
%NONE of the vars are present. Use 0 all around
datacell(1:num_selected) = {0};
else
%fill in the missing the same size as the first that exists
numrow = size(datacell(present_idx{1}), 1);
datacell(~present_mask) = {zeros(numrow,1)};
end
h = cell2mat(datacell);
%now write out h to appropriate csv file
  3 comentarios
Walter Roberson
Walter Roberson el 24 de En. de 2021
numrow = size(datacell{present_idx(1)}, 1);

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Numeric Types en Help Center y File Exchange.

Productos


Versión

R2016b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by