Home > functions > internal > dsReadText.m

dsReadText

PURPOSE ^

Purpose: read equations for DynaSim model from .mech, .eqns, or .m file.

SYNOPSIS ^

function text = dsReadText(file)

DESCRIPTION ^

 Purpose: read equations for DynaSim model from .mech, .eqns, or .m file.
 
 See also: dsCheckSpecification, dsParseModelEquations

 Author: Jason Sherfey, PhD <jssherfey@gmail.com>
 Copyright (C) 2016 Jason Sherfey, Boston University, USA

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function text = dsReadText(file)
0002 % Purpose: read equations for DynaSim model from .mech, .eqns, or .m file.
0003 %
0004 % See also: dsCheckSpecification, dsParseModelEquations
0005 %
0006 % Author: Jason Sherfey, PhD <jssherfey@gmail.com>
0007 % Copyright (C) 2016 Jason Sherfey, Boston University, USA
0008 
0009 if ischar(file) && exist(file,'file')
0010   [~,name,ext]=fileparts2(file);
0011   switch ext
0012     case '.m'
0013       model=feval(name); % evaluate model-creating function and return model
0014       return;
0015     case '.mat' % todo: uncomment once dsImportModel supports loading .mat
0016       %model=dsImportModel(text);
0017       %return;
0018   end
0019   
0020   % load equations from file
0021   [text,res]=readtext(file,'\n','%'); % text: cell array of strings, one element per line in text file
0022   
0023   % remove all lines without text
0024   text=text(res.stringMask);
0025   
0026   % remove leading/trailing white space
0027   text=strtrim(text);
0028   
0029   % end each line with semicolon
0030   for i=1:length(text)
0031     if ~isequal(text{i}(end),';')
0032       text{i}(end+1)=';';
0033     end
0034   end
0035   
0036   % concatenate into a single string
0037   text=[text{:}]; % concatenate text from all lines
0038 else
0039   warning('File not found: %s',file);
0040   text='';
0041 end

Generated on Tue 12-Dec-2017 11:32:10 by m2html © 2005