Save a Matlab Property to a mat file - matlab

I have a class with a struct property called myStruct:
properties
myStruct;
end
I would like to save the struct to a .mat file. I tried:
save -append registers.mat myStruct;
But it gives the error:
Variable 'myStruct' not found.
I have different functions for updating the struct, like:
function configureChannel( obj, Channel )
myStruct.Channel = Channel;
save -append registers.mat myStruct;
end
function updateConfiguration( obj, dataPath )
myStruct.EN = 1;
save -append registers.mat myStruct;
end
These are all functions of the same class.

I think that the main problem in this code is how you access myStruct from your functions. Take a look at the following piece of code:
function configureChannel( obj, Channel )
myStruct.Channel = Channel;
...
end
What you intended for it to do was to assign Channel into the Channel field of the current object's myStruct's Channel property. What it actually does is equivalent to calling:
myStruct = struct('Channel', Channel);
That is, you create a new local variable in the current workspace/scope of configureChannel, without actually updating the property of the object, obj.
So the first correction you need to make is how you access myStruct in your setter:
obj.myStruct.Channel = Channel;
Next is the issue of saving.
When dumping a copy of an object's field to a file, I would expect save to treat your arbitrary object as a struct, and so struct saving conventions should apply:
'-struct',structName | Store the fields of the scalar structure specified by structName as individual variables in the file. For example, save('filename.mat','-struct','S') saves the scalar structure, S.
Thus, I would expect the following to work disclaimer: I didn't test it:
save('filename.mat','-struct','obj.myStruct');
Other alternatives in case the above fails:
save('filename.mat','obj.myStruct'); % Alternative #1
tmp = obj.myStruct; save('filename.mat','tmp'); % Alternative #2
One last thing - as you may have noticed, I'm using the function form of save, because I consider it to be more maintainable/readable/fool-proof:
save(filename,variables,'-append');

Related

Using Setters and Getters from a Class

I have an class called TestData that houses a private property called data, which I define as a numeric array. Its goal is to take in data from various other .m files and extract the data and place it in the specified format (numeric array. So, a random_data.m file that I am currently working with spits out a 1X13 double array called Avec. I generate and instance of the class myTestData = TestData(); however, because the member variables are private I need to have getData and setData functions. The only idea I have is to pass Avec into getData (e.g. myTestData.getData(Avec)) and then store it in a temporary array that could then be used by setData to write into data; but I feel like this is bad practice as that array would need to be public. Also, would it make sense to pass the entire array in or should I pass each element in individually. I would like to have it check the data to make sure that it is in proper format, as well.
I guess in general my concept of how the class works in MATLAB may be flawed.
Thanks for your help in advance and if there is anything else that I can provide, please let me know. Below is some code. The first snippet above the class is from the separate .m file.
%Write data to file using the TestData Object
Avec = [some 1X13 double array]
myTestData=TestData; % Generate an instance of the object
myTestData.getData(Avec);
classdef TestData
properties (Access = private)
metaData % stores meta data in Nx2 array
data % stores data in PxQ array
colLabels % labels columns
colUnits % provides units
metaPrint % used to print metaData
temp % debugging purposes only
end
methods
%****************************************************************************%
%Function: TestData
%Purpose: Constructor used to allocate data arrays
%****************************************************************************%
function this = TestData() %constructor
this.metaData = [];
this.data = [];
this.colLabels = [];
this.colUnits = [];
this.metaPrint = [];
this.temp = [];
end %TestData()
%%
%****************************************************************************%
%Function:
%Inputs:
%Purpose:
%****************************************************************************%
function this = getData(this, someArray)
????
end %getData
I think you're misunderstanding the idea of getters and setters. A get function is designed to take something from the object and return it, while a setter is designed to put something into a property of the object. You would want something like:
function data = getData(this)
data = this.data;
% Do any processing to put data into a different format for output
end
function this = setData(this, data)
% Check the data input to make sure it is the right format, etc.
this.data = data;
end
You may also want to design setData to take different types of arguments, like a file name that it can use to load the matrix itself. You could also design your constructor to accept a matrix or file name and initialize data as well.
Also, as Cris alludes to in his comment, if the reason you were making data private was to control how the user could access and modify it, it's enough to just have getters and setters. You can make data public and your property access methods will still be called when accessing the object like Avec = myTestData.data or myTestData.data = Avec.

How to load a property on demand?

I want to load a property on demand but i cant get it working.
I have a class with a transient property foo. So the property is not stored when the object is saved. When I use a method that is calling the property 'foo', I want that the value of foo is loaded from a separate mat file and stored into the object as long as it is in workspace.
I tried something with get and set methods but cant get it working. Is this possible? Or do I always add a line of code that is loading the property? The following code does not do what I want but it gives an indication what i tried.
In addition, the code below keeps loading the foo.mat file when the property is used. I want to load foo.mat only one time and store it as a property and retrieve the data from there instead of loading. The reason for my question is that the foo property is rather large i.e. a class with many properties in itself. I only want to load it when it is needed and dont want to store it in foobar class itself.
classdef foobar
properties(Transient = true)
foo
end
methods
function value = get.foo(obj)
if isempty(obj.foo)
value = load('foo.mat');
disp('load foo.mat');
end
end
function obj = set.foo(obj,value)
obj.foo = value;
end
end
end
You have two major problems here:
In your get.foo method, once you load the value, you never update the value of foo in the object, so it remains empty.
Even if you tried to update foo in your get.foo method, it would still be empty in the original object because your foobar class is a value class. Methods that modify a value class object have to return the modified object as an output, because they are essentially modifying a copy of the object. A set method of a value class returns a modified object that is used to overwrite the original object, but get methods don't return modified objects (since they aren't generally expected to modify them). To get around that limitation, you'll need the reference-like behavior of a handle class (here's a related question you may want to take a look at for more background).
So, in order to get the behavior you want you'd have to implement foobar as a subclass of the handle class and update the foo field when you first load it:
classdef foobar < handle % Inherit from handle class
properties(Transient = true)
foo
end
methods
function value = get.foo(obj)
if isempty(obj.foo)
value = load('foo.mat');
disp('load foo.mat');
obj.foo = value; % Update foo
end
value = obj.foo; % Return current foo value
end
function set.foo(obj, value) % Return value is unnecessary for handle class
obj.foo = value;
end
end
end
This should now give you the behavior you want (i.e. foo is only loaded when it is first accessed).
Note: Any method that invokes get.foo will initialize foo. One method you may overlook, because it's created by default for a class, is the disp method. The default display for a class object will show the class name followed by a list of non-hidden public properties and their values. Note what happens when I create an object of class foobar from above with and without a semicolon:
>> f = foobar; % Display is suppressed
>> f = foobar
f =
load foo.mat % foo gets initialized...
foobar with properties:
foo: [1×1 struct] % ...because its value is displayed here
If you want to avoid this, you can overload the disp function for your foobar object so that displaying the object doesn't access (and thus initialize) foo. For example, you can add this method to the above foobar class:
function disp(obj)
disp(' foobar object');
end
Now you won't initialize foo when displaying the object:
>> f = foobar
f =
foobar object % foo not yet initialized
>> a = f.foo;
load foo.mat % foo initialized because we accessed it

Force conversion of struct to object in MATLAB loadobj function

I am working with a custom defined class I called "PathObj_Standard". I want to make sure that when I load this class, if the property CalcDate was saved as a cell array it is converted to a standard array. However, I changed the class definition some time ago, so when I use the loadobj function, I am getting a struct instead of an object. The original code I'm using has a lot more properties, so I'd rather not create a new object by assigning property by property from the struct to a new object. Furthermore, I'm also hesitant to change the constructor to accept a struct as an argument.
I tried using the class function inside loadobj, but I am getting a Cannot redefine class 'PathObj_Standard' without a call to 'clear classes' error. Isn't this function supposed to force conversion of a struct to an object? Why doesn't it work within the loadobj function?
classdef PathObj_Standard < handle
properties (SetAccess = protected)
CalcDate;
Name;
end
methods(Static)
function obj=loadobj(s)
if isstruct(s)
obj=class(s,'PathObj_Standard');
else
obj=s;
end
if not(isempty(obj.CalcDate)) && iscell(obj.CalcDate)
obj.CalcDate=cell2mat(obj.CalcDate);
end
end
end
methods
function obj=PathObj_Standard(Name,CalcDate)
obj.Name=Name;
obj.CalcDate=CalcDate;
end
end
The issue is that calling class attempts to create a class which you can't do from within your loadobj. You'll want to call the actual constructor
Also in my experience, the easiest way to construct a class from a struct is to inherit from hgsetget rather than handle as that automatically has the set and get methods of MATLAB's graphics objects and these methods can accept property/values in the form of a struct. In newer versions of MATLAB, you can also use the SetGet mixin
classdef PathObj_Standard < hgsetget
If you do this, you could change your loadobj method to be something like
function obj = loadobj(s)
% Update the input struct as needed
if isfield(s, 'CalcDate') && ~isempty(s.CalcDate) && iscell(s.CalcDate)
s.CalcDate = cell2mat(s.CalcDate);
end
% Call the default constructor
obj = PathObj_Standard();
% Update all properties that were supplied to loadobj
set(obj, s)
end

Matlab: Getter runs before Constructor?

Given an object with custom get-methods for some properties, does Matlab execute some of the code (the getter) before the class constructor is executed?
Even if i set the default of a property to empty, and have a getter (!) open an io connection to a file, when I step through the debugger, even on the first line the object is already defined as file.io (with a filepath that corresponds the information available to the object before the constructor ran). How can this be, and whats the reasoning behind this implementation?
Edit: A breakpoint in the get method does not halt the debugger, so I'm not sure wether it is actually executed or not.
Edit 2: It seems like the getter is executed after the constructor is entered, after the debugger halts in the first line, before the first line is executed. No halt at breakpoint within get method though...
As per request, some code:
classdef Cat < handle
properties
filename
poop = []; % my data matrix the cat is there to produce/manage
end
methods
function obj = Cat(config)
obj.filename = config.FILENAME; % Halt debugger in this line
end
function value = get.poop(obj)
obj.poop = matfile(obj.filename)
value = obj.poop.ingredients; % 'ingeredients' being the name of the variable in poopfile.mat
end
end
end
To debug, I call
myCat = Cat(config)
from a different script. Workspace is cleared and path is rehashed.
When the debugger halts, obj.poop is not [], but is already a reference to some undefined file, and the reference to the linked file obj.poop.Source is empty, which is obvious, as obj.filename has not been set yet.
Test setup:
With a slightly modified class Cat.m:
classdef Cat < handle
properties
filename
poop = [];
end
methods
function obj = Cat(config)
display('In constructor.');
obj.filename = config.FILENAME;
end
function value = get.poop(obj)
display('In poop getter.');
obj.poop = matfile(obj.filename);
value = obj.poop.ingredients;
end
end
end
to display the execution order of the class methods, and the test.m script:
ingredients = 1:100;
save('a', 'ingredients');
config.FILENAME = 'a.mat';
myCat = Cat(config)
I got the following result:
>>test
In constructor.
myCat =
In poop getter.
Cat handle
Properties:
filename: 'a.mat'
poop: [1x100 double]
Methods, Events, Superclasses
Please note that the first assignment in the getter method was ended with semicolon (while in the original code was not).
In conclusion:
The get.poop() method is called after the constructor, as expected. This was tested on MATLAB R2012a, but I strongly believe that this is not a matter of version.
The reason for which get.poop() method is called is because the assignment myCat = Cat(config) is not ended with a semicolon ;.
Rationale:
The default behavior for assignments not ended with semicolon is to display the result of assignment. Displaying an object means, among other things, displaying the values of public properties. To get the value of the public property poop, get.poop() is called; that explains the getter call. Once the statement is changed to myCat = Cat(config);, the getter is not called anymore, because the result of assignment is not displayed anymore.
Later note:
Please note also that every request for display of the object will call the getter. So, yes, the getter might be called while the constructor is still halted by the debugger, because you inspect the poop member.

Accessing elements of a struct instance variable in Objective-C using the getter

I have an instance variable which is a struct, for example:
struct Data {
UInt32 i;
UInt32 arr[1];
};
And a property is defined in my class:
#property struct Data data; // and the corresponding #synthesize in the imp file
Now, I am aware that changing the values of i and arr through the getter of data is conceptually wrong, since I will be accessing the copy of data returned by the getter (the correct way is accessing it using self->data).
However some general Objective-C questions arise regarding the following lines:
self.data.i = 1; // produces compile error
self.data.arr[0] = 1; // compiles ok
First, why does the first line produces a compile error, and the 2nd line does not?
Second, if the dot syntax in the above line (self.data) is just a syntactic sugar to [self data], why do I get 2 different (although similar) compile errors?
self.data.i = 1; // error: Expression is not assignable
[self data].i = 1; // error: Semantic Issue: Assigning to 'readonly' return result of an objective-c message not allowed
Actually, structs are passed by value in C (and Objective C). That means that your property actually returns a read only copy (rvalue) of the internal "Data" type. The assignment is to the temporary returned copy, which the compiler (rightfully) flags as a bit suspect.
The second line that compiles correctly does so since self.data.arr returns a read only UInt32*, but when you index it with [0] and write to that, you're not writing to the pointer, you're writing to the memory that it points to which is allowed.