"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.:
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 =
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.).
0 Comments
Sign in to comment.