0

Suppose I have three variable stored in workspace like following

a = [1 2 4 8]
b = [4 9 3 8]
c = [8 6 4 2]

and I want to make a cell array of them and store it in x using a who command like

x = who

now a new cell array is stored as x that contains { 'a' 'b' 'c' }

but how can I call the values of variables stored in a like x{1} returns me value 'a' and I want to get

[1 2 4 8]

as the result

Thanks

Zeryab Hassan Kiani
  • 467
  • 1
  • 7
  • 18
  • 4
    I don't think you really want to do this. Why do you want to do this? – sco1 Jan 27 '16 at 23:44
  • It is indeed possible to do this, but, as @excaza points out, that's not really what you want. Why do you have these variables, why are they in your workspace and why do you want to call them? There's probably a better storage way with easy access, but we don't know until you tell us more. – Adriaan Jan 27 '16 at 23:48
  • I am developing a GUI and I have .mat file and I have to plot different graphs, for these graphs I want load these variables in popup menu and then plot the graphs So I need help – Zeryab Hassan Kiani Jan 27 '16 at 23:51

2 Answers2

2

Rading the comment you should start in a different way. Use load with an output argument!

data=load(...)

This way all your data is stored in a struct called data with the field names a b and c. Now continue with your code:

%replacement for who, returning all field names
x=fieldnames(data)
%access first field using dynamic field names
data.(x{1})
Daniel
  • 36,610
  • 3
  • 36
  • 69
0

As it was mentioned in comments to your previous question by @JohnHascall, @MatthiasW and @AndrasDeak, x = who would return you the list of variables in the form of cell array. There can be many different ways to record the values of those variables. I suggest to take advantage from the fact that the elements of x are strings, so we can access the values of the variables by using eval inside a loop:

x = who;
for ii = 1:numel(x)
    y{ii} = eval(x{ii});
end

The statement inside a loop would create another cell array and would record the values of corresponding variables. So, you would end up with something like this:

x = 

    'a'
    'b'
    'c'
    'd'
    'e'
y = 

[5]    'Hello'    [1x100 double]    [14x14 double]    [512x512x3 uint8]

So, suppose, you now wish to access b(3). You would do it like this:

y{2}(3)

ans =

    l

Weird, but works anyway.

Community
  • 1
  • 1
brainkz
  • 1,335
  • 10
  • 16
  • 2
    `eval` is a terrible suggestion – sco1 Jan 28 '16 at 03:42
  • @excaza is a terrible commenter. Please, elaborate on why you don't like my suggestion – brainkz Jan 28 '16 at 07:24
  • 1
    Because `eval` is a terrible function. The problem with that function is that it breaks *everything* in MATLAB. It can't compile your code any more, gives rise to all kinds of hard to find errors etc. See [this answer](http://stackoverflow.com/questions/32467029/how-to-put-these-images-together/32467170#32467170) for a longer list of reasons why `eval` is indeed an evil function. Therefore I'd suggest setting a couple of warnings about the usage of `eval` around the post. – Adriaan Jan 28 '16 at 11:53
  • 2
    For a good reason the [documentation page](http://www.mathworks.com/help/matlab/ref/eval.html) for the functions links alternatives to eval. – Daniel Jan 28 '16 at 11:56