diff --git a/@MyLog/MyLog.m b/@MyLog/MyLog.m index f95faa9..7047bce 100644 --- a/@MyLog/MyLog.m +++ b/@MyLog/MyLog.m @@ -1,293 +1,293 @@ -% +% Class to store data series versus time classdef MyLog < matlab.mixin.Copyable properties (Access=public) % 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 columns are separated by this symbol data_column_sep = '\t' % File extension that is appended by default when saving the log % if a different one is not specified explicitly file_ext = '.log' file_name = '' % Used to save or load the data data_headers = {} % Cell array of column headers end properties (SetAccess=public, GetAccess=public) timestamps % Times at which data was aqcuired data % Cell array of measurements Headers % MyMetadata object to store labeled time marks end properties (Dependent=true) % Information about the log, including time labels and data headers Metadata % Format specifier for one data line data_line_fmt end methods (Access=public) %% Constructo and destructor methods function this = MyLog(varargin) P=MyClassParser(this); P.KeepUnmatched=true; processInputs(P, this, varargin{:}); this.Headers=MyMetadata(varargin{:}); % Load the data from file if the file name was provided if ~ismember('file_name', P.UsingDefaults) loadLog(this); end end %% Save and load functionality % save the entire data record function saveLog(this, fname) % File name can be either supplied explicitly or given as the % file_name property if nargin()<2 fname = this.file_name; end % Verify that the data can be saved assert(isDataArray(this),... ['Data cannot be reshaped into array, saving in '... 'text format is not possible. You may try saving as ',... '.mat file instead.']); % Add file extension if it is not specified explicitly if ~ismember('.',fname) fname = [fname, this.file_ext]; end try createFile(fname); % Write time labels and column headers printAllHeaders(this.Metadata, fname); fid = fopen(fname,'a'); % Write data body fmt=this.data_line_fmt; for i=1:length(this.timestamps) fprintf(fid, fmt,... posixtime(this.timestamps(i)), this.data{i}); end fclose(fid); catch warning('Log was not saved'); % Try closing fid in case it is still open try fclose(fid); catch end end end % Save log header to file function loadLog(this, fname) if nargin()<2 fname=this.file_name; end [this.Headers, end_line_no]=MyMetadata('load_path',fname); % Read data as delimiter-separated values and convert to cell % array mdata = dlmread(filename, this.column_sep, end_line_no, 0); [m,~] = size(mdata); this.data = mat2cell(mdata, ones(1,m)); if ismember('ColumnNames', this.Headers.field_names) cnames=structfun(@(x) x.value, this.Headers.ColumnNames,... 'UniformOutput', false); % The first column name is time, so skip it cnames(1)=[]; % Assign the rest of the names to data headers for i=1:length(cnames) this.data_headers{i}=cnames{i}; end % Delete the field ColumnNames as it is generated % automatically when referring to Metadata and is not % supposed to be stored in the Headers deleteField(this.Headers, 'ColumnNames') end end %% Other functions % Append data point to the log function appendPoint(this, time, val, varargin) p=inputParser(); addParameter(p, 'save', false, @islogical); parse(p, varargin{:}); this.timestamps=[this.timestamps; time]; this.data=[this.data; {val}]; if p.Results.save try exstat = exist(this.file_name,'file'); if exstat==0 % if the file does not exist, create it and write % the metadata createFile(this.file_name); printAllHeaders(this.Metadata, this.file_name); fid = fopen(this.file_name,'a'); else % otherwise open for appending fid = fopen(this.file_name,'a'); end fprintf(fid, this.data_line_fmt, posixtime(time), val); fclose(fid); catch warning(['Logger cannot save data at time = ',... datestr(time)]); % Try closing fid in case it is still open try fclose(fid); catch end end end end % Add label to the metadata function addTimeLabel(this, time, str) if nargin()<3 % Invoke a dialog to add the label time and name answ = inputdlg({'Label text', 'Time'},'Add time label',... [2 40; 1 40],{'',datestr(datetime('now'))}); if isempty(answ)||isempty(answ{1}) return else % Conversion of the inputed value to datetime to % ensure proper format time=datetime(answ{2}); str=answ{1}; end end time_str=datestr(time); fieldname=genvarname('Lbl1', this.Headers.field_names); addField(this.Headers, fieldname); addParam(this.Headers, fieldname, 'time', time_str); % str can contain multiple lines, record them as separate % parameters [nlines,~]=size(str); for i=1:nlines strname=genvarname('str1',... fieldnames(this.Headers.(fieldname))); addParam(this.Headers, fieldname, strname, str(i,:)); end end % Plot the log data with time labels function plotLog(this, Ax) if nargin()<2 % If axes handle was not supplied, create new axes Ax = axes(); else cla(Ax); end try mdata = cell2mat(this.data); catch warning(['Cannot display logger data, '... 'possibly because of data dimensions being different ',... 'at different times. Can try crlearing data to resolve.']) return end hold(Ax,'on'); [~, n] = size(mdata); % Plot data for i=1:n plot(Ax, this.timestamps, mdata(:,i)); end % Plot time labels hold(Ax,'off'); % Add legend if n>=1 && ~isempty(this.data_headers{:}) legend(Ax, this.data_headers{:},'Location','southwest'); ylabel(Ax, app.y_label); end end function clearLog(this) this.timestamps = {}; this.data = {}; delete(this.Headers); this.Headers = MyMetadata(); end % Check if data is suitable for plotting and saving as a list of % numerical vectors of equal length function bool = isDataArray(this) % An empty cell array passes the test if isempty(this.data) bool = true; return end % Then check if all the elements are numeric vectors and have % the same length. Difference between columns and rows is % disregarded here. l=length(this.data{1}); bool = all(cellfun(@(x)(isreal(x)&&(length(x)==l)),this.data)); end end %% set and get methods methods function data_line_fmt=get.data_line_fmt(this) cs=this.data_column_sep; nl=this.Headers.line_sep; if isempty(this.data) l=0; else % Use end of the data array for better robustness when % appending a measurement l=length(this.data{end}); 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 Metadata=get.Metadata(this) Metadata=copy(this.Headers); if ismember('ColumnNames', Metadata.field_names) deleteField(Metadata, 'ColumnNames') end addField(Metadata, 'ColumnNames'); addParam(Metadata, 'ColumnNames', 'name1',... 'POSIX time (s)') for i=1:length(this.data_headers) tmpname = genvarname('name1',... fieldnames(Metadata.ColumnNames)); addParam(Metadata, 'ColumnNames', tmpname,... this.data_headers{i}) end end end end diff --git a/@MyMetadata/MyMetadata.m b/@MyMetadata/MyMetadata.m index ffe5f5f..4eabb50 100644 --- a/@MyMetadata/MyMetadata.m +++ b/@MyMetadata/MyMetadata.m @@ -1,331 +1,334 @@ classdef MyMetadata < dynamicprops & matlab.mixin.Copyable properties (Access=public) % Header sections are separated by [hdr_spec,hdr_spec,hdr_spec] - hdr_spec + hdr_spec='==' % Data starts from the line next to [hdr_spec,end_header,hdr_spec] - end_header - column_sep % Columns are separated by this symbol - comment_sep % Comments start from this symbol - line_sep + end_header='Data' + % Columns are separated by this symbol + column_sep=' \t' + % Comments start from this symbol + comment_sep='%' + line_sep='\r\n' % Limit for column padding. Variables which take more space than % this limit are ignored when calculating the padding length. - pad_lim + pad_lim=12 end properties (Access=private) PropHandles %Used to store the handles of the dynamic properties end properties (Dependent=true) field_names end methods function [this,varargout]=MyMetadata(varargin) p=inputParser; + p.KeepUnmatched=true; addParameter(p,'hdr_spec','==',@ischar); addParameter(p,'load_path','',@ischar); addParameter(p,'end_header','Data',@ischar); addParameter(p,'column_sep',' \t',@ischar); addParameter(p,'comment_sep','%',@ischar); addParameter(p,'line_sep','\r\n',@ischar); addParameter(p,'pad_lim',12,@isreal); parse(p,varargin{:}); this.hdr_spec=p.Results.hdr_spec; this.column_sep=p.Results.column_sep; this.comment_sep=p.Results.comment_sep; this.end_header=p.Results.end_header; this.pad_lim=p.Results.pad_lim; this.line_sep=p.Results.line_sep; this.PropHandles=struct(); if ~isempty(p.Results.load_path) varargout{1}=scanHeaders(this,p.Results.load_path,... 'end_header',p.Results.end_header); end end %Fields are added using this command. The field is a property of %the class, populated by the parameters with their values and %string specifications for later printing function addField(this, field_name) assert(isvarname(field_name),... 'Field name must be a valid MATLAB variable name.'); assert(~ismember(field_name, this.field_names),... ['Field with name ',field_name,' already exists.']); this.PropHandles.(field_name)=addprop(this,field_name); this.PropHandles.(field_name).SetAccess='protected'; this.PropHandles.(field_name).NonCopyable=false; this.(field_name)=struct(); end %Deletes a named field function deleteField(this, field_name) assert(isvarname(field_name),... 'Field name must be a valid MATLAB variable name.'); assert(ismember(field_name,this.field_names),... ['Attemped to delete field ''',field_name ... ,''' that does not exist.']); % Delete dynamic property from the class delete(this.PropHandles.(field_name)); % Erase entry in PropHandles this.PropHandles=rmfield(this.PropHandles,field_name); end %Clears the object of all fields function clearFields(this) cellfun(@(x) deleteField(this, x), this.field_names) end % Copy all the fields of another Metadata object to this object function addMetadata(this, Metadata) assert(isa(Metadata,'MyMetadata'),... 'Input must be of class MyMetadata, current input is %s',... class(Metadata)); assert(~any(ismember(this.field_names,Metadata.field_names)),... ['The metadata being added contain fields with the same ',... 'name. This conflict must be resolved before adding']) for i=1:length(Metadata.field_names) fn=Metadata.field_names{i}; addField(this,fn); param_names=fieldnames(Metadata.(fn)); cellfun(@(x) addParam(this,fn,x,Metadata.(fn).(x).value,... 'fmt_spec', Metadata.(fn).(x).fmt_spec,... 'comment', Metadata.(fn).(x).comment),... param_names); end end %Adds a parameter to a specified field. The field must be created %first. function addParam(this, field_name, param_name, value, varargin) assert(ischar(field_name),'Field name must be a char'); assert(isprop(this,field_name),... '%s is not a field, use addField to add it',param_name); assert(ischar(param_name),'Parameter name must be a char'); p=inputParser(); % Format specifier for printing the value addParameter(p,'fmt_spec','',@ischar); % Comment to be added to the line addParameter(p,'comment','',@ischar); parse(p,varargin{:}); %Adds the field, making sure that neither value nor comment %contain new line or carriage return characters, which would %mess up formating when saving the header newline_smb={sprintf('\n'),sprintf('\r')}; %#ok if (ischar(value)||isstring(value)) && ... contains(value, newline_smb) fprintf(['Value of ''%s'' must not contain ',... '''\\n'' and ''\\r'' symbols, replacing them ',... 'with '' ''\n'], param_name); this.(field_name).(param_name).value=... replace(value, newline_smb,' '); else this.(field_name).(param_name).value=value; end if contains(p.Results.comment, newline_smb) fprintf(['Comment string for ''%s'' must not contain ',... '''\\n'' and ''\\r'' symbols, replacing them ',... 'with '' ''\n'], param_name); this.(field_name).(param_name).comment= ... replace(p.Results.comment, newline_smb,' '); else this.(field_name).(param_name).comment=p.Results.comment; end this.(field_name).(param_name).fmt_spec=p.Results.fmt_spec; end function printAllHeaders(this, fullfilename) addTimeHeader(this); for i=1:length(this.field_names) printHeader(this, fullfilename, this.field_names{i}); end printEndHeader(this, fullfilename); end function printHeader(this, fullfilename, field_name, varargin) %Takes optional inputs p=inputParser; addParameter(p,'title',field_name); parse(p,varargin{:}); title_str=p.Results.title; ParamStruct=this.(field_name); param_names=fieldnames(ParamStruct); %width of the name column name_pad_length=max(cellfun(@(x) length(x), param_names)); % Make list of parameter values converted to strings par_strs=cell(1,length(param_names)); par_lengths=zeros(1,length(param_names)); for i=1:length(param_names) TmpParam=ParamStruct.(param_names{i}); if isempty(TmpParam.fmt_spec) % Convert to string with format specifier % extracted from the varaible calss par_strs{i}=var2str(TmpParam.value); else par_strs{i}=sprintf(TmpParam.fmt_spec, TmpParam.value); end % For beauty, do not account for variables with excessively % long value strings when calculating the padding if length(par_strs{i})<=this.pad_lim par_lengths(i)=length(par_strs{i}); end end %width of the values column val_pad_length=max(par_lengths); fileID=fopen(fullfilename,'a'); %Prints the header fprintf(fileID,[this.hdr_spec, title_str,... this.hdr_spec, this.line_sep]); for i=1:length(param_names) %Capitalize first letter of comment if ~isempty(ParamStruct.(param_names{i}).comment) fmt_comment=[this.comment_sep,' '... upper(ParamStruct.(param_names{i}).comment(1)),... ParamStruct.(param_names{i}).comment(2:end)]; else fmt_comment=''; end print_spec=[sprintf('%%-%is',name_pad_length),... this.column_sep,... sprintf('%%-%is',val_pad_length),... this.column_sep,'%s', this.line_sep]; fprintf(fileID, print_spec, param_names{i}, par_strs{i},... fmt_comment); end %Prints an extra line at the end fprintf(fileID, this.line_sep); fclose(fileID); end %Print terminator that separates header from data function printEndHeader(this, fullfilename) fileID=fopen(fullfilename,'a'); fprintf(fileID,... [this.hdr_spec, this.end_header, ... this.hdr_spec, this.line_sep]); fclose(fileID); end %Adds time header function addTimeHeader(this) if isprop(this,'Time') deleteField(this,'Time') end dv=datevec(datetime('now')); addField(this,'Time'); addParam(this,'Time','Year',dv(1),'fmt_spec','%i'); addParam(this,'Time','Month',dv(2),'fmt_spec','%i'); addParam(this,'Time','Day',dv(3),'fmt_spec','%i'); addParam(this,'Time','Hour',dv(4),'fmt_spec','%i'); addParam(this,'Time','Minute',dv(5),'fmt_spec','%i'); addParam(this,'Time','Second',floor(dv(6)),'fmt_spec','%i'); addParam(this,'Time','Millisecond',... round(1000*(dv(6)-floor(dv(6)))),'fmt_spec','%i'); end function n_end_header=scanHeaders(this, fullfilename, varargin) %Before we load, we clear all existing fields clearFields(this); fileID=fopen(fullfilename); title_exp=[this.hdr_spec,'(\w.*)',this.hdr_spec]; %Loop initialization line_no=0; curr_title=''; %Loop continues until we reach the next header or we reach the end of %the file while ~feof(fileID) line_no=line_no+1; %Grabs the current line curr_line=fgetl(fileID); %Gives an error if the file is empty, i.e. fgetl returns -1 if curr_line==-1 error('Tried to read empty file. Aborting.') end %Skips if current line is empty if isempty(curr_line) continue end res_str=regexp(curr_line,title_exp,'once','tokens'); %If we find a title, first check if it is the specified %end header. Then change the title if a title was found, %then if no title was found, put the data under the current %title. if ismember(res_str, this.end_header) break elseif ~isempty(res_str) % Apply genvarname for sefety in case the title string % is not a proper variable name curr_title=genvarname(res_str{1}); addField(this,curr_title); %This runs if there was no match for the regular %expression, i.e. the current line is not a header, and the %current line is not empty. We then add this line to the %current field (curr_title). elseif ~isempty(curr_title) % First separate the comment if present tmp=strsplit(curr_line, this.comment_sep); if length(tmp)>1 % the line has comment comment_str=[tmp{2:end}]; else comment_str=''; end % Then process name-value pair tmp=strsplit(tmp{1}, this.column_sep,... 'CollapseDelimiters',true); if length(tmp)>=2 % If present line does not contain name-value pair, % ignore it name=strtrim(tmp{1}); % Assume everything after the 1-st column separator % to be the value and attempt convertion to number val=strtrim([tmp{2:end}]); val=str2doubleHedged(val); %Store retrieved value addParam(this, curr_title, name, val,... 'comment',comment_str); end end end if isempty(this.field_names) warning('No header found, continuing without header') n_end_header=1; else n_end_header=line_no; end end end methods function field_names=get.field_names(this) field_names=fieldnames(this.PropHandles); end end end \ No newline at end of file