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

3
  • 4
    I don't think you really want to do this. Why do you want to do this? Commented Jan 27, 2016 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. Commented Jan 27, 2016 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 Commented Jan 27, 2016 at 23:51

2 Answers 2

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})
Sign up to request clarification or add additional context in comments.

Comments

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.

4 Comments

eval is a terrible suggestion
@excaza is a terrible commenter. Please, elaborate on why you don't like my suggestion
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 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.
For a good reason the documentation page for the functions links alternatives to eval.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.