using variables whose names are stored in another array
Ältere Kommentare anzeigen
Hi
I have created a list of variables in the workspace using 'who' and now I would like to use the variable names stored to conduct operation on them but I am having trouble access the. For example I have used command a=who; or a= whos; and now a has names of about 400 variables in the workspace, so then I want to b able to do operations on the variables by using values stored in a. e.g. a(1,1).names = 'ABC' and ABC is an array of 10 values and I want to operate on those values but I can't seem to access the values of ABC using a... please help
Akzeptierte Antwort
Weitere Antworten (2)
2 Kommentare
It can certainly be done, as you would find reading those links that I gave.
But it is ugly hack code, and the question remains: how did you "create" four hundred variables in your workspace? There seem to be two likely possibilities, both of which are much loved by beginners who don't realize how much trouble they really cause:
- dynamically named variables. In this case read all of my original answer and learn to use faster and more robust programming tools.
- loading variables directly into the workspace using load. In this case you can simply use the output of load and avoid the whole problem:
out = load(...);
Where you can put out into a cell array or process it immediately as you wish. Probably the most efficient solution is to load the data into a non-scalar structure:
for k = numel(files):-1:1
S(k) = load(files{k});
end
Walter Roberson
am 29 Jul. 2015
save('TempMat.mat');
datastruct = load('TempMat.mat');
now datastruct is a struct with one entry for every variable that was in the workspace. You can work on them by different means, including structfun(). For example to average them all, assuming they are all the same size and of maximum dimension 2:
datacell = struct2cell(datastruct);
mean_of_all = mean( cat(3, datacell{:}), 3);
Kategorien
Mehr zu Variables finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!