function out = var2str(in,name) % %--------------------------------------------------------------------------------------------------- % var2str.m Converts Matlab variable to command string % % Description This function will take any Matlab variable, and return the string that when % typed in the MATLAB command window creates the input variable. % % Syntax out = var2str(in,name) % % Input Arguments in : variable to be processed % name : variable for recursive data structure calls (not used directly) % % Output Arguments out : the command string to create the passed variable % % Exceptions Cannot handle cell type variables. % Can only handle arrays with no more than 2 dimensions. % % Literature Ref This file is part of the code accompaning the doctoral dissertation of the % author. References throughout the source to tables, equations, concepts, etc % are referring to the material contained in this document. % % %Copyright (2003) Ralph Ewig - ewig@aa.washington.edu This is free software published under the GPL. %--------------------------------------------------------------------------------------------------- %--------------------------------------------------------------------------------------------------- % This program is free software; you can redistribute it and/or modify it under the terms of the GNU % General Public License as published by the Free Software Foundation; either version 2 of the % License, or (at your option) any later version. % % This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without % even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU % General Public License for more details. % % You should have received a copy of the GNU General Public License along with this program; if not, % write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, % USA. % % For questions regardig this software, you can contact ewig@aa.washington.edu %--------------------------------------------------------------------------------------------------- % % $Author: ralph $ % $Revision: 3.0 $ % $Date: 2006/02/05 02:04:27 $ % $Source: /home/cvsroot/PHRITS_Matlab/Utilities/var2str.m,v $ % % Version History: % ---------------- % $Log: var2str.m,v $ % Revision 3.0 2006/02/05 02:04:27 ralph % cvs server rebuild % %--------------------------------------------------------------------------------------------------- if ~exist('name','var'); name = 'A'; end %default name for non-structure variables if isempty(in) ; out=[name '=[]']; return;end %catch empty arrays siz = size(in); % determine size out = cell(1); % initialize ouput %%%%%%%%%%%%%%%%%%% %% numeric arrays %%%%%%%%%%%%%%%%%%% if isa(in,'double') || isa(in,'numeric') || isa(in,'logical') %check if the array is no more than 2-d if max(size(siz)) > 2, error('Cannot support more than 2 dimension array.'),end %this function works a lot faster if it loops over the shorter dimension if find(siz==min(siz)) == 1 %go row wise for e=1:size(in,1) out(e) = {[ name '(' num2str(e) ',:)=[' num2str(in(e,:),' %1.32g') ']']} ; end else %go column wise in = in'; out = cell(1,size(in,1)); lead = [name '(:,']; tail = '])'; for e=1:size(in,1) tmp = {[ lead num2str(e) ')=transpose([' num22str(in(e,:),' %1.32g') tail ]} ; out(1,e) = tmp; end end %%%%%%%%%%%%%%%%%%% %% character arrays %%%%%%%%%%%%%%%%%%% elseif isa(in,'char') %loop through all entries, one row at a time for e=1:size(in,1) txt = in(e,:); % current row txt = strrep(txt,'''',''''''); % escape single quotes out(e) = {[ name '(' num2str(e) ',:)=''' txt '''' ]} ; % store the output end %%%%%%%%%%%%%%%%%%%%%%%%% %% data structure arrays %%%%%%%%%%%%%%%%%%%%%%%%% elseif isa(in,'struct') index = make_index(siz); % make the indeces fields = fieldnames(in); % get the field names of the structure for e=1:numel(in) for f=1:length(fields) now = [ 'in(' deblank(index(e,:)) ').' char(fields(f)) ]; new_name = [ name '(' deblank(index(e,:)) ').' char(fields(f)) ]; out = [out;{var2str(eval(now),new_name)}]; end end %remove the first cell cause its empty out = out(2:end); % %%%%%%%%%%%%%%%%%%%%%%%%% % %% cell arrays % %%%%%%%%%%%%%%%%%%%%%%%%% % elseif isa(in,'cell') % % for e=1:prod(size(in)) % now = [ 'in{' deblank(index(e,:)) '}' ]; % cmd = var2str(eval(now)); % out = [ out; {[ '{' cmd '}']}]; % end % % %remove the first cell cause its empty % out = out(2:end); else out = ['Unsupported variable type: ' class(in)]; error(out); return end %collapse cell array of individual commands into a single line, ";" %delimited string out = implode(out,'%'); return %--------------------------------------------------------------------------------------------------- % creates indeces into an arbitrary dimension vector %--------------------------------------------------------------------------------------------------- function in = make_index(siz,in) if ~exist('in'); lead = []; in = []; else lead = [deblank(in(end,:)) ',' ]; in = in(1:end-1,:); end for i=1:siz(1) line = [lead num2str(i)]; in = strvcat(in,line); if length(siz)>1 in = make_index(siz(2:end),in); end end return %--------------------------------------------------------------------------------------------------- % equivalent to PHP implode fuction - collapses an array into a delimited string %--------------------------------------------------------------------------------------------------- function string = implode(pieces,delimiter) string = []; for i=1:length(pieces) string = [string delimiter pieces{i}]; end return %--------------------------------------------------------------------------------------------------- % skinny num2str version for better performance %--------------------------------------------------------------------------------------------------- function s = num22str(x, f) if isempty(x);s = '';return;end %formatting maxDigitsOfPrecision = 256; k = strfind(f,'%'); dotPositions = strfind(f,'.'); if ~isempty(dotPositions) decimalPosition = find(dotPositions > k(1)); % dot to the right of '%' if ~isempty(decimalPosition) digitsOfPrecision = sscanf(f(dotPositions(decimalPosition(1))+1:end),'%d'); if digitsOfPrecision > maxDigitsOfPrecision error('MATLAB:num2str:exceededMaxDigitsOfPrecision', ... 'Exceeded maximum %d digits of precision.',maxDigitsOfPrecision); end end end d = sscanf(f(k(1)+1:end),'%f'); fi = ['%-' int2str(d) 's']; %------------------------------------------------------------------------------- % Print numeric array as a string image of itself. [m,n] = size(x); scell = cell(1,m); xIsReal = isreal(x); for i = 1:m, t = []; if xIsReal && (min(x(i,:)) >= 0) && (max(x(i,:)) < 2^31-1) t = sprintf(f,x(i,:)); else for j = 1:n, u0 = sprintf(f, real(x(i,j))); % we add a space infront of the negative sign % because Win32 version of sprintf does not. if (j>1) && u0(1)=='-' u = [' ' u0]; else u = u0; end % If we are printing integers and have overflowed, then % add in an extra space. if (real(x(i,j)) > 2^31-1) && (~isempty(findstr(f,'d'))) u = [' ' u]; end if ~xIsReal if imag(x(i,j)) == 0, u = [u '+' formatimag(f,fi,0)]; elseif imag(x(i,j)) > 0 u = [u '+' formatimag(f,fi,imag(x(i,j)))]; elseif imag(x(i,j)) < 0 u = [u '-' formatimag(f,fi,-imag(x(i,j)))]; end end t = [t u]; end end scell{i} = t; end if m > 1 s = strvcat(scell{:}); else s = t; end % If it's a scalar remove the trailing blanks too. s = strrep(s,' ',''); %------------------------------------------------------------------------------ function v = formatimag(f,fi,x) % Format imaginary part v = [sprintf(f,x) 'i']; v = lefttrim(v); v = sprintf(fi,v); %------------------------------------------------------------------------------ function s = lefttrim(s) % Remove leading blanks if ~isempty(s) [r,c] = find(s ~= ' ',1); if ~isempty(c) s = s(:,c:end); end end %--------------------------------------------------------------------------