Effacer les filtres
Effacer les filtres

How to save -struct

76 vues (au cours des 30 derniers jours)
Rafael Monteiro
Rafael Monteiro le 22 Nov 2012
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.

Réponses (2)

Matt Fig
Matt Fig le 22 Nov 2012
Modifié(e) : Matt Fig le 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 le 11 Avr 2018
https://www.mathworks.com/help/matlab/ref/matlab.io.savevariablestoscript.html

Catégories

En savoir plus sur Structures dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by