Search code examples
matlabmatlab-struct

Save variables into a struct from the workspace


enter image description here

As you can see in the picture, I have three variables, two are named number_of_steps_... and the third one is struct.

How can I save the content saved in the number_of_steps variables into struct?

I have tried the save command but that doesn't work for what I need. What is the right way to do this?


Solution

  • I don't know what you mean by "save", but you can add them to the struct by using

    mystruct.number_of_steps1 = number_of_steps1
    mystruct.number_of_steps2 = number_of_steps2
    

    Then save the struct to a file using

    save mystruct.mat mystruct
    

    Notice I've taken the liberty of renaming the variable struct as mystruct. Since "struct" is a Matlab command, it's not a good idea to use it as a variable name.