How can I load a single variable from a structure?
109 views (last 30 days)
MathWorks Support Team on 2 Sep 2021
Edited: MathWorks Support Team on 8 Oct 2021
If you have many variables in a MAT file, among which the structure of interest is also contained, there is no way to load only a single member of this structure into the workspace. For example, consider the following contents in the MAT file "sample.mat":
where "structureVar" contains the following:
Say we would like to load only "sVar1" into our workspace from "sample.mat". Currently, there is no way to do this. This is described in the "Limitations" section of the "matfile" documentation.
A workaround is to save the structure as an independent MAT file, and load members of the structure as needed. This is documented in "Variables" section of the "save" documentation:
For example, save "structureVar" as "structureVarMatFile.mat" when it is in the workspace using the following command:
Now, you can load only "sVar1" if needed, using the following command: