Getting a field in a Class

I am trying to define a Class. When I create an object of the Class, I want to be able to get one of its properties. I have made it so when I have a 'get' method I can return the property when I use the syntax:
b = newobject(4); % My class only needs to take one int value
val = get(b, 'SomeVal') % this just returns a value
How do I make make my Class so that when I write:
val = b.SomeVal
I get the same result?
Also, this dynamic preview is really slow, is there a way to disable it?
Hmm, having trouble posting the class that I made. It's very poorand I am going to move on and come back to it later

2 comentarios

Walter Roberson
Walter Roberson el 29 de Ag. de 2011
Sorry, there is no way to disable the dynamic preview of the Answers system. The problem is known, and I do not know why it is taking so long to fix.
Daniel Shub
Daniel Shub el 31 de Ag. de 2011
Can you post some more code about your class. Is your class a subclass of hgsetget? How have you defined get and how have you defined the property SomeVal? What do you currently get when you do b.SomeVal?

Iniciar sesión para comentar.

Respuestas (1)

Lucas García
Lucas García el 29 de Ag. de 2011

0 votos

You should now be able to do:
b.get('SomeVal')
Or your access method for the SomeVal property has to be defined as public:
properties (GetAccess = public, SetAccess = private)
SomeVal
end
and at some point in the constructor method do:
function obj = newobject(value)
obj.SomeVal(end+1) = value; % end+1 to make it vector based
end
Then you will be able to do:
b.SomeVal

9 comentarios

Stagleton
Stagleton el 31 de Ag. de 2011
hmm, I have not been able to get this to work. Where should I make this properties declaration?
It also does not work to write
b.get('SomeVal')
I am probably not implementing these things correctly.
Can you clarify further?
Lucas García
Lucas García el 31 de Ag. de 2011
Question: which MATLAB version are you using?
Stagleton
Stagleton el 1 de Sept. de 2011
2010 B
Lucas García
Lucas García el 1 de Sept. de 2011
Here is a simple class of what your code could look like:
classdef newobject
properties (GetAccess = public, SetAccess = private)
SomeVal
end
methods
function obj = newobject(value)
obj.SomeVal = value;
end
end
end
Stagleton
Stagleton el 1 de Sept. de 2011
ok, I will try this out.
Here is one method I created for my class in order to get it to work (its a bit messy and there is probably a better way to do it):
function [ B ] = subsref( A,L )
%SUBSREF Summary of this function goes here
% Detailed explanation goes here
if (strcmp(L(1).type,'.') && strcmp(L(1).subs, 'Value'))
B=A.Value;
end
end
Stagleton
Stagleton el 1 de Sept. de 2011
this seems like a decent way of writing it that has worked for me:
function [ B ] = subsref( A,L )
%SUBSREF Summary of this function goes here
% Detailed explanation goes here
switch L(1).type
case '.'
switch L(1).subs
case 'Value'
B=A.Value;
end
end
end
Stagleton
Stagleton el 1 de Sept. de 2011
when I write:
properties (GetAccess = public, SetAccess=private)
outp.Value
end
the 'end' connects with the constructor at the top. I can't seem to get the 'properties' method/function to work
Daniel Shub
Daniel Shub el 1 de Sept. de 2011
Something is wrong. First, I am pretty sure outp.Value is not a valid property name and that property names cannot have "." in them. Second, you do not need a subsref method to do this. Third, your subsref method is so simple that it is going to cause all sorts of problems as you expand the class. I would suggest posting your entire simplified class file with all the methods.
Stagleton
Stagleton el 2 de Sept. de 2011
1st: yes, it seems I do not understand how to use the method 'properties'. I thought a property could be one of the fields in the structure that is created in the constructor....I suppose not?
2nd: Maybe not, but I am going through one of the courses on this site and I am trying to understand how to write classes in matlab. There may be another way of doing what I am trying to do without a subsref overriding method, and I am up for learning that as well.
3rd: I will post my class when I can. sry computer here doesnt have it and im heading on vacation.....
whoops fire alarm. see you

Iniciar sesión para comentar.

Categorías

Más información sobre Customize Object Indexing en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 29 de Ag. de 2011

Community Treasure Hunt

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

Start Hunting!

Translated by