How can I load a single variable from a structure?

조회 수: 20 (최근 30일)
MathWorks Support Team
MathWorks Support Team 2019년 2월 26일
편집: MathWorks Support Team 2021년 10월 8일
How can I load only one member of a structure contained with other variables in a MAT file, without loading any other members of the structure?

채택된 답변

MathWorks Support Team
MathWorks Support Team 2021년 9월 2일
편집: MathWorks Support Team 2021년 10월 8일
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":
var1
var2
var3
structureVar
where "structureVar" contains the following:
sVar1
sVar2
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:
>> save('structureVarMatFile','-struct','structureVar');
Now, you can load only "sVar1" if needed, using the following command:
>> load('structureVarMatFile','sVar1');

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Workspace Variables and MAT Files에 대해 자세히 알아보기

태그

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by