using variables whose names are stored in another array

8 次查看(过去 30 天)
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

采纳的回答

Guillaume
Guillaume 2015-7-29
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 个评论
Aditya
Aditya 2015-7-30
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 2015-7-30
编辑:Stephen23 2015-7-30
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.

请先登录,再进行评论。

更多回答(2 个)

Stephen23
Stephen23 2015-7-29
编辑:Stephen23 2019-6-19
  2 个评论
Aditya
Aditya 2015-7-29
编辑:Aditya 2015-7-29
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 2015-7-29
编辑:Stephen23 2015-7-29
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
Walter Roberson 2015-7-29
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);

类别

Help CenterFile Exchange 中查找有关 Cell Arrays 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by