MATLAB Answers

Add Variables to workspace from a struct

8 views (last 30 days)
I have a struct called "Values" that has two headers - Name and Data.
My final goal is to export the variables as a .mat file that I would be able to use afterwards for other things
The ouput I want to accomplish is that it looks like this in the workspace when the .mat file is loaded into another .m file:
%Name Value
S1 [3000000x1 double]
S2 [3000000x1 double]
S3 [3000000x1 double]
S4 [3000000x1 double]
S5 [3000000x1 double]
So far I have tried to accomplish this as follows:
Names = {Values(:).Name}';
Data = {Values(:).Data}';
assignin('base',string(Names(:)),cell2mat(Data(:)))
But it give me an error
Error using assignin
Invalid variable name "" in ASSIGNIN.

  0 Comments

Sign in to comment.

Accepted Answer

Ameer Hamza
Ameer Hamza on 19 Nov 2020
Edited: Ameer Hamza on 19 Nov 2020
It is not a good coding practice to create a variable name like S1, S2, ... https://www.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval. It usually creates complex, inefficient, hard to debug code, and In the long term, such a code is hard to maintain. Keep the code in an array format. However, if you want to do this, then try something like this.
Names = {Values(:).Name}';
Data = {Values(:).Data}';
cellfun(@(x,y) assignin('base', x, y), Names, Data)

  3 Comments

Jimmy Neutron
Jimmy Neutron on 19 Nov 2020
Do you mean
cellfun(@(x_,y_) assignin('base', x_, y_), Names, Data)
If so when I try it on other structures I get "Invalid variable name "Untitled/SP-78" in ASSIGNIN."
Ameer Hamza
Ameer Hamza on 19 Nov 2020
Yes, that was the mistake in my answer. I have updated it. Can you share your 'Values' struct in a .mat file?
Jimmy Neutron
Jimmy Neutron on 21 Nov 2020
Thank you for your reply.Ii have solved my error. It was that my "Values" struct contained boxes with names that had a '.' or a slash in them. When I replaced them with a underscore, everything worked fine :)

Sign in to comment.

More Answers (1)

Stephen Cobeldick
Stephen Cobeldick on 19 Nov 2020
Edited: Stephen Cobeldick on 19 Nov 2020
"My final goal is to export the variables as a .mat file that I would be able to use afterwards for other things"
Then creating variables names dynamically is entirely the wrong approach:
The robust and efficient solution is to use the -struct syntax of save, e.g.:
S(1).data = 1:3;
S(1).name = 'S1';
S(2).data = 4:6;
S(2).name = 'S2';
S(3).data = 7:9;
S(3).name = 'S3';
C = [{S.name};{S.data}];
T = struct(C{:});
save('myfile','-struct','T')
Checking the mat-file contents:
whos -file myfile.mat
Name Size Bytes Class Attributes S1 1x3 24 double S2 1x3 24 double S3 1x3 24 double
"The ouput I want to accomplish is that it looks like this in the workspace when the .mat file is loaded into another .m file:"
The recommended way to load data from a mat-file is into an output structure:
S = load('myfile')
S = struct with fields:
S1: [1 2 3] S2: [4 5 6] S3: [7 8 9]
This avoids a number of bugs that can occur when loading directly into a workspace:
You can access the fieldnames dynamically:
Note that using numbered variables is a sign that you are doing something wrong. Most likely the data could be designed better so that your code would be simpler and more efficient, e.g. by replacing those pseudo-indices with real indices into an array (which could be a container type, e.g. cell, structure, table, etc.).

  3 Comments

Jimmy Neutron
Jimmy Neutron on 21 Nov 2020
Thank you very much for your reply. I have tried to save my struct as you said, but unfortunately something did not work (possibly my naming). I fully understand that this is the most efficient solution, but I wanted to create a .mat file where once loaded all the variables were displayed right there (So I could play around with values without double clicking on anything). I hope it is understandable what I wrote :) As Ameer Hamza's answer worked best for me I have accepted that one. (if I could accept 2, then Ii would add yours ;) )
Stephen Cobeldick
Stephen Cobeldick on 21 Nov 2020
"but I wanted to create a .mat file where once loaded all the variables were displayed right there"
That is exactly what my answer gives you. The difference is that my answer uses a more effiicent approach (i.e. no assignin or eval or the like), but it certainly gives exactly the same mat file (which is why I showed the content of the mat file using whos).
Loading into an output variable is recommended, but does not change the content of the mat file. You can load it into the workspace directly just as you describe.
Jimmy Neutron
Jimmy Neutron on 24 Nov 2020
Okay, thank you. I will re-read your solution :)

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!