How to use a method to assign a structure to a class property?

18 views (last 30 days)
I'm new to Matlab, and am trying to create a class definition that will allow me to dynamically assign structured property variables to an object. I've defined the following class:
classdef WAVE
%UNTITLED5 Summary of this class goes here
% Detailed explanation goes here
properties
s %string structure
v %variable structure
end
methods
function WAVE = newVar(WAVE,NAME,VALUE)
% test if variable value is text or number
if ischar(VALUE)
% if text, then store as given variable name in the "s"
% structure
WAVE.s.(NAME) = VALUE;
else
% otherwise store as a given variable name in the "v"
% structure
WAVE.v.(NAME) = VALUE;
end
end
end
end
The idea here is to create the object (ie, object1 = WAVE) but then be able to assign a new property to it as such:
object1.newVar('name','My Object')
When this method is run, I'd like to have object1 updated so its "s" property (that for string variables) becomes a structure that has a new entry "name" which contains the data "My Object".
In this manner, the idea is to be able to get this object's name at a later point by running the following:
object1.s.name
Currently the above method works, but I have to reassign the output to the original object, such as the following:
object1 = object1.newVar('name','My Object')
Is there any way to implement this as a method so the method directly alters the object's property and does not need to be reassigned? I think there may be some issue with the use of "(NAME)" in the class method, since I can use "WAVE.v = VALUE" and it works just fine; however, this simply changes the "v" property to a single value and does not let it become a structure (my intended property type).
I should add that I can create an object from this class by the following command:
object1 = WAVE;
...and then properly assign structures to the "s" and "v" properties in the following manner:
field = 'name';
object1.s.(field) = 'My Object';
This ends up working as expected, but I cannot seem to get it to work when called within the methods block of the class definition.

Accepted Answer

per isakson
per isakson on 19 May 2013
Edited: per isakson on 6 Apr 2014
Try this script:
w = WAVE();
w.newVar('name','My Object');
field = 'name2';
w.s.(field) = 'My Object 2';
w.s.name
w.s.name2
It prints
ans =
My Object
ans =
My Object 2
where
classdef WAVE < handle
properties
s %string structure
v %variable structure
end
methods
function WAVE = newVar(WAVE,NAME,VALUE)
% test if variable value is text or number
if ischar(VALUE)
% if text, then store as given variable name in the "s"
% structure
WAVE.s.(NAME) = VALUE;
else
% otherwise store as a given variable name in the "v"
% structure
WAVE.v.(NAME) = VALUE;
end
end
end
end
.
In response to comment regarding value class:
  • Edit the first line of the class definition file; replace classdef WAVE<handle by classdef WAVE
  • Run this modified script
w = WAVE();
w = w.newVar( 'name', 'My Object' );
field = 'name2';
w = w.newVar( field, 'My Object 2' );
w.s.name
w.s.name2
which shall return
ans =
My Object
ans =
My Object 2
>>
  3 Comments
Olhado
Olhado on 3 Apr 2014
Is there a way to get this functionality in a value class? Thanks a ton for info
per isakson
per isakson on 6 Apr 2014
Depends on what exactly you mean by "this functionality". See the answer above.

Sign in to comment.

More Answers (1)

per isakson
per isakson on 19 May 2013
Edited: per isakson on 19 May 2013
"but I have to reassign the output to the original object" See Handle Classes Objects that share references with other objects.
  2 Comments
Christopher
Christopher on 19 May 2013
I've seen it, but have no clue how to implement it. How would I use that in the class definition I've described above?
When I try it in the methods such as:
WAVE.s.addprop(VALUE,NAME);
...I get an error stating "Attempt to reference field of non-structure array." I get the same error when I try to use it at the MatLab command prompt for an object defined by the same class. For instance:
object1 = WAVE;
object1.s.addprop('My Object','name');
per isakson
per isakson on 19 May 2013
Your questions gave me the impression that you want to add properties to an instance of a class at run time. The MathWorks has added the abstract class dynamicprops with the method addprops to support adding properties to instancies. I though that you was about the reinvent the this functionality and thus, I linked to the help on Dynamic Properties.
"have no clue how to implement it". You have to study the on-line help that I linked to.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!