saving struct empty give me error

4 ビュー (過去 30 日間)
Luca Re
Luca Re 2024 年 10 月 9 日
回答済み: Animesh 2024 年 10 月 9 日
hi, it's possibile to save empty struct? How can i do it?
A=app.portFolio_struct;
On=logical(str2double(A.List(:,2)));
A.List=A.List(On,:);
A.List=[];
B=A.List; %memorizzo la struttura ma senza il setting !(e' caricato con un file a parte nel MPV_Serafini_PortfolioManager e poi messo in questa struttura )
save(A.portFolio_setting.tslist,'-struct','B');
Error using save
The argument to -STRUCT must be the name of a scalar structure variable.

採用された回答

Stephen23
Stephen23 2024 年 10 月 9 日
編集済み: Stephen23 2024 年 10 月 9 日
"hi, it's possibile to save empty struct? How can i do it?"
Of course, just give the variable name exactly like you would any other variable:
S = struct('x',{},'y',{})
S = 0x0 empty struct array with fields: x y
save('mytest.mat','S') % no error
checking:
T = load('mytest.mat').S
T = 0x0 empty struct array with fields: x y
What will not work is the -struct option, which (as the error clearly states) only works for scalar structures. It also behaves differently, by saving each field as separate variables in the MAT file.
But there is absolutely no reason why you cannot save a structure of any size as its own variable.

その他の回答 (1 件)

Animesh
Animesh 2024 年 10 月 9 日
The error message suggests that the variable you're trying to save with the "-struct" option is not a scalar structure. In this case, the variable "B" is causing the issue. To save an empty struct, ensure that you're saving a structure variable, even if it is empty. Here's how you can modify your code:
B = struct('List', A.List);
save(A.portFolio_setting.tslist, 'B');
In here, we create "B" as a structure with an empty field named "List".

カテゴリ

Help Center および File ExchangePrinting and Saving についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by