Clear Filters
Clear Filters

How to save -struct

76 views (last 30 days)
Rafael Monteiro
Rafael Monteiro on 22 Nov 2012
Answered: Oleg Isaev on 11 Apr 2018
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.

Answers (2)

Matt Fig
Matt Fig on 22 Nov 2012
Edited: Matt Fig on 22 Nov 2012
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 on 11 Apr 2018
https://www.mathworks.com/help/matlab/ref/matlab.io.savevariablestoscript.html

Categories

Find more on Structures in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!