how to refer variable from string name

182 visualizzazioni (ultimi 30 giorni)
Hello,
I have an array of string names for variables I need to use, and variables with such names in workspace. How should I refer? for example I need to find mean for variables with names 'm1_n' 'm3_n' specified in array A. The names will be different all the time -- I cant just write mean(m1_n) I need to do it through A and indexing, but in A names are strings.
  5 Commenti
Yelena Bibineyshvili
Yelena Bibineyshvili il 19 Mag 2021
yes they are created by other script
Stephen23
Stephen23 il 19 Mag 2021
"yes they are created by other script"
The best** solution is to fix that other script so that it does not store meta-data (e.g. indices) in variable names.
** best in the sense more efficient (run-time, programming time, debugging time, maintenance time), easier to understand (simpler code, no obfuscation, clear where data comes from), easier to debug (variable highlighting, static code checking, mlint checking), etc.

Accedi per commentare.

Risposta accettata

Jeff Miller
Jeff Miller il 19 Mag 2021
Modificato: Jeff Miller il 19 Mag 2021
This is pretty clumsy, but I think something like this will do what you want:
varnames = {'m1_n', 'm3_n'}; % names of the variables you want to process
save('temp.mat'); % save all workspace variables into a temporary mat file
g = load('temp.mat'); % load all variables into a "global" structure
x = mean(g.(varnames{1})); % get the mean of the first named variable
y = max(g.(varnames{2})); % get the mean of the second named variable
The keys are that (1) the load command makes a structure g with fields corresponding to the names of the original workspace vars, and (2) the syntax g.(s) picks out the field named by the string s.
Don't forget to delete temp.mat.

Più risposte (1)

Steven Lord
Steven Lord il 18 Mag 2021
Can you do this? Yes.
Should you do this? The general consensus is no.
Rather than defining variables with numbered names, consider putting them in an array.
x1 = 1:5;
x2 = x1 + 5;
x3 = x1 + 10;
% or
xmat = [x1; x2; x3];
To iterate through x1, x2, and x3 I'd need to use the discouraged approach. To iterate through the rows of xmat is easy.
for k = 1:size(xmat, 1) % or 1:height(xmat)
y = xmat(k, :)
% Do something with y
end
y = 1×5
1 2 3 4 5
y = 1×5
6 7 8 9 10
y = 1×5
11 12 13 14 15
  4 Commenti
Steven Lord
Steven Lord il 18 Mag 2021
x = cell(1, 3);
for k = 1:3
x{k} = magic(k+2);
end
for k = 1:3
fprintf("Element %d of x is of size %s.\n", k, mat2str(size(x{k})))
end
Element 1 of x is of size [3 3]. Element 2 of x is of size [4 4]. Element 3 of x is of size [5 5].
Yelena Bibineyshvili
Yelena Bibineyshvili il 18 Mag 2021
I already have variables and I want to avoid making another array from variables, here you write x=magic or random, but in my program I first will need to make that array from my varibles which have different names for each subject and different days. I need to know how to use variable with name specified in string.

Accedi per commentare.

Categorie

Scopri di più su Characters and Strings in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by