Save/Load an Object Array
显示 更早的评论
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
采纳的回答
更多回答(2 个)
Walter Roberson
2015-8-6
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}
James Tursa
2015-8-7
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 个评论
ScottPT303
2015-8-11
Steven Lord
2015-8-11
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')
类别
在 帮助中心 和 File Exchange 中查找有关 Workspace Variables and MAT Files 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!