How to save data structures? (Currently get nonsense files)

3 ビュー (過去 30 日間)
Jonathan
Jonathan 2014 年 7 月 25 日
回答済み: Jonathan 2014 年 7 月 28 日
Hi everyone,
I'm trying to save data structures (from a rather complex calculation set) so I can later call up that data and manipulate it.
To simplify, I attempt
%Create some data
S.a = 1
S.b = 2
%Save Data
save('MyData.m','S')
Which runs successfully, but the saved file is nonsense that matlab cannot handle:
MATLAB 5.0 MAT-file, Platform: MACI64, Created on: Fri Jul 25 14:51:40 2014 IM#xãc``0b6 æÒ À
å3Â1#C"f³èÉ$xãc``0b6 æÒ À
å3Â1#CfÒL@øË
Have not found any solutions on google and trying other file formats (eg. .dat) hasn't made a difference
Any ideas what is going on or how to fix would be gratefully received!
Thanks.
Edit: running R2012b
Edit: Should also clarify that the output fails to work with the "load" command either
load('Test_struct_save.m')
Error using load
Number of columns on line 2 of ASCII file
[stuff]Project/matlab_code/Test_struct_save.m
must be the same as previous lines.
  1 件のコメント
Wayne King
Wayne King 2014 年 7 月 25 日
Can you please include information on what version of MATLAB you are running on the mac64?

サインインしてコメントする。

採用された回答

Robert Cumming
Robert Cumming 2014 年 7 月 25 日
Save it as a .mat file instead of a .m file.
save('MyData.mat','S')

その他の回答 (2 件)

Geoff Hayes
Geoff Hayes 2014 年 7 月 25 日
The MATLAB function save is saving the structure variable S to a binary file (MAT-file) which explains the "nonsense" that you observe in the file when opened in the Editor. Since the format is not ascii, the file is not meant to be "read" in this manner. (And structs are not supported data types for writing to ascii file - try *save('MyData.m','S','-ascii') to see this).
Use the load function to load the data from file and back into the workspace.
  2 件のコメント
Jonathan
Jonathan 2014 年 7 月 25 日
Thanks for the info.
As now clarified in the question above, load command does not work with the save file either.
What is the correct process for saving data structures for retrieval?
Matlab online help suggests that save function should handle this fine?
http://www.mathworks.co.uk/help/matlab/import_export/save-structure-fields-as-separate-variables.html (Article discussing how to modify save command to store structure variables individually rather than the whole structure - implying that default save should work fine for structures(?).)
Geoff Hayes
Geoff Hayes 2014 年 7 月 25 日
As Robert indicates in his answer, save the file with an extension of mat
S.a = 1
S.b = 2
save('MyData.mat','S');
And then just load the data from file using load
clear S; % just to remove from the workspace
load('Mydata.mat');
Alternatively, if you choose not to change the extension of the file, you can force load to treat the file as if it were a mat file regardless of its extension with
load('MyData.m','-mat');
See load for details.

サインインしてコメントする。


Jonathan
Jonathan 2014 年 7 月 28 日
Great! Thanks both.
.mat works perfectly.

カテゴリ

Help Center および File ExchangeVariables についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by