Saving a structure array, not fields of it

조회 수: 5 (최근 30일)
Snoopy
Snoopy 2023년 3월 16일
댓글: Snoopy 2023년 3월 16일
I have a structure array called s1 in the Workspace. I want to save it as it is so that when I load it, I want to see just s1 in the Workspace, not what is in s1. That is, if I use
save('s1.mat','-struct','s1');
MATLAB saves the content of s1 to s1.mat and when I load s1.mat, the content gets loaded to the Workspace. But I do not want this. I want just s1 to appear in the Workspace.
  댓글 수: 2
Stephen23
Stephen23 2023년 3월 16일
편집: Stephen23 2023년 3월 16일
Although you could simply skip the -struct option, the robust approach is to SAVE using -struct exactly as you do now and then LOAD into an output variable:
save('s1.mat','-struct','s1');
s1 = load('s1.mat')
This is more robust than making variables pop into the workspace:
Snoopy
Snoopy 2023년 3월 16일
Thanks a lot.

댓글을 달려면 로그인하십시오.

채택된 답변

Les Beckham
Les Beckham 2023년 3월 16일
편집: Les Beckham 2023년 3월 16일
s1 = struct('a', 1, 'b', 2')
s1 = struct with fields:
a: 1 b: 2
save('s1.mat', 's1') % don't use the '-struct' option
clearvars
whos
load('s1.mat')
whos
Name Size Bytes Class Attributes s1 1x1 352 struct
Another option:
s1 = struct('a', 1, 'b', 2')
s1 = struct with fields:
a: 1 b: 2
save('s1.mat', '-struct', 's1')
s2 = load('s1.mat')
s2 = struct with fields:
a: 1 b: 2
whos
Name Size Bytes Class Attributes s1 1x1 352 struct s2 1x1 352 struct
isequal(s1, s2)
ans = logical
1

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by