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

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.

 採用された回答

per isakson
per isakson 2013 年 5 月 19 日
編集済み: per isakson 2014 年 4 月 6 日

0 投票

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 件のコメント

Christopher
Christopher 2013 年 5 月 19 日
編集済み: Christopher 2013 年 5 月 19 日
Beautiful! It seems I was on the right track, but didnt use an appropriate inheritance for the class definition. It's working well now.
I also found in messing around that you can simply run the following at the command prompt on a currently-defined object:
wave1 = WAVE
wave1.s.name = 'My Object'
The problem with this is you cannot do any verification on the variable and its value. I'd like eventually to be able to run a check to ensure only strings are in the 's' structure, and only variables are in the 'v' structure, etc.
Thanks a bunch! This puts me on the right track.
Olhado
Olhado 2014 年 4 月 3 日
Is there a way to get this functionality in a value class? Thanks a ton for info
per isakson
per isakson 2014 年 4 月 6 日
Depends on what exactly you mean by "this functionality". See the answer above.

サインインしてコメントする。

その他の回答 (1 件)

per isakson
per isakson 2013 年 5 月 19 日
編集済み: per isakson 2013 年 5 月 19 日

0 投票

"but I have to reassign the output to the original object" See Handle Classes Objects that share references with other objects.

2 件のコメント

Christopher
Christopher 2013 年 5 月 19 日
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 2013 年 5 月 19 日
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.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File Exchange で Construct and Work with Object Arrays についてさらに検索

質問済み:

2013 年 5 月 19 日

編集済み:

2014 年 4 月 6 日

Community Treasure Hunt

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

Start Hunting!

Translated by