using variables whose names are stored in another array

8 views (last 30 days)
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

Accepted Answer

Guillaume
Guillaume on 29 Jul 2015
As per Stephen's comment, you're on your way to more trouble and you really need to rethink the way you're storing your data. Your 400 different vectors should have been stored in a matrix or table if all the same size, or a cell array otherwise. Either way, one line of fast, easy to debug, code would be all that's needed to add that single value.
As it is, it is still possible using eval and its friends ( evalin and assignin), but speed, ease of debugging, syntax highlighting all go out of the window:
%this code assumes it's run as a script.
for var = who'
tempval = eval(var{1}); %get variable value
tempval = [tempval, rand]; %add one value, replace rand by whatever you want
assignin('base', var{1}, tempval);
end
  2 Comments
Aditya
Aditya on 30 Jul 2015
thank you all for the comments, I was working with data that someone else had already created so I did not have a chance to change the way it was stored previously...
Stephen23
Stephen23 on 30 Jul 2015
Edited: Stephen23 on 30 Jul 2015
If the four hundred variables have been created by someone else then how did you get them into your MATLAB workspace?
You say that they were "...stored previously..." and if they were stored then you have complete choice between:
  • loading them individually into your workspace and writing ugly difficult-to-debug hack code based on eval to try working with them.
  • loading them properly into one cell array or structure and using the inbuilt tools to help you program reliably.

Sign in to comment.

More Answers (2)

Stephen23
Stephen23 on 29 Jul 2015
Edited: Stephen23 on 19 Jun 2019
  2 Comments
Aditya
Aditya on 29 Jul 2015
Edited: Aditya on 29 Jul 2015
thank u for your answer, unfortunately I already have about 400 variables (each is an array of numbers) and I wanted to do only one simple operation with all of them and that is to add one more element to each array. So I wanted to make a for loop so that this operation can be done, so I wanted to store the variable names in one cell array and call it in the for loop, so as I understand this cannot be done, please advice
Stephen23
Stephen23 on 29 Jul 2015
Edited: Stephen23 on 29 Jul 2015
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

Sign in to comment.


Walter Roberson
Walter Roberson on 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);

Categories

Find more on Cell Arrays in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!