MATLAB: How to save structured objects into mat-files

MATLABobjectssave

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

Best Answer

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