diff --git a/Utility functions/Setup.mlapp b/Utility functions/Setup.mlapp index 32ccd94..2ebedea 100644 Binary files a/Utility functions/Setup.mlapp and b/Utility functions/Setup.mlapp differ diff --git a/Utility functions/runInstrument.m b/Utility functions/runInstrument.m index 5cb5c14..acada44 100644 --- a/Utility functions/runInstrument.m +++ b/Utility functions/runInstrument.m @@ -1,93 +1,86 @@ % Create instrument instance and add it to the collector function Instr = runInstrument(name, instr_class, varargin) % Get the unique instance of Collector Collector = MyCollector.instance(); % Check if the instrument is already running if ismember(name, Collector.running_instruments) % If instrument is already present in the Collector, do not create % a new object, but try taking the existing one. disp([name, ' is already running. Assigning the existing ', ... 'object instead of running a new one.']); try Instr = Collector.InstrList.(name); catch error('Could not assign instrument %s from Collector',name); end return end % Create a new instrument object - if nargin()==1 + if ~exist('instr_class', 'var') % Load instr_class, interface, address and other startup arguments % from InstrumentList InstrumentList = getLocalSettings('InstrumentList'); - assert(isfield(InstrumentList, name), [name ' must be a field ' ... - 'of InstrumentList.']) + ind = ([InstrumentList.name]==name); - assert(isfield(InstrumentList.(name), 'control_class'), ... - ['InstrumentList entry ' name ... - ' has no ''control_class'' field.']) + assert(any(ind), [name ' must correspond to an entry in ' ... + 'InstrumentList.']) - instr_class = InstrumentList.(name).control_class; + InstrEntry = InstrumentList(ind); - instr_args = {}; % instrument startup arguments - - if isfield(InstrumentList.(name), 'interface') - instr_args = [instr_args, {'interface', ... - InstrumentList.(name).interface}]; + if length(InstrEntry) > 1 + + % Multiple entries found + warning(['Multiple InstrumentList entries found with ' ... + 'name ' name]); + InstrEntry = InstrEntry(1); end - if isfield(InstrumentList.(name), 'address') - instr_args = [instr_args, {'address', ... - InstrumentList.(name).address}]; - end - - % Make a list of optional name-value pairs. Put the options on the - % left-hand side of the list so that they could not overshadow - % 'interface' and 'address' - if isfield(InstrumentList.(name), 'StartupOpts') - Opts = InstrumentList.(name).StartupOpts; - instr_args = [struct2namevalue(Opts), instr_args]; - end + instr_class = InstrEntry.control_class; + + assert(~isempty(instr_class), ['Control class is not specified '... + 'for ' name]); + + instr_args = struct2namevalue(InstrEntry.StartupOpts); else % Case when all the arguments are supplied explicitly instr_args = varargin; end % Create an instrument instance and store it in Collector Instr = feval(instr_class, instr_args{:}); addInstrument(Collector, name, Instr); try % Open communication. Typically instrument commands will re-open % the communication object if it is closed, but keepeing it open % speeds communication up. if ismethod(Instr, 'openComm') openComm(Instr); end % Send identification request to the instrument if ismethod(Instr, 'sync') sync(Instr); end % Send identification request to the instrument if ismethod(Instr, 'idn') idn(Instr); end catch ME warning(['Could not start communication with ' name ... '. Error: ' ME.message]); end end diff --git a/Utility functions/runInstrumentWithGui.m b/Utility functions/runInstrumentWithGui.m index 9e6b328..f5b098a 100644 --- a/Utility functions/runInstrumentWithGui.m +++ b/Utility functions/runInstrumentWithGui.m @@ -1,54 +1,67 @@ % Create an instrument instance with gui add them to the collector function [Instr, Gui] = runInstrumentWithGui(name, instr_class, gui, varargin) % Get the unique instance of Collector Collector = MyCollector.instance(); % Run instrument first - if nargin==1 + if ~exist('instr_class', 'var') || ~exist('gui', 'var') - % load parameters from InstrumentList + % Run instrument without GUI + Instr = runInstrument(name); + + % Load GUI name from InstrumentList InstrumentList = getLocalSettings('InstrumentList'); - assert(isfield(InstrumentList, name), [name ' must be a field ' ... - 'of InstrumentList.']) + ind = ([InstrumentList.name]==name); - assert(isfield(InstrumentList.(name), 'gui'), ... - ['InstrumentList entry ' name ' has no ''gui'' field.']) + assert(any(ind), [name ' must correspond to an entry in ' ... + 'InstrumentList.']) - gui = InstrumentList.(name).gui; - Instr = runInstrument(name); + InstrEntry = InstrumentList(ind); + + if length(InstrEntry) > 1 + + % Multiple entries found + warning(['Multiple InstrumentList entries found with ' ... + 'name ' name]); + InstrEntry = InstrEntry(1); + end + + gui = InstrEntry.gui; + + assert(~isempty(gui), ['GUI is not specified for ' name]); else - % All the arguments are supplied explicitly + % All arguments are supplied explicitly Instr = runInstrument(name, instr_class, varargin{:}); end % Check if the instrument already has GUI Gui = getInstrumentGui(Collector, name); if isempty(Gui) % Run a new GUI and store it in the collector Gui = feval(gui, Instr); addInstrumentGui(Collector, name, Gui); % Display the instrument's name Fig = findfigure(Gui); if ~isempty(Fig) Fig.Name = char(name); else warning('No UIFigure found to assign the name') end else % Bring the window of existing GUI to the front try Fig = findfigure(Gui); Fig.Visible = 'off'; Fig.Visible = 'on'; catch end end end