Saving to .mat without additional struct.

43 ビュー (過去 30 日間)
Gabriel ROUX
Gabriel ROUX 2022 年 7 月 8 日
コメント済み: Steven Lord 2022 年 7 月 8 日
I have that structure
test =
struct with fields:
F0: 243000
I: [3×3 double]
I_1: [3×3 double]
I_X: 5550000
I_XZ: 33000
%[...]
m: 160000
that i want to save 'as it is'
uisave('test')
I save it as name ('test2'), i load it back and i go this
aircraftdata = load('test2.mat')
aircraftdata =
struct with fields:
test: [1×1 struct]
What i want is a structure like this :
aircraftdata =
struct with fields:
F0: 243000
I: [3×3 double]
I_1: [3×3 double]
I_X: 5550000
I_XZ: 33000
%[...]
m: 160000
The whole point is saving the structure untouched and load it with a new name

採用された回答

Steven Lord
Steven Lord 2022 年 7 月 8 日
When you save the struct array, specify the '-struct' option. This will save each of the struct fields separately in the MAT-file. When you call load with an output argument, each of those separate pieces of data from the MAT-file will become fields of the new struct.
S = struct('theAnswer', 42, 'magicMatrix', magic(5), 'nephews', ["Huey", "Dewey", "Louie"])
S = struct with fields:
theAnswer: 42 magicMatrix: [5×5 double] nephews: ["Huey" "Dewey" "Louie"]
cd(tempdir)
name = 'temporaryMatfile.mat';
save(name, '-struct', 'S')
whos('-file', name) % Individual variables
Name Size Bytes Class Attributes magicMatrix 5x5 200 double nephews - 258 string theAnswer 1x1 8 double
data = load(name)
data = struct with fields:
theAnswer: 42 magicMatrix: [5×5 double] nephews: ["Huey" "Dewey" "Louie"]
Compare with:
name2 = 'otherTemporaryMatfile.mat';
save(name2, 'S')
whos('-file', name2) % The struct was stored intact
Name Size Bytes Class Attributes S 1x1 970 struct
data = load(name2)
data = struct with fields:
S: [1×1 struct]
data.S
ans = struct with fields:
theAnswer: 42 magicMatrix: [5×5 double] nephews: ["Huey" "Dewey" "Louie"]
  2 件のコメント
Gabriel ROUX
Gabriel ROUX 2022 年 7 月 8 日
編集済み: Gabriel ROUX 2022 年 7 月 8 日
Thank you very much,
It works fine, but i doesn't suits my needs. I'm working with "uisave()" because it is more user-friendly with AppDesigner. Does uisave allows '-struct' options ? Another issue is that with 'uisave we have no return about what is the filename chosen by the user and what is it's path, so i cannot apply further processing once he's done.
Steven Lord
Steven Lord 2022 年 7 月 8 日
I don't believe uisave offers the '-struct' option. You could try using a different tool, like uiputfile, to select the file to which you want to save. Once you have the file name and path information, use fullfile to construct the fully qualified name and pass name that into save.

サインインしてコメントする。

その他の回答 (2 件)

Steve Eddins
Steve Eddins 2022 年 7 月 8 日
Try this:
s = load("test2.mat");
aircraftdata = s.test;

Gabriel ROUX
Gabriel ROUX 2022 年 7 月 8 日
I have it https://fr.mathworks.com/matlabcentral/answers/330397-how-can-i-save-some-fields-of-a-struct-using-uisave-function#answer_259211

カテゴリ

Help Center および File ExchangeDevelop Apps Using App Designer についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by