convert variable to string. This string can be evaluated by eval function. The reason why I made this function is to edit variable by editbox. [USAGE] [evalstr, available] = vb_var2evalstr(var); [IN] var : to be converted variable [OUT] evalstr : converted string available : = true : you can make variable by eval() function = false : In this case, evalstr contains only type and dimension (e.g. 'cell[5x5x5]') type_dim_str : type and dimension strings(e.g. 'cell[5x5x5]') [NOTE] unsupported case - more than 3 dimensions - cell array which contains except string. - struct is given, just only returns 'struct' [HISTORY] 2010-12-15 rhayashi Initial version Copyright (C) 2011, ATR All Rights Reserved. License : New BSD License(see VBMEG_LICENSE.txt)
0001 function [evalstr, available, type_dim_str] = vb_var2evalstr(var) 0002 % convert variable to string. 0003 % This string can be evaluated by eval function. 0004 % The reason why I made this function is to edit variable by editbox. 0005 % [USAGE] 0006 % [evalstr, available] = vb_var2evalstr(var); 0007 % [IN] 0008 % var : to be converted variable 0009 % [OUT] 0010 % evalstr : converted string 0011 % available : = true : you can make variable by eval() function 0012 % = false : In this case, evalstr contains only type and dimension 0013 % (e.g. 'cell[5x5x5]') 0014 % type_dim_str : type and dimension strings(e.g. 'cell[5x5x5]') 0015 % [NOTE] 0016 % unsupported case 0017 % - more than 3 dimensions 0018 % - cell array which contains except string. 0019 % - struct is given, just only returns 'struct' 0020 % [HISTORY] 0021 % 2010-12-15 rhayashi Initial version 0022 % 0023 % Copyright (C) 2011, ATR All Rights Reserved. 0024 % License : New BSD License(see VBMEG_LICENSE.txt) 0025 0026 % 0027 % --- Previous check 0028 % 0029 if ~exist('var', 'var') 0030 error('var is a required parameter.'); 0031 end 0032 0033 available = true; 0034 evalstr = ''; 0035 % 0036 % --- Get information of variable 0037 % 0038 ret = whos('var'); 0039 dim = length(ret.size); 0040 dim_str = strrep(num2str(mat2str(ret.size)), ' ', 'x'); % e.g.'[5x5x5]' 0041 type_str = ret.class; % e.g. 'cell' 0042 type_dim_str = [type_str, dim_str]; 0043 0044 % 0045 % --- convert variable to string 0046 % 0047 if isstruct(var) 0048 str = 'struct'; 0049 available = false; 0050 elseif dim >= 3 0051 % return size information e.g. [5x5x5] 0052 str = type_dim_str; 0053 available = false; 0054 elseif ischar(var) 0055 str = single_quoted_string(var); 0056 elseif iscellstr(var) 0057 [L, M] = size(var); 0058 str = '{'; 0059 for i=1:L 0060 for j=1:M 0061 str = [str, single_quoted_string(var{i, j})]; 0062 if j<M 0063 str = sprintf('%s,', str); 0064 end 0065 end 0066 if i<L 0067 str = sprintf('%s;\n', str); 0068 end 0069 end 0070 str = [str, '}']; 0071 elseif iscell(var) 0072 % return type and size information 0073 str = type_dim_str; 0074 available = false; 0075 else 0076 str = mat2str(var); 0077 end 0078 evalstr = str; 0079 0080 function out_str = single_quoted_string(in_str) 0081 out_str = sprintf('''%s''', in_str);