diff --git a/@MyGuiSync/MyGuiSync.m b/@MyGuiSync/MyGuiSync.m index ccab52e..e5527bb 100644 --- a/@MyGuiSync/MyGuiSync.m +++ b/@MyGuiSync/MyGuiSync.m @@ -1,753 +1,751 @@ % A mechanism to implement synchronization between parameters and GUI % elements in app-based GUIs classdef MyGuiSync < handle properties (GetAccess = public, SetAccess = protected) Listeners = struct() % Link structures Links = struct( ... 'reference', {}, ... % reference to the link target 'GuiElement', {}, ... % graphics object 'gui_element_prop', {}, ... 'inputProcessingFcn', {}, ... % applied after a value is ... % inputed to GUI 'outputProcessingFcn', {}, ... % applied before a new value is ... % displayed in GUI 'getTargetFcn', {}, ... 'setTargetFcn', {}, ... 'Listener', {} ... % PostSet listener (optional) ); % List of objects to be deleted when App is deleted cleanup_list = {} end properties (Access = protected) App updateGuiFcn end methods (Access = public) function this = MyGuiSync(App, varargin) p = inputParser(); addRequired(p, 'App', ... @(x)assert(isa(x, 'matlab.apps.AppBase'), ... 'App must be a Matlab app.')); % Deletion of kernel object triggers the delition of app addParameter(p, 'KernelObj', [], @(x)assert( ... ismember('ObjectBeingDestroyed', events(x)), ... ['Object must define ''ObjectBeingDestroyed'' event ' ... 'to be an app kernel.'])); % Optional function, executed after an app parameter has been % updated (either externally of internally) addParameter(p, 'updateGuiFcn', [], ... @(x)isa(x, 'function_handle')); parse(p, App, varargin{:}); this.updateGuiFcn = p.Results.updateGuiFcn; this.App = App; this.Listeners.AppDeleted = addlistener(App, ... 'ObjectBeingDestroyed', @(~, ~)delete(this)); if ~isempty(p.Results.KernelObj) KernelObj = p.Results.KernelObj; addToCleanup(this, p.Results.KernelObj); this.Listeners.KernelObjDeleted = addlistener(KernelObj,... 'ObjectBeingDestroyed', @this.kernelDeletedCallback); end end function delete(this) % Delete generic listeners try lnames = fieldnames(this.Listeners); for i=1:length(lnames) try delete(this.Listeners.(lnames{i})); catch fprintf(['Could not delete the listener to ' ... '''%s'' event.\n'], lnames{i}) end end catch fprintf('Could not delete listeners.\n'); end % Delete link listeners for i=1:length(this.Links) try delete(this.Links(i).Listener); catch ME warning(['Could not delete listener for a GUI ' ... 'link. Error: ' ME.message]) end end % Delete the content of cleanup list for i = 1:length(this.cleanup_list) Obj = this.cleanup_list{i}; try if isa(Obj, 'timer') % Stop if object is a timer try stop(Obj); catch end end % Check if the object has an appropriate delete method. % This is a safety measure to never delete a file by % accident. if ismethod(Obj, 'delete') delete(Obj); else fprintf(['Object of class ''%s'' ' ... 'does not have ''delete'' method.\n'], ... class(Obj)) end catch fprintf(['Could not delete an object of class ' ... '''%s'' from the cleanup list.\n'], class(Obj)) end end end % Establish a correspondence between the value of a GUI element and % some other property of the app % % Elem - graphics object % prop_ref - reference to a content of app, e.g. 'var1.subprop(3)' function addLink(this, Elem, prop_ref, varargin) % Parse function inputs p = inputParser(); p.KeepUnmatched = true; % The decision whether to create ValueChangedFcn and % a PostSet callback is made automatically by this function, % but the parameters below enforce these functions to be *not* % created. addParameter(p, 'create_elem_callback', true, @islogical); addParameter(p, 'event_update', true, @islogical); parse(p, varargin{:}); % Make the list of unmatched name-value pairs for subroutine sub_varargin = struct2namevalue(p.Unmatched); % Find the handle object which the end property belongs to, % the end property name and, possibly, further subscripts [Hobj, hobj_prop, RelSubs] = parseReference(this, prop_ref); % Check if the specified target is accessible for reading try if isempty(RelSubs) Hobj.(hobj_prop); else subsref(Hobj.(hobj_prop), RelSubs); end catch disp(['Property referenced by ' prop_ref ... ' is not accessible, the corresponding GUI ' ... 'element will be not linked and will be disabled.']) Elem.Enable = 'off'; return end % Create the basis of link structure (everything except for % set/get functions) Link = createLinkBase(this, Elem, prop_ref, sub_varargin{:}); % Do additional link processing in the case of % MyInstrument commands if isa(Hobj, 'MyInstrument') && ... ismember(hobj_prop, Hobj.command_names) Link = extendMyInstrumentLink(this, Link, Hobj, hobj_prop); end % Assign the function that returns the value of reference Link.getTargetFcn = createGetTargetFcn(this, Hobj, ... hobj_prop, RelSubs); % Check if ValueChanged or another callback needs to be created elem_prop = Link.gui_element_prop; cb_name = findElemCallbackType(this, Elem, elem_prop, ... Hobj, hobj_prop); if p.Results.create_elem_callback && ~isempty(cb_name) % Assign the function that sets new value to reference Link.setTargetFcn = createSetTargetFcn(this, Hobj, ... hobj_prop, RelSubs); switch cb_name case 'ValueChangedFcn' Elem.ValueChangedFcn = ... createValueChangedCallback(this, Link); case 'MenuSelectedFcn' Elem.MenuSelectedFcn = ... createMenuSelectedCallback(this, Link); otherwise error('Unknown callback name %s', cb_name) end end % Attempt creating a callback to PostSet event for the target % property. If such callback is not created, the link needs to % be updated manually. if p.Results.event_update try Link.Listener = addlistener(Hobj, hobj_prop, ... 'PostSet', createPostSetCallback(this, Link)); catch Link.Listener = event.proplistener.empty(); end end % Store the link structure ind = length(this.Links)+1; this.Links(ind) = Link; % Update the value of GUI element updateElementByIndex(this, ind); end % Change link reference for a given element or update the functions % that get and set the value of the existing reference. function reLink(this, Elem, prop_ref) % Find the index of link structure corresponding to Elem ind = findLinkInd(this, Elem); if isempty(ind) return end if ~exist('prop_ref', 'var') % If the reference is not supplied, update existing prop_ref = this.Links(ind).reference; end this.Links(ind).reference = prop_ref; if ~isempty(this.Links(ind).Listener) % Delete and clear the existing listener delete(this.Links(ind).Listener); this.Links(ind).Listener = []; end [Hobj, hobj_prop, RelSubs] = parseReference(this, prop_ref); this.Links(ind).getTargetFcn = createGetTargetFcn(this, ... Hobj, hobj_prop, RelSubs); if ~isempty(this.Links(ind).setTargetFcn) % Create a new ValueChanged callback this.Links(ind).setTargetFcn = createSetTargetFcn(this, ... Hobj, hobj_prop, RelSubs); this.Links(ind).GuiElement.ValueChangedFcn = ... createValueChangedCallback(this, this.Links(ind)); end % Attempt creating a new listener try this.Links(ind).Listener = addlistener(Hobj, hobj_prop, ... 'PostSet', createPostSetCallback(this, ... this.Links(ind))); catch this.Links(ind).Listener = event.proplistener.empty(); end % Update the value of GUI element according to the new % reference updateElementByIndex(this, ind); end function updateAll(this) for i = 1:length(this.Links) % Only update those elements for which listeners do not % exist if isempty(this.Links(i).Listener) updateElementByIndex(this, i); end end % Optionally execute the update function defined within the App if ~isempty(this.updateGuiFcn) this.updateGuiFcn(); end end % Update the value of one linked GUI element. function updateElement(this, Elem) ind = findLinkInd(this, Elem); if ~isempty(ind) updateElementByIndex(this, ind); end end function addToCleanup(this, Obj) this.cleanup_list{end+1} = Obj; end end methods (Access = protected) function kernelDeletedCallback(this, ~, ~) % Switch off the AppBeingDeleted callback in order to prevent % an infinite loop this.Listeners.AppDeleted.Enabled = false; delete(this.App); delete(this); end function f = createPostSetCallback(this, Link) function postSetCallback(~,~) val = Link.getTargetFcn(); if ~isempty(Link.outputProcessingFcn) val = Link.outputProcessingFcn(val); end setIfChanged(Link.GuiElement, Link.gui_element_prop, val); % Optionally execute the update function defined within % the App if ~isempty(this.updateGuiFcn) this.updateGuiFcn(); end end f = @postSetCallback; end % Callback that is assigned to graphics elements as ValueChangedFcn function f = createValueChangedCallback(this, Link) function valueChangedCallback(~, ~) val = Link.GuiElement.Value; if ~isempty(Link.inputProcessingFcn) val = Link.inputProcessingFcn(val); end if ~isempty(Link.Listener) % Switch the listener off Link.Listener.Enabled = false; % Set the value Link.setTargetFcn(val); % Switch the listener on again Link.Listener.Enabled = true; else Link.setTargetFcn(val); end % Update non event based links updateAll(this); end f = @valueChangedCallback; end % MenuSelected callbacks are different from ValueChanged in that % the state needs to be toggled manually function f = createMenuSelectedCallback(this, Link) function menuSelectedCallback(~, ~) % Toggle the menu state if strcmpi(Link.GuiElement.Checked, 'on') Link.GuiElement.Checked = 'off'; val = 'off'; else Link.GuiElement.Checked = 'on'; val = 'on'; end if ~isempty(Link.inputProcessingFcn) val = Link.inputProcessingFcn(val); end if ~isempty(Link.Listener) % Switch the listener off Link.Listener.Enabled = false; % Set the value Link.setTargetFcn(val); % Switch the listener on again Link.Listener.Enabled = true; else Link.setTargetFcn(val); end % Update non event based links updateAll(this); end f = @menuSelectedCallback; end function f = createGetTargetFcn(~, Obj, prop_name, S) function val = refProp() val = Obj.(prop_name); end function val = subsrefProp() val = subsref(Obj, S); end if isempty(S) % Faster way to access property f = @refProp; else % More general way to access property S = [substruct('.', prop_name), S]; f = @subsrefProp; end end function f = createSetTargetFcn(~, Obj, prop_name, S) function assignProp(val) Obj.(prop_name) = val; end function subsasgnProp(val) Obj = subsasgn(Obj, S, val); end if isempty(S) % Faster way to assign property f = @assignProp; else % More general way to assign property S = [substruct('.', prop_name), S]; f = @subsasgnProp; end end % Find the link structure corresponding to Elem function ind = findLinkInd(this, Elem) % Argument 2 is a GUI element, for which we find the link - ind = arrayfun(@(x)isequal(x.GuiElement, Elem), this.Links); + ind = ([this.Links.GuiElement] == Elem); ind = find(ind); if isempty(ind) warning('No link found for the GUI element below.'); disp(Elem); elseif length(ind) > 1 warning('Multiple links found for the GUI element below.'); disp(Elem); ind = []; end end % Update the value of one linked GUI element given the index of % corresponding link function updateElementByIndex(this, ind) Link = this.Links(ind); val = Link.getTargetFcn(); if ~isempty(Link.outputProcessingFcn) val = Link.outputProcessingFcn(val); end % Setting value to a matlab app elemen is time consuming, % so first check if the value has actually changed setIfChanged(Link.GuiElement, Link.gui_element_prop, val); end %% Subroutines of addLink % Parse input and create the base of Link structure function Link = createLinkBase(this, Elem, prop_ref, varargin) % Parse function inputs p = inputParser(); % GUI control element addRequired(p, 'Elem'); % Target to which the value of GUI element will be linked % relative to the App itself addRequired(p, 'prop_ref', @ischar); % Linked property of the GUI element (can be e.g. 'Color') addParameter(p, 'elem_prop', 'Value', @ischar); % If input_prescaler is given, the value assigned to the % instrument propery is related to the value x displayed in % GUI as x/input_presc. addParameter(p, 'input_prescaler', 1, @isnumeric); % Arbitrary processing functions can be specified for input and % output. outputProcessingFcn is applied to values before % assigning them to gui elements and in_proc_fcn is applied % before assigning to the linked properties. addParameter(p, 'outputProcessingFcn', [], ... @(f)isa(f,'function_handle')); addParameter(p, 'inputProcessingFcn', [], ... @(f)isa(f,'function_handle')); % Parameters relevant for uilamps addParameter(p, 'lamp_on_color', MyAppColors.lampOn(), ... @iscolor); addParameter(p, 'lamp_off_color', MyAppColors.lampOff(), ... @iscolor); % Option which allows converting a binary choice into a logical % value addParameter(p, 'map', {}, @this.validateMapArg); parse(p, Elem, prop_ref, varargin{:}); - assert(~any( ... - arrayfun(@(x) isequal(p.Results.Elem, x.GuiElement), ... - this.Links)), ['Another link for the same GUI element ' ... - 'that is attempted to be linked to ' prop_ref ... - ' already exists.']) + assert(all([this.Links.GuiElement] ~= p.Results.Elem), ... + ['Another link for the same GUI element that is ' ... + 'attempted to be linked to ' prop_ref ' already exists.']) % Create a new link structure Link = struct( ... 'reference', prop_ref, ... 'GuiElement', p.Results.Elem, ... 'gui_element_prop', p.Results.elem_prop, ... 'inputProcessingFcn', p.Results.inputProcessingFcn, ... 'outputProcessingFcn', p.Results.outputProcessingFcn, ... 'getTargetFcn', [], ... 'setTargetFcn', [], ... 'Listener', [] ... ); % Lamp indicators is a special case. It is often convenient to % make a lamp indicate on/off state. If a lamp is being linked % to a logical-type variable we therefore assign a dedicated % OutputProcessingFcn that puts logical values in % corresponcence with colors if strcmpi(Elem.Type, 'uilamp') Link.gui_element_prop = 'Color'; % Select between the on and off colors. Link.outputProcessingFcn = @(x)select(x, ... p.Results.lamp_on_color, p.Results.lamp_off_color); return end % Treat the special case of uimenus if strcmpi(Elem.Type, 'uimenu') Link.gui_element_prop = 'Checked'; end if ~ismember('map', p.UsingDefaults) ref_vals = p.Results.map{1}; gui_vals = p.Results.map{2}; % Assign input and output processing functions that convert % a logical value into one of the options and back Link.inputProcessingFcn = @(x)select( ... isequal(x, gui_vals{1}), ref_vals{:}); Link.outputProcessingFcn = @(x)select( ... isequal(x, ref_vals{1}), gui_vals{:}); end % Simple scaling is a special case of value processing % functions. if ~ismember('input_prescaler', p.UsingDefaults) if isempty(Link.inputProcessingFcn) && ... isempty(Link.outputProcessingFcn) Link.inputProcessingFcn = ... @(x) (x/p.Result.input_prescaler); Link.outputProcessingFcn = ... @(x) (x*p.Result.input_prescaler); else warning(['input_prescaler is ignored for target ' ... prop_ref 'as inputProcessingFcn or ' ... 'outputProcessingFcn has been already ' ... 'assigned instead.']); end end end function Link = extendMyInstrumentLink(~, Link, Instrument, tag) Cmd = Instrument.CommandList.(tag); % If supplied command does not have read permission, issue a % warning. if isempty(Cmd.readFcn) fprintf('Instrument property ''%s'' is nor readable\n', ... tag); % Try switching the color of the gui element to orange try Link.GuiElement.BackgroundColor = MyAppColors.warning(); catch try Link.GuiElement.FontColor = MyAppColors.warning(); catch end end end % Generate Items and ItemsData for dropdown menues if they were % not initialized manually if isequal(Link.GuiElement.Type, 'uidropdown') && ... isempty(Link.GuiElement.ItemsData) if all(cellfun(@ischar, Cmd.value_list)) % Capitalized displayed names for beauty Link.GuiElement.Items = cellfun( ... @(x)[upper(x(1)),lower(x(2:end))],... Cmd.value_list, 'UniformOutput', false); else % Items in a dropdown should be strings, so convert if % necessary str_value_list = cell(length(Cmd.value_list), 1); for i=1:length(Cmd.value_list) if ~ischar(Cmd.value_list{i}) str_value_list{i} = num2str(Cmd.value_list{i}); end end Link.GuiElement.Items = str_value_list; end % Assign the list of unprocessed values as ItemsData Link.GuiElement.ItemsData = Cmd.value_list; end % Add tooltip if isprop(Link.GuiElement, 'Tooltip') && ... isempty(Link.GuiElement.Tooltip) Link.GuiElement.Tooltip = Cmd.info; end end % Decide what kind of callback (if any) needs to be created for % the GUI element. Options: 'ValueChangedFcn', 'MenuSelectedFcn' function callback_name = findElemCallbackType(~, ... Elem, elem_prop, Hobj, hobj_prop) % Check property attributes Mp = findprop(Hobj, hobj_prop); prop_write_accessible = strcmpi(Mp.SetAccess,'public') && ... (~Mp.Constant) && (~Mp.Abstract); % Check if the GUI element enabled and editable try gui_element_editable = strcmpi(Elem.Enable, 'on'); catch gui_element_editable = true; end % A check for editability is only meaningful for uieditfieds. % Drop-downs also have 'Editable' property, but it corresponds % to the editability of elements and should not have an effect % on assigning callbacks. if (strcmpi(Elem.Type, 'uinumericeditfield') || ... strcmpi(Elem.Type, 'uieditfield')) ... && strcmpi(Elem.Editable, 'off') gui_element_editable = false; end if ~(prop_write_accessible && gui_element_editable) callback_name = ''; return end % Do not create a new callback if one already exists (typically % it means that a callback was manually defined in AppDesigner) if strcmp(elem_prop, 'Value') && ... isprop(Elem, 'ValueChangedFcn') && ... isempty(Elem.ValueChangedFcn) % This is the most typical type of callback callback_name = 'ValueChangedFcn'; elseif strcmp(elem_prop, 'Checked') && ... strcmpi(Elem.Type, 'uimenu') && ... isempty(Elem.MenuSelectedFcn) % Callbacks for menus callback_name = 'MenuSelectedFcn'; else callback_name = ''; end end % Extract the top-most handle object in the reference, the end % property name and any further subreference function [Hobj, prop_name, Subs] = parseReference(this, prop_ref) % Make sure the reference starts with a dot and convert to % subreference structure if prop_ref(1)~='.' PropSubs = str2substruct(['.', prop_ref]); else PropSubs = str2substruct(prop_ref); end % Find the handle object to which the end property belongs as % well as the end property name Hobj = this.App; Subs = PropSubs; % Subreference relative to Hobj.(prop) prop_name = PropSubs(1).subs; for i=1:length(PropSubs)-1 testvar = subsref(this.App, PropSubs(1:end-i)); if isa(testvar, 'handle') Hobj = testvar; Subs = PropSubs(end-i+2:end); prop_name = PropSubs(end-i+1).subs; break end end end % Validate the value of 'map' optional argument in createLinkBase function validateMapArg(~, arg) try is_map_arg = iscell(arg) && length(arg)==2 && ... length(arg{1})==2 && length(arg{2})==2; catch is_map_arg = false; end assert(is_map_arg, ['The value must be a cell of the form ' ... '{{reference value 1, reference value 2}, ' ... '{GUI dispaly value 1, GUI dispaly value 2}}.']) end end end diff --git a/@MyLog/MyLog.m b/@MyLog/MyLog.m index 3c9a785..8d44d04 100644 --- a/@MyLog/MyLog.m +++ b/@MyLog/MyLog.m @@ -1,858 +1,858 @@ % Class to store data versus time. % Data can be continuously appended and saved. It is possible to add % labels (time marks) for particular moments in time. Data can be saved % and plotted with the time marks. % Metadata for this class is stored independently. % If instantiated as MyLog(load_path) then % the content is loaded from file classdef MyLog < matlab.mixin.Copyable properties (Access = public, SetObservable = true) % Save time as posixtime up to ms precision time_fmt = '%14.3f' % Save data as reals with up to 14 decimal digits. Trailing zeros % are removed by %g data_fmt = '%.14g' % Data column and line separators column_sep = '\t' line_sep = '\r\n' % File extension that is appended by default when saving the log % if a different one is not specified explicitly data_file_ext = '.log' % File extension for metadata meta_file_ext = '.meta' % Formatting options for the metadata metadata_opts = {} file_name = '' % Used to save or load the data data_headers = {} % Cell array of column headers length_lim = Inf % Keep the log length below this limit % Format for string representation of timestamps datetime_fmt = 'yyyy-MMM-dd HH:mm:ss' save_cont = false % If true changes are continuously saved end properties (GetAccess = public, SetAccess = protected, ... SetObservable = true) timestamps % Times at which data was aqcuired data % Array of measurements % Structure array that stores labeled time marks TimeLabels = struct( ... 'time', {}, ... % datetime object 'time_str', {}, ... % time in text format 'text_str', {}); % message string % Structure array that stores all the axes the log is plotted in PlotList = struct( ... 'Axes', {}, ... % axes handles 'DataLines',{}, ... % data line handles 'LbLines', {}, ... % label line handles 'BgLines', {}); % label line background handles % First timestamp that was saved in the current file. This value % is used to decide which time labels to retain under when the log % data is being trimmed. FirstSaveTime = datetime.empty() end properties (Dependent = true) channel_no % Number of data colums data_line_fmt % Format specifier for one data row to be printed column_headers % Time column header + data column headers data_file_name % File name with extension for data saving meta_file_name % File name with extension for metadata saving timestamps_num % Timestamps converted to numeric format end methods (Access = public) function this = MyLog(varargin) P = MyClassParser(this); processInputs(P, this, varargin{:}); end % save the entire data record function save(this, filename) % File name can be either supplied explicitly or given as the % file_name property if nargin() < 2 filename = this.file_name; else this.file_name = filename; end assert(~isempty(filename), 'File name is not provided.'); datfname = this.data_file_name; stat = createFile(datfname); if ~stat return end fid = fopen(datfname, 'w'); % Write column headers str = printDataHeaders(this); fprintf(fid, '%s', str); % Write the bulk of data fmt = this.data_line_fmt; for i = 1:length(this.timestamps) fprintf(fid, fmt, this.timestamps_num(i), this.data(i,:)); end fclose(fid); this.FirstSaveTime = this.timestamps(1); % Save time labels in a separate file saveMetadata(this); end %% Plotting % Plot the log data with time labels. Reurns plotted line objects. function Pls = plot(this, varargin) [~, ncols] = size(this.data); p = inputParser(); % Axes in which log should be plotted addOptional(p, 'Axes', [], @(x)assert( ... isa(x,'matlab.graphics.axis.Axes')||... isa(x,'matlab.ui.control.UIAxes'),... 'Argument must be axes or uiaxes.')); % If time labels are to be displayed addParameter(p, 'time_labels', true, @islogical); % If legend is to be displayed addParameter(p, 'legend', true, @islogical); % Logical vector defining the data columns to be displayed addParameter(p, 'isdisp', true(1,ncols), @(x) assert(... islogical(x) && isvector(x) && length(x)==ncols, ... ['''isdisp'' must be a logical vector of the size ',... 'equal to the number of data columns.'])); % If 'reset' is true than all the data lines and time labels are % re-plotted with default style even if they are already present % in the plot addParameter(p, 'reset', false, @islogical); parse(p, varargin{:}); if ~isempty(p.Results.Axes) Axes = p.Results.Axes; else Axes = gca(); end % Find out if the log was already plotted in these axes. If % not, appned Ax to the PlotList. ind = findPlotInd(this, Axes); if isempty(ind) l = length(this.PlotList); this.PlotList(l+1).Axes = Axes; ind = l+1; end % Plot data if isempty(this.PlotList(ind).DataLines) % If the log was never plotted in Ax, % plot using default style and store the line handles Pls = line(Axes, this.timestamps, this.data); this.PlotList(ind).DataLines = Pls; else % Replace existing data Pls = this.PlotList(ind).DataLines; for i=1:length(Pls) Pls(i).XData = this.timestamps; Pls(i).YData = this.data(:,i); end end % Set the visibility of lines if ~ismember('isdisp', p.UsingDefaults) for i=1:ncols Pls(i).Visible = p.Results.isdisp(i); end end if p.Results.time_labels % Plot time labels plotTimeLabels(this, Axes); else % Hide existing time labels try set(this.PlotList(ind).LbLines, 'Visible', 'off'); set(this.PlotList(ind).BgLines, 'Visible', 'off'); catch end end if (p.Results.legend)&&(~isempty(this.data_headers))&&... (~isempty(this.data)) % Add legend only for for those lines that are displayed disp_ind = cellfun(@(x)strcmpi(x,'on'),{Pls.Visible}); legend(Axes, Pls(disp_ind), this.data_headers{disp_ind},... 'Location','southwest'); end end % Append data point to the log function appendData(this, Time, val) % Format checks on the input data assert(isa(Time, 'datetime') || isnumeric(Time),... ['''time'' argument must be numeric or ',... 'of the class datetime.']); assert(isrow(val) && isnumeric(val), ... '''val'' argument must be a numeric row vector.'); if ~isempty(this.data) [~, ncols] = size(this.data); assert(length(val) == ncols,['Length of ''val'' ' ... 'does not match the number of data columns']); end % Ensure time format if isa(Time,'datetime') Time.Format = this.datetime_fmt; end % Append new data and time stamps this.timestamps = [this.timestamps; Time]; this.data = [this.data; val]; % Ensure the log length is within the length limit trim(this); % Optionally save the new data point to file if this.save_cont try if exist(this.data_file_name, 'file') == 2 % Otherwise open for appending fid = fopen(this.data_file_name, 'a'); else % If the file does not exist, create it and write % the column headers createFile(this.data_file_name); fid = fopen(this.data_file_name, 'w'); str = printDataHeaders(this); fprintf(fid, '%s', str); end % Convert the new timestamps to numeric form for saving if isa(Time, 'datetime') time_num = posixtime(Time); else time_num = Time; end % Append new data points to file fprintf(fid, this.data_line_fmt, time_num, val); fclose(fid); if isempty(this.FirstSaveTime) this.FirstSaveTime = Time; end catch warning(['Logger cannot save data at time = ',... datestr(datetime('now', ... 'Format', this.datetime_fmt))]); % Try closing fid in case it is still open try fclose(fid); catch end end end end %% Time labels % Add label function addTimeLabel(this, varargin) p = inputParser(); addParameter(p, 'Time', datetime('now'), ... @(x)assert(isa(x, 'datetime'), ['''Time'' must be of ' ... 'the type datetime.'])) addParameter(p, 'text', '', ... @(x)assert(iscellstr(x) || ischar(x) || isstring(x), ... '''text'' must be a string or cell array of strings.')) % If 'index' is specified, then the function configures an % existing time label instead of adding a new one addParameter(p, 'index', [], ... @(x)assert(floor(x)==x && x<=length(this.TimeLabels), ... ['Index must be a positive integer not exceeding the ' ... 'number of time labels.'])) parse(p, varargin{:}) Time = p.Results.Time; Time.Format = this.datetime_fmt; if ismember('text', p.UsingDefaults) % Invoke a dialog to input label time and name if ismember('index', p.UsingDefaults) % Default dialog field values dlg_args = {'', datestr(Time)}; else % Provide existing values for modification Tlb = this.TimeLabels(p.Results.index); dlg_args = {char(Tlb.text_str), Tlb.time_str}; end answ = inputdlg({'Label text', 'Time'},'Add time label',... [2 40; 1 40], dlg_args); % If 'Cancel' button is pressed or no input is provided, do % not modify or add a new label if isempty(answ) || isempty(answ{1}) return end % Convert the inputed value to datetime with proper format Time = datetime(answ{2}, 'Format', this.datetime_fmt); % Store multiple lines as cell array str = cellstr(answ{1}); else str = cellstr(p.Results.text); end if ~isempty(this.timestamps) && (Time < this.timestamps(1)) warning(['The time of label ''%s'' is earlier than ' ... 'the first timestamp in memory. Such time label ' ... 'will not be plotted but will be saved in ' ... 'metadata.'], str{1}); end if ~isempty(this.FirstSaveTime) && (Time < this.FirstSaveTime) warning(['The time of label ''%s'' is earlier than ' ... 'the first timestamp in the current data file. ' ... 'Such time label will not be automatically saved ' ... 'in metadata.'], str{1}); end % Find the index of time label. We do not supply a default % value for the end of the list in case the length was changed % over the course of function execution. if ismember('index', p.UsingDefaults) ind = length(this.TimeLabels)+1; else ind = p.Results.index; end this.TimeLabels(ind).time = Time; this.TimeLabels(ind).time_str = datestr(Time); this.TimeLabels(ind).text_str = str; % Order time labels by ascending time sortTimeLabels(this); if this.save_cont saveMetadata(this); end end function deleteTimeLabel(this, ind) this.TimeLabels(ind) = []; if this.save_cont saveMetadata(this); end end % Show the list of labels in a readable format function lst = printTimeLabels(this) if isempty(this.TimeLabels) lst = {}; return end if ~isempty(this.timestamps) % Select only those time labels that are within % the interval of time of currently stored data t_ind = ([this.TimeLabels.time] >= this.timestamps(1)); Tl = this.TimeLabels(t_ind); else Tl = this.TimeLabels; end % The returned output is a list of character strings lst = cell(length(Tl), 1); for i = 1:length(Tl) if ~isempty(Tl(i).text_str) % Display the first line of label lbl = Tl(i).text_str{1}; else lbl = ''; end lst{i} = [Tl(i).time_str, ' ', lbl]; end end %% Misc public functions % Clear log data and time labels function clear(this) % Clear while preserving the array types this.TimeLabels(:) = []; % Delete all the data lines and time labels for i=1:length(this.PlotList) delete(this.PlotList(i).DataLines); delete(this.PlotList(i).LbLines); delete(this.PlotList(i).BgLines); end this.PlotList(:) = []; % Clear data and its type this.timestamps = []; this.data = []; % Switch off continuous saving to prevent the overwriting of % previously saved data this.save_cont = false; end % Convert the log channel record to trace function Trace = toTrace(this, varargin) p = inputParser(); addParameter(p, 'channel', 1, ... @(x)assert(x>0 && floor(x)==x && x<=this.channel_no, ... ['Channel number must be an integer between 1 and ' ... num2str(this.channel_no) '.'])); addParameter(p, 'index', []); % If false, the beginning of x data in the trace is shifted to % zero addParameter(p, 'absolute_time', false, @islogical); parse(p, varargin{:}); n_ch = p.Results.channel; Trace = MyTrace(); if ismember('index', p.UsingDefaults) Trace.x = this.timestamps_num; Trace.y = this.data(:, n_ch); else Trace.x = this.timestamps_num(p.Results.index); Trace.y = this.data(p.Results.index, n_ch); end Trace.name_x = 'Time'; Trace.unit_x = 's'; if ~p.Results.absolute_time % Shift the beginning of data to zero Trace.x = Trace.x-Trace.x(1); end if length(this.data_headers) >= n_ch % Try extracting the name and unit from the column header. % The regexp will match anything of the format 'x (y)' with % any types of brackets, (), [] or {}. tokens = regexp(this.data_headers{n_ch}, ... '(.*)[\(\[\{](.*)[\)\]\}]', 'tokens'); if ~isempty(tokens) % Removes leading and trailing whitespaces tokens = strtrim(tokens{1}); Trace.name_y = tokens{1}; Trace.unit_y = tokens{2}; else Trace.name_y = this.data_headers{n_ch}; end end end end methods (Access = public, Static = true) % Load log from file. Formatting parameters can be supplied as % varargin function L = load(filename, varargin) assert(exist(filename, 'file') == 2, ... ['File ''', filename, ''' is not found.']) L = MyLog(varargin{:}); L.file_name = filename; % Load metadata if file is found if exist(L.meta_file_name, 'file') == 2 Mdt = MyMetadata.load(L.meta_file_name,L.metadata_opts{:}); setMetadata(L, Mdt); else disp(['Log metadata file is not found, continuing ' ... 'without it.']); end % Read column headers from the data file fid = fopen(filename,'r'); dat_col_heads = strsplit(fgetl(fid), L.column_sep, ... 'CollapseDelimiters', true); fclose(fid); % Assign column headers if length(dat_col_heads) > 1 L.data_headers = dat_col_heads(2:end); end % Read data as delimiter-separated values and convert to cell % array, skip the first line containing column headers fulldata = dlmread(filename, L.column_sep, 1, 0); L.data = fulldata(:, 2:end); L.timestamps = fulldata(:, 1); % Convert time stamps to datetime if the time column header % is 'posixtime' if ~isempty(dat_col_heads) && ... contains(dat_col_heads{1}, 'posix', 'IgnoreCase', true) L.timestamps = datetime(L.timestamps, ... 'ConvertFrom', 'posixtime', 'Format', L.datetime_fmt); end L.FirstSaveTime = L.timestamps(1); end end methods (Access = protected) function plotTimeLabels(this, Axes) % Find out if the log was already plotted in these axes ind = findPlotInd(this, Axes); if isempty(ind) l = length(this.PlotList); this.PlotList(l+1).Axes = Axes; ind = l+1; else Axes = this.PlotList(ind).Axes; end if ~isempty(this.timestamps) && ~isempty(this.TimeLabels) % Select for plotting only those time labels that are within % the interval of time of currently stored data t_ind = ([this.TimeLabels.time] >= this.timestamps(1)); Tl = this.TimeLabels(t_ind); else Tl = this.TimeLabels; end % Plot labels for i = 1:length(Tl) T = Tl(i); n_lines = max(length(T.text_str), 1); try Lbl = this.PlotList(ind).LbLines(i); % Update the existing label line Lbl.Value = T.time; Lbl.Label = T.text_str; Lbl.Visible = 'on'; % Update the background width - font size times the % number of lines Bgl = this.PlotList(ind).BgLines(i); Bgl.Value = T.time; Bgl.LineWidth = Lbl.FontSize*n_lines; Bgl.Visible = 'on'; catch % Add new background line this.PlotList(ind).BgLines(i) = xline(Axes, T.time, ... 'LineWidth', 10*n_lines, ... 'Color', [1, 1, 1]); % Add new label line this.PlotList(ind).LbLines(i) = xline(Axes, T.time, ... '-', T.text_str, ... 'LineWidth', 0.5, ... 'LabelHorizontalAlignment', 'center', ... 'FontSize', 10); end end % Remove redundant markers if any n_tlbl = length(Tl); if length(this.PlotList(ind).LbLines) > n_tlbl delete(this.PlotList(ind).LbLines(n_tlbl+1:end)); this.PlotList(ind).LbLines(n_tlbl+1:end) = []; end if length(this.PlotList(ind).BgLines) > n_tlbl delete(this.PlotList(ind).BgLines(n_tlbl+1:end)); this.PlotList(ind).BgLines(n_tlbl+1:end) = []; end end % Ensure the log length is within length limit function trim(this) len = length(this.timestamps); if len <= this.length_lim return end % Remove data points beyond the length limit dn = len-this.length_lim; this.timestamps(1:dn) = []; this.data(1:dn, :) = []; if isempty(this.TimeLabels) return end % Remove only the time labels which times fall outside the % range of both a) trimmed data b) data in the current file BeginTime = min(this.timestamps(1), this.FirstSaveTime); ind = ([this.TimeLabels.time] < BeginTime); this.TimeLabels(ind) = []; end % Print column names to a string function str = printDataHeaders(this) cs = this.column_sep; str = sprintf(['%s',cs], this.column_headers{:}); str = [str, sprintf(this.line_sep)]; end % Find out if the log was already plotted in the axes Ax and return % the corresponding index of PlotList if it was function ind = findPlotInd(this, Ax) assert(isvalid(Ax),'Ax must be valid axes or uiaxes') if ~isempty(this.PlotList) - ind_b = cellfun(@(x) isequal(x, Ax),{this.PlotList.Axes}); + ind_b = ([this.PlotList.Axes] == Ax); - % Find index of the first match - ind = find(ind_b,1); + % Find the index of first match + ind = find(ind_b, 1); else ind = []; end end % Re-order the elements of TimeLabels array so that labels % corresponding to later times have larger index function sortTimeLabels(this) times = [this.TimeLabels.time]; [~,ind] = sort(times); this.TimeLabels = this.TimeLabels(ind); end % Create metadata from log properties function Mdt = getMetadata(this) if ~isempty(this.FirstSaveTime) % Select for saving only those time labels that are within % the interval of time of data in the current file ind = ([this.TimeLabels.time] >= this.FirstSaveTime); Tl = this.TimeLabels(ind); else Tl = this.TimeLabels; end if ~isempty(Tl) % Add the textual part of TimeLabels structure Mdt = MyMetadata(this.metadata_opts{:}, ... 'title', 'TimeLabels'); Lbl = struct('time_str', {Tl.time_str}, ... 'text_str', {Tl.text_str}); addParam(Mdt, 'Lbl', Lbl); else Mdt = MyMetadata.empty(); end end % Save log metadata, owerwriting existing function saveMetadata(this, Mdt) if exist('Mdt', 'var') == 0 Mdt = getMetadata(this); end if isempty(Mdt) return end metfilename = this.meta_file_name; % Create or clear the file stat = createFile(metfilename, 'overwrite', true); if ~stat return end save(Mdt, metfilename); end % Process metadata function setMetadata(this, Mdt) % Assign time labels Tl = titleref(Mdt, 'TimeLabels'); if ~isempty(Tl) Lbl = Tl.ParamList.Lbl; for i=1:length(Lbl) this.TimeLabels(i).time_str = Lbl(i).time_str; this.TimeLabels(i).time = datetime(Lbl(i).time_str, ... 'Format', this.datetime_fmt); this.TimeLabels(i).text_str = Lbl(i).text_str; end end end end %% Set and get methods methods function set.length_lim(this, val) assert(isreal(val),'''length_lim'' must be a real number'); % Make length_lim non-negative and integer this.length_lim = max(0, round(val)); % Apply the new length limit to log trim(this); end function set.data_headers(this, val) assert(iscellstr(val) && isrow(val), ['''data_headers'' must '... 'be a row cell array of character strings.']) %#ok this.data_headers = val; end function set.save_cont(this, val) this.save_cont = logical(val); end function set.file_name(this, val) assert(ischar(val),'''file_name'' must be a character string.') if ~isequal(this.file_name, val) % Reset the first saved timestamp marker this.FirstSaveTime = datetime.empty(); %#ok end this.file_name = val; end % The get function for file_name adds extension if it is not % already present and also ensures proper file separators % (by splitting and combining the file name) function fname = get.data_file_name(this) fname = this.file_name; [filepath,name,ext] = fileparts(fname); if isempty(ext) ext = this.data_file_ext; end fname = fullfile(filepath,[name,ext]); end function fname = get.meta_file_name(this) fname = this.file_name; [filepath,name,~] = fileparts(fname); ext = this.meta_file_ext; fname = fullfile(filepath,[name,ext]); end function data_line_fmt = get.data_line_fmt(this) cs = this.column_sep; nl = this.line_sep; if isempty(this.data) l = 0; else [~,l] = size(this.data); end data_line_fmt = this.time_fmt; for i = 1:l data_line_fmt = [data_line_fmt, cs, this.data_fmt]; %#ok end data_line_fmt = [data_line_fmt, nl]; end function hdrs = get.column_headers(this) % Add header for the time column if isa(this.timestamps,'datetime') time_title_str = 'POSIX time (s)'; else time_title_str = 'Time'; end hdrs = [time_title_str, this.data_headers]; end function time_num_arr = get.timestamps_num(this) % Convert time stamps to numbers if isa(this.timestamps, 'datetime') time_num_arr = posixtime(this.timestamps); else time_num_arr = this.timestamps; end end function val = get.channel_no(this) if isempty(this.data) val = length(this.data_headers); else [~, val] = size(this.data); end end end end