How do I save structured objects into mat-files?

I am dealing with large object in my research. I am not familiar with class oriented programing in MATLAB. However, I wanted to save the object into Mat-files. I tried two following ways. But neither way works out with separate message:
objMain.obj1 = function1(Variables);
objMain.obj2 = function1(Variables);
objMain.obj3 = function1(Variables);
objMain.obj4 = function1(Variables);
dir2Save = 'C:\Documents\Matlab\Research';
fname = ['mainTask' int2str(1)];
save([dir2Save '\' fname '.mat'], objMain);
*Message:* _Error using save
_Argument must contain a string._
save([dir2Save '\' fname '.mat'], '-struct', objMain);
*Message:* _Error using save
The argument to -STRUCT must be the name of a scalar structure variable._
Does any body have any ideas how to solve....
Thanks for your help.
Mahesh

댓글 수: 1

@Mahesh: I fixed your incorrect terminology: an M-file is a script or function, a mat file is a binary file used to store data.

댓글을 달려면 로그인하십시오.

 채택된 답변

the cyclist
the cyclist 2016년 7월 30일
편집: the cyclist 2016년 7월 30일

0 개 추천

Try
save([dir2Save '\' fname '.mat'], 'objMain')
You needed to enclose the variable name in quotes, as the second argument.

댓글 수: 5

save( fullfile(dir2Save, [fname '.mat']), 'objMain')
Mahesh
Mahesh 2016년 7월 30일
Thank you so much...
@Mahesh: it is better to use fullfile than a simple concatenation.
Cross-platform compatibility, or other reasons?
Mahesh
Mahesh 2016년 7월 30일
I am not sure about fullfile. Could you please help me how to use such. Since I am making file name as variable depending upon the parameters. Thanks for suggestions

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Workspace Variables and MAT Files에 대해 자세히 알아보기

제품

태그

질문:

2016년 7월 30일

댓글:

2016년 7월 31일

Community Treasure Hunt

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

Start Hunting!

Translated by