Code covered by the BSD License  

Highlights from
Simscape Language Equations, Pretty Print

image thumbnail

Simscape Language Equations, Pretty Print

by

 

14 Jan 2011 (Updated )

MATLAB code for displaying Simscape language equations in a format that is easy to read.

eqs=ssc2latex(filename)
function eqs=ssc2latex(filename)
% Copyright 2011 The MathWorks, Inc.
  
  %%
  fidr = fopen(filename,'r');
  cr = onCleanup(@()fclose(fidr));
  
  %% SCAN SSC file
  T=textscan(fidr,'%s','Delimiter','\n');
  T=T{1,1};
  
  %% Look for equations section and keep list of equations
  idxEqWord = regexp(T,'^equations');
  idxEqWord = ~cellfun(@isempty,idxEqWord);
  posidxEqWord = find(idxEqWord);
  shortList = T(posidxEqWord+1:end);
  % remove empty lines
  shortList=shortList(~cellfun(@isempty,shortList));
  
  idxEnd = regexp(shortList,'^end');
  idxEnd = ~cellfun(@isempty,idxEnd);
  idxEq = find(idxEnd);
  
  % list of equations
  eqs = shortList(1:idxEq(end-1)-1);
  
  %% convert equation to latex expression
  % remove comments
  % TODO : handle comments
  eqs = regexprep(eqs,'[ ]*%.*','');
  
  % remove empty lines
  eqs = eqs(~cellfun(@isempty,eqs));
  
  % remove spaces at end of line
  eqs = regexprep(eqs,'[ ]*$','');
  
  % remove ...
  eqs = concatMultipleLines(eqs);
  
  % replace '}' by '\}'
  eqs = strrep(eqs,'}','\}');
  
  % replace '{' by '\{'
  eqs = strrep(eqs,'{','\{');
  
  % if else elseif
  pattern = '(^|\ )(if)($|\ )';
  eqs = regexprep(eqs,pattern,['$1' '\\mathit{if}' '$3']);
  pattern =  '(^|\ )(else)($|\ )';
  eqs = regexprep(eqs,pattern,['$1' '\\mathit{else}\\;' '$3']);
  pattern =  '(^|\ )(elseif)($|\ )';
  eqs = regexprep(eqs,pattern,['$1' '\\mathit{elseif}\\;' '$3']);
 
  % replace ' ' by '\;'
  eqs = strrep(eqs,' ','\;');
  
  % replace == by =
  eqs = strrep(eqs,'==','=');
  
  % replace '&' by '\ \&\ '
  eqs = strrep(eqs,'&','{\&}');
  
  % nicely format >= and <=
  eqs = strrep(eqs,'>=','\ge ');
  eqs = strrep(eqs,'<=','\le ');
  
  % replace *.der by \frac{*}{dt}
  pattern = '([\w\.\{\}]+).der';
  eqs = regexprep(eqs, pattern, ['\\frac{d' '$1' '}{dt}']);
  
  % replace x^y by {x}^{y}
  eqs = cellfun(@(C)replacePattern(C,'\^',1),eqs,'UniformOutput',false);    %equation's right side x^y   ->   x^{y}
  eqs = cellfun(@(C)replacePattern(C,'\^',1,2),eqs,'UniformOutput',false);  %equation's left side  x^{y} -> {x}^{y}
  
  % replace x/y by \frac{x}{y}
  eqs = cellfun(@(C)replacePattern(C,'/'),eqs,'UniformOutput',false);               %equation's right side x/y   ->       x/{y}
  eqs = cellfun(@(C)replacePattern(C,'/',0,2,'\frac',1),eqs,'UniformOutput',false); %equation's left side  x/{y} -> \frac{x}{y}
  
  % functions
  pattern = {'(sin\()','(cos\()','(tan\()','(asin\()','(acos\()','(atan\()',...
    '(atan2\()','(log\()','(log10\()','(sinh\()','(cosh\()','(tanh\()','(exp\()','(sqrt\()'};
  eqs = regexprep(eqs,pattern,'\\$1');
    
  % replace sqrt(x) by sqrt{x}
  eqs = cellfun(@(C)replacePattern(C,'sqrt'),eqs,'UniformOutput',false);
  
  % replace * by \times
  eqs = strrep(eqs,'*','{\times}');
  
  % replace '_' by '\_' (if string contains multiple _ change only the first one
  pattern = '([^0-9A-Za-z]?[a-zA-Z][0-9a-zA-Z]*[\_])([\w]+)';
  eqs = regexprep(eqs,pattern,'$1{${regexprep($2,''\_'',''\\\\_'')}}');
  
  % greek letters
  greekLetters = {'(alpha)','(beta)','(gamma)','(delta)','(epsilon)','(zeta)',...
    '(theta)','(Theta)','([^zhZ]eta)','(iota)','(kappa)','(lambda)','(mu)','(nu)', '(xi)',...
    '(pi)','(rho)','(sigma)','(tau)','(upsilon)', '(phi)','(chi)','(psi)',...
    '(omega)','(Gamma)','(Delta)','(Lambda)','(Xi)','(Pi)',...
    '(Sigma)','(Upsilon)', '(Phi)','(Psi)','(Omega)'};
  % replace 'psi' by '\psi'
  pattern = cellfun(@(C) strcat('([^a-zA-Z]*)',strcat(C,'([^a-zA-Z0-9]*)')),greekLetters,'UniformOutput',false);
  eqs = regexprep(eqs,pattern,'$1\\$2$3');
  % replace 'psir' by '\psi_r'
  pattern = cellfun(@(C) strcat(C,'([a-zA-Z0-9]+)'),greekLetters,'UniformOutput',false);
  eqs = regexprep(eqs,pattern,'$1_$2');
  
  % replace ' by \prime
  eqs = strrep(eqs,'''','\prime');
  
  % replace ( by \left(
  eqs = strrep(eqs,'(','\left(');

  % replace ) by \right)
  eqs = strrep(eqs,')','\right)');

  % TODO indent
%   eqs = indent(eqs);
  
  % add $$ around the strings for latex interpreter
  eqs=strcat(strcat('$$',eqs),'$$');
  
end

Contact us