Property set method for structure-like property
17 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hello, I'm implementing a class (used to control an instrument through a USB interface).
Due to the structure of the instrument, I'd like to store properies as structures.
I wonder if there is a good way to implement property set method which would only update the field that was changed.
Here is what I have know:
classdef Driver < handle
properties
input; % Structure array with input settings
% - range
% - ac (coupling: AC/DC)
% - impedance_50ohm (50/1e6)
output; % Structure array with output settings
% - on
% - range (V)
% - amplitude (V, peak-to-peak)
% - mixer (channel)
end
methods
% Getters
function input = get.input(obj)
range = obj.get('input_range');
ac = obj.get('input_ac');
impedance_50ohm = obj.get('input_impedance_50');
input = struct('range', range, 'ac', ac, 'impedance_50ohm', impedance_50ohm);
end
function output = get.output(obj)
on = obj.get('output_on');
range = obj.get('output_range');
amplitude = obj.get('output_amplitude');
mixer = obj.get('output_mixer');
output = struct('on', on, 'range', range, 'amplitude', amplitude, 'mixer', mixer);
end
% Setters
function set.input(obj, input)
obj.set('input_range', input.range);
obj.set('input_ac', input.ac);
obj.set('input_impedance_50', input.impedance_50ohm);
end
function set.output(obj, output)
obj.set('output_on', output.on);
obj.set('output_range', output.range);
obj.set('output_amplitude', output.amplitude);
obj.set('output_mixer', output.mixer);
end
end
end
However, I do not want to update all the field each time one field is changed. I want to do something like (obviously this not a proper code):
classdef Driver < handle
properties
input; % Structure array with input settings
% - range
% - ac (coupling: AC/DC)
% - impedance_50ohm (50/1e6)
output; % Structure array with output settings
% - on
% - range (V)
% - amplitude (V, peak-to-peak)
% - mixer (channel)
end
methods
% Getters
function range = get.input.range(obj)
range = obj.get('input_range');
end
function ac = get.input.ac(obj)
ac = obj.get('input_ac');
end
%etc...
% Setters
function set.input.range(obj, input.range)
obj.set('input_range', input.range);
end
function set.input.ac(obj, input.ac)
obj.set('input_ac', input.ac);
end
%etc...
end
end
I understand that I could just make them separate properties, but this would be not a pleasing solution.
classdef Driver < handle
properties
%Input Parameters
input_range; % Input ranges of all signal inputs
input_ac; % Input couplings of all signal inputs
input_impedance_50; % Input impedances of all signal inputs
%Output Parameters
output_on; % Output status of all signal outputs
output_range; % Output ranges of all signal outputs
output_amplitude; % Output amplitudes of all signal outputs
output_mixer; % Output mixer channels of all signal outputs
end
end
Thank you.
2 Kommentare
Mario Malic
am 25 Apr. 2024
I think you should keep implement a class for Input and Output, each containing appropriate properties. If your device needs a structure as a way to communicate, you can write a method that will create it from the properties and their values.
Antworten (0)
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!