How to save -struct

76 次查看(过去 30 天)
Rafael Monteiro
Rafael Monteiro 2012-11-22
回答: Oleg Isaev 2018-4-11
Hey, I'm having trouble saving this struct.
I've tried
save('datafile.mat','mati','-struct','materials');
when my code is something like:
mati = 1;
materials(mati).name = input('','s');
disp(strcat('Introduce the properties of the material',32,materials(mati).name,32,'in SI units.'));
disp('Density [Kg/m3]');
materials(mati).properties(1) = input('');
disp('fusion temp [K]');
materials(mati).properties(2) = input('');
disp('stress blab la [MPa]');
materials(mati).properties(3) = input('');
disp('Tensão de ruptura [MPa]');
But I get this error:
Error using save
The argument to -STRUCT must be the name of a scalar structure variable.

回答(2 个)

Matt Fig
Matt Fig 2012-11-22
编辑:Matt Fig 2012-11-22
According to the help for SAVE, you need to call like this:
save(filename, '-struct', structName, fieldNames)
Note that -struct is the second argument, not the third... But I wonder if all you need is this:
save('datafile.mat','materials');
For example:
clear all % Start with a clean slate
S.name = 'Iron'; % Build a structure
S.density = 2700;
S.CS = 'Fe';
save('myIron.mat','S') % Save the structure
clear all % Clear to make sure struct is gone.
load('myIron.mat') % Load it. (Also, X=load(...) is preferred)
whos % See if we have our structure back...
Name Size Bytes Class Attributes
S 1x1 548 struct

Oleg Isaev
Oleg Isaev 2018-4-11
https://www.mathworks.com/help/matlab/ref/matlab.io.savevariablestoscript.html

类别

Help CenterFile Exchange 中查找有关 Structures 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by