Saving a structure array, not fields of it
44 次查看(过去 30 天)
显示 更早的评论
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
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:
采纳的回答
Les Beckham
2023-3-16
编辑:Les Beckham
2023-3-16
s1 = struct('a', 1, 'b', 2')
save('s1.mat', 's1') % don't use the '-struct' option
clearvars
whos
load('s1.mat')
whos
Another option:
s1 = struct('a', 1, 'b', 2')
save('s1.mat', '-struct', 's1')
s2 = load('s1.mat')
whos
isequal(s1, s2)
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Structures 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!