function matrix2m(matrix, digits) %MATRIX2M(A, digits) returns a matrix as a displayed text % % MATRIX2M(A, digits) returns the matrix A as a displayed text. % Designed to copy/add a raw matrix in an m file. % The default value of digits is 4. % The matrix A can have one, two or three dimensions % % Usage example: % % >>A = rand(1,4); % >>matrix2m(A); % returns the text description of A, as follows % % Copy the following text into your m file: % A = [0.2583 0.4094 0.5951 0.2626]; % % See also matrix2WordTable, matrix2WordEq at FileExchange. % J. Palacín, V1.0, July 2024 % http://robotica.udl.cat if nargin == 0 matrix = rand(1,10); out = 'a'; else % get the name of the variable out = inputname(1); if isempty(out) out = 'a'; end end if nargin < 2 % default digits = 4; end % prepare the output disp(' '); disp('Copy the following text into your m file:'); [nr,nc,nd] = size(matrix); if (nd == 1) if (nr == 1) aux = [out ' = [']; for i = 1:1:nc aux = [aux num2str(matrix(i),digits) ' ']; end aux(end) = ']'; aux(end+1) = ';'; disp(aux); disp(' '); else disp([out ' = [...']); for ii = 1:1:nr aux = [' ']; for i = 1:1:nc aux = [aux num2str(matrix(ii,i),digits) ',']; end aux(end) = []; aux = [aux ';...']; disp(aux); end disp(' ];'); disp(' '); end else for iii =1:1:nd disp([out '(:,:,' num2str(iii) ') = [...']); for ii = 1:1:nr aux = [' ']; for i = 1:1:nc aux = [aux num2str(matrix(ii,i,iii),digits) ',']; end aux(end) = []; aux = [aux ';...']; disp(aux); end disp(' ];'); end end