特定のプロパティが設定されているときにリスナーを使用してオブジェクトを変更するクラス定義を作成しています。そのようです:
classdef MyObject < handle
properties (SetObservable)
orientation = 'h'; % h = horizontal, v = vertical, o = other
length
width
end
methods
% Constructor
function mo = MyObject(o)
mo.orientation = o;
addlistener(mo, 'orientation', 'PreSet', @mo.changeOrientation);
end
% Change Orientation Listener
function changeOrientation(mo, src, evnt)
celldisp(src);
celldisp(evnt);
% I want a way to access newor here
if mo.orientation == 'h' && newor == 'o'
tempw = mo.width
mo.width = mo.length
mo.length = tempw;
end
end
% Setter
function set.orientation(mo, newor)
mo.orientation = newor;
end
end
end
向きを設定するときに変数 newor を使用できるようにしたい。新しい方向変数を changeOrientation メソッドに渡すにはどうすればよいですか?
Matlab はプロパティ (長さと幅) が初期化されていない可能性があると文句を言うためchangeOrientation、メソッドに内容を移動することは避けたいと思います。set.orientation
編集の長さは方向に依存しません。向きが変わったら、長さと幅を交換するだけです。それ以外の場合、ユーザーは長さまたは幅を任意の正の値に設定できる必要があります。