MATLAB: How to save a structure as .mat

arraymatMATLABsavesaving a structurestructure

I am trying to save a 1×6 structure named 'PIN' as a .mat file. When I attempt to use save(newfilename.mat,'PIN'), I get this error: Attempt to reference field of non-structure array.
How would I go about saving this structure as a .mat file with the specified name?

Best Answer

newfilename is a variable in your workspace when you define it as
newfilename = fullfile(newsubfolder, filenamei);
In your first argument to save(), you say "newfilename.mat", which attempts to access the mat field of object newfilename. Hence the error Attempt to reference field of non-structure array.
Try this syntax instead:
save(newfilename,'PIN');
Or if you really want to tag ".mat" on the end explicitly:
save([newfilename,'.mat'],'PIN');