Save/Load an Object Array

54 views (last 30 days)
ScottPT303
ScottPT303 on 6 Aug 2015
Commented: ScottPT303 on 12 Aug 2015
I have a script that imports a large dataset from a .csv file and converts it to Matlab objects of a custom class. The resulting output of the script is an array of these objects. The script takes roughly 8 hours to complete. Once I've imported and structured the data into the object array I'd like to save it so I only have to do it once and can utilize it in other scripts by simply loading the saved object array. I believe I'm able to do the save part properly - after the script is finished I just call the save('filename', 'variable') function from the Command Window to save my object array from the workspace. Where I'm running into problems is when I'm trying to load it in another script. For example, lets say I saved the object array as 'testObjArray.mat'. If I load it and try to call an attribute from it using the following code:
thisObjArray = load('testObjArray.mat')
thisObjArray.name{1}
...I get a failure stating that I'm referencing a non-existent field 'name'. It seems like my problem is that when assigning thisObjArray with the load function I'm ending up with a struct rather than my object array. Is there a different way to load the file so that it remains an object array?
Thanks, Scott

Accepted Answer

Steven Lord
Steven Lord on 11 Aug 2015
Does the MATLAB session in which you're trying to LOAD the objects have access to the class definition file for your object? If you don't give it the "blueprints" for your house, it's not going to be able to build you a house (the object) -- it'll leave a pile of construction supplies on your lawn (the struct array.) It should have issued a warning in this scenario, though.
Here's a sample object. It's not on my path but is in my current directory, so I can instantiate it.
>> which -all myobjectForSaveLoadExample.m
c:\temp\sampleObject\myobjectForSaveLoadExample.m % myobjectForSaveLoadExample constructor
>> dbtype myobjectForSaveLoadExample.m
1 classdef myobjectForSaveLoadExample
2 properties
3 x;
4 end
5 methods
6 function obj = myobjectForSaveLoadExample(x)
7 obj.x = x;
8 end
9 end
10 end
>> y = myobjectForSaveLoadExample(1:10); % It exists!
Now let's go up a level, taking the class out of scope.
>> cd ..
>> which -all myobjectForSaveLoadExample
Not on MATLAB path % myobjectForSaveLoadExample constructor
>> save('mySampleObjectMatfile.mat', 'y');
If I clear the instance out of memory, then try to LOAD it from the MAT-file, I receive a warning.
>> clear all
>> which -all myobjectForSaveLoadExample
'myobjectForSaveLoadExample' not found.
>> load mySampleObjectMatfile
Warning: Variable 'y' originally saved as a myobjectForSaveLoadExample cannot be instantiated
as an object and will be read in as a uint32.
[The line break in the warning is mine, to make it fit in the Answer window.]
  3 Comments
Steven Lord
Steven Lord on 11 Aug 2015
In this example, y should be a struct array. It should have one field per variable stored in the MAT-file. If one of the variables in the MAT-file is named x, and x is a nonempty cell array or object for which curly brace indexing is defined, that should work.
Post the output of this command:
whos -file mySampleObjectMatfile.mat
Look for any variables listed in that output as fields of your struct array y.
ScottPT303
ScottPT303 on 12 Aug 2015
I've seen the error of my ways. I have it working now. Thank you very much for the help!

Sign in to comment.

More Answers (2)

Walter Roberson
Walter Roberson on 6 Aug 2015
The output of load() is always a struct, with one field for every variable saved in the file. You might need something like
thisObjArray.testObjArray.name{1}
  1 Comment
ScottPT303
ScottPT303 on 11 Aug 2015
Thanks Walter, but I can't seem to get this to work either. If the output of load() is always a struct is there some other function I should instead for objects? I see a loadobj() function, but I'm having a hard time understanding how it operates.

Sign in to comment.


James Tursa
James Tursa on 7 Aug 2015
To get your original variables to show up with the names they had originally (instead of field names in a struct), just use load without assigning the output to a variable. E.g.,
load('testObjArray.mat')
  2 Comments
ScottPT303
ScottPT303 on 11 Aug 2015
Thanks for the reply James. I think I need more help understanding though. I want to use the testObjArray.mat within my script. If I just use the load function without assigning it to a variable it seems that testObjArray gets loaded to the workspace, but within the script Matlab is not recognizing "testObjArray" as a variable. I feel like I need to assign it to a variable in my code, but when I do that it becomes a struct rather than an object class array. Do you have any other suggestions?
Steven Lord
Steven Lord on 11 Aug 2015
The variable is stored as one of the fields in your struct array. Try this example; I predict that the fields of the struct array data will be x and y, that data.x will be 17, and that data.y will be 23.
x = 17;
y = 23;
save('mymatfile.mat', 'x', 'y')
data = load('mymatfile.mat')

Sign in to comment.

Categories

Find more on Data Type Identification in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!