How to set class property without calling setter function

10 ビュー (過去 30 日間)
amoney
amoney 2016 年 1 月 23 日
回答済み: grajcjan 2018 年 10 月 25 日
I'm looking to achieve the following code structure in my class definition for several different properties to make an instrument driver.
function set.prop(obj, value)
'send command to instrument'
obj.prop = value
end
function value = get.prop(obj)
value = obj.prop
end
funciton readprop(obj)
obj.prop = 'function to read value from instrument'
end
With this code format I can store the value in property and recall it by obj.prop without having to reread the instrument each time. However, when I call obj.readprop, this method calls the setter method. How do I get around this in the simplest way?

回答 (3 件)

Krishna Chaitanya Duggineni
Krishna Chaitanya Duggineni 2016 年 1 月 27 日
Let me know if my understanding of your question is incorrect.
'readprop' member function of a class is used to read a property from the instrument and then calls 'setter' to set the property value with the current reading. You would like to avoid calling the setter.
This requirement needs you to modify your code for 'readprop' function. For 'readprop' not to call setter function, you can add another boolean argument (ex: "enable") which is by default 1. Manipulate 'readprop' to call the 'setter' only if this argument is true(or the vice versa of this).
Member function 'readprop' will look like this
function readprop(obj,enable)
if nargin < 2
enable = true;
%get property value from instrument
end
if(enable)
%call setter
else
% Do not call setter
end
end
  1 件のコメント
amoney
amoney 2016 年 1 月 27 日
編集済み: amoney 2016 年 1 月 27 日
Perhaps what I really need is a getter that has two different modes:
function value get.prop(obj, enable)
if nargin < 2
value = obj.prop
else
obj.prop = external query
value = obj.prop
end
end
will this work?

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


Krishna Chaitanya Duggineni
Krishna Chaitanya Duggineni 2016 年 1 月 29 日
Getters must have only one argument. So, passing 'enable' as an input and getting 'value' as output might be a trouble. Moreover, It will be better if the getter does not alter the value of property.
  1 件のコメント
amoney
amoney 2016 年 1 月 29 日
Ah, yes, this makes sense. However, your suggestion has given me the idea of how to solve the problem. It's the setter that I'll but an indicator to read the instrument.
function get.prop(obj, newvalue)
if strcmp(newvalue, 'read')
newvalue='read inst func'
else
'set inst func'
end
obj.prop = newvalue
end
Thanks for your help!

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


grajcjan
grajcjan 2018 年 10 月 25 日
Hi, I'm trying to solve a similar problem with set and get. Do you know how I can do something similar, using only get & set function? When I'm connected to a device, I want to read the set values directly from the device, but when I'm disconnected, I want to see the last set value and if I reconnect, I would like to set the device to the last set value.
function set.Prop(visaObject, Prop)
if strcmp(visaObject.Status,'open')
fprintf(visaObject, ['command',Prop]);
end
visaObject.Prop = Prop;
end
function Prop = get.Prop(visaObject)
if strcmp(visaObject.Status,'closed')
Prop = visaObject.Prop;
else
Prop = query(visaObject,command);
visaObject.Prop = Prop;
end
end

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by