MATLAB Answers


How can I make struct with existing variables?

Asked by Giorgi
on 22 Aug 2014
Latest activity Edited by Matt J
on 22 Aug 2014
Accepted Answer by Matt J

Hi all, Well i have some variables for example alfa, beta and gama, Now I want to make structure that will contain these variables. Can someone give me a code or function with which i will be able to do it.



No products are associated with this question.

4 Answers

Answer by Matt J
on 22 Aug 2014
Edited by Matt J
on 22 Aug 2014
 Accepted answer

Using structvars ( Available here ).

>> clear
>> a=1; b=2; c=3;
>> s=whos;
>> C=cell2struct({}.',{})
C = 
      a: 'a'
      b: 'b'
      c: 'c'
>> structvars(C,0); %copy/paste the output below to the command line
ans =
C.a = a;    
C.b = b;    
C.c = c;   


First of all thanks for your activity and help now well I folloew your code but when I write structvars(C,0) it says Undefined function 'structvars' for input arguments of type 'struct'.

Did you follow Matt J's 'Available here' link and add the downloaded location to your path?

Though I don't really recommend it, you could also do

>> eval(structvars(C,0).'); C
C = 
      a: 1
      b: 2
      c: 3

Answer by Matt J
on 22 Aug 2014
Edited by Matt J
on 22 Aug 2014

 >> save tempFile *
 >> S=load('tempFile');


Answer by Adam
on 22 Aug 2014
Edited by Adam
on 22 Aug 2014

myStruct.alfa = alfa;
myStruct.beta = beta;



myStruct = struct( 'alfa', alfa, 'beta', beta );


Is it every variable in your workspace or just some subset? If you can create a cell array of all the variable names then you can also use that with an equivalent cell array of the values of each variable to pass to struct.

Well as you say in txt file I have all variable names, so I measured its length and use cycle here is a code but it does not work any ideas?

for i=1:a

I could try to give some help further along this line, but I think potentially either of Matt J's solutions would work better so unless neither of them do I'll leave it.

Answer by Guillaume
on 22 Aug 2014

s = struct('alpha', alpha, 'beta', beta, 'gamma', gamma);
doc struct


Well that is fine but if i have more than 30 variables should I write it manually? The problem is that I cant write a code with which it automaticly will assign all variables

You could dynamically create a structure with 30 field names that are the names of the variables, however I would urge to think whether it's really what you want. How are you going to then use that structure if the field names are created arbitrarily. I would advise against this as your code may easily break in the future because a hardcoded field is not there anymore.

If that's really what you want to do, then structvars from the other answer (you have to download it), or the following function will do it:

function s = variables2struct(varargin)
  s = struct;
  for var = 1:nargin
    s.(genvarname(inputname(var), fieldnames(s))) = varargin{var};

Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!