String based interface for inter-class communication

2 views (last 30 days)
Hello to all,
I have a question about accessing variables in classes by using strings. I have classes for several measuring instruments and each has several signals. I also have GUI where I choose instruments and signals in listboxes. Property of each instrument and signal is a string name, e.g. 'Meteostation' (instr.) or 'Rain rate' (signal).
I would like to collect the available instruments and their available signals and display it in the listboxes. Then, by selection of a particular instr. and its signal I want to update plot and access the signal for modifications.
Now I get strings by clicking on the listbox and access the signal in classes by passing the string to methods and inside the methods I use switch command to access demanded signal. I would like to define the signal name only at one place - in the signal class - to keep the code readable. This string name in the signal class should be the same which is displayed in listbox - it walks through two or three class layers.
Do you have any idea how to make this more elegant? Something like translator from strings to variables? Or the way with switch/case is OK?
Thanks in advance for any advices,
Petr
  2 Comments
Guillaume
Guillaume on 14 Apr 2015
Could you give a clearer description of your classes structure (an example classdef for the instrument and signal classes)? At the moment, I'm very unclear on what you're asking.
It would also help to use the proper terminology. Classes in matlab have properties and methods. You seem to be using 'property' to refer to something else and 'variables' to refer to properties?
Petr Dvorak
Petr Dvorak on 14 Apr 2015
Dear Guillaume, thank you for your response and sorry for my unclear explanation.
I have a GUI with listboxes - one for instruments (this Meteostation is not the only one) and signals that are coming from instruments (example MeteoRainRate). These listboxes are empty. I start to work - I create the instance of Meteostation and Meteostation creates instance of MeteoRainRate. I want to show this instrument and signal names in the listboxes. I use getAvailableSignals() to obtain the string name of instrument and I put it to the listbox and I do the same for signal - the same function getAvailableSignals() is in superclass Signal. I put more instruments and more signals ot listboxes and for each instrument I display only relevant signals. Example of the signal MeteoRainRate is below, and there are more, e.g. MeteoTemperature etc. Listboxes are now filled with available instruments and their signals. I select some instrument and then signals which I want to display. As the output of listbox after selection I get cell array with strings - the strings that I put there at the beginning. And I want to click, select and display signals. And here we come to the point, after selection I have 'Rain rate' string from listbox and I ask for this signal in Meteostation class by method getSignalByName('Rain rate') in switch/case. I am not sure if this way is it OK - to communicate between GUI and the class instance by means of string handling.
The shortened Instrument class is:
classdef Meteostation < Instrument
properties (SetAccess = protected)
rainRate
end
properties (Constant)
INSTRUMENT_NAME = 'Meteostation'
end
methods
function [this] = Meteostation(filePath,fileName) % Constructor
% Call superclass DayData
this@Instrument(filePath,fileName);
this.instrumentName = this.INSTRUMENT_NAME;
% load data from mat file and
tempData = load([filePath fileName]);
% fill the object
this.rainRate = MeteoRainRate(tempData.RR);
end % constuctor
function [values, object] = getSignalByName(this,signalName)
switch signalName
case this.rainRate.name
values = this.rainRate.values(:,1);
object = this.rainRate;
end
end
function signalStringArray = getAvailableSignals(this)
signalStringArray = [...
this.rainRate.getAvailableSignals(),...
];
end
end % methods
end % classdef
and here is example of shortened signal class
classdef MeteoRainRate < Signal
properties(Constant)
% Constant and fixed variables
SIGNAL_NAME = 'Rain rate';
SIGNAL_UNIT = 'mm/h';
DEFAULT_SIZE = [1440 1];
end
properties
% all are in superclass Signal but I don't thing it is necessary
end
methods
function [this] = MeteoRainRate(values) % Constructor
% Call for super-class to access common parameters
this@Signal();
% Fill values with rain rate data
this.values = values;
% Init of constant and fixed parameters;
this.name = this.SIGNAL_NAME;
this.unit = this.SIGNAL_UNIT;
this.def_size = this.DEFAULT_SIZE;
end % constuctor
end % methods
end % classdef
Any comments how to do it better are appreciated. Thank you for any help or idea how to code it correctly.

Sign in to comment.

Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!