Print a matrix in input form

14 visualizaciones (últimos 30 días)
Bardo
Bardo el 17 de Nov. de 2017
Editada: Bardo el 20 de Nov. de 2017
Hi,
is there a way to display a matrix in input format?
So a copy/paste would be valid code, like
>> inputForm(A)
A = [1, 2, 3; ...
4, 5, 6; ...
7, 8, 9];
>>
Would be quite helpful for automatic code generation.
Thanks

Respuesta aceptada

Bardo
Bardo el 20 de Nov. de 2017
Editada: Bardo el 20 de Nov. de 2017
function s = inputForm(A, name)
%s = inputForm(A, name)
% Create assignment code from disp output
%
% Inputs
% A: anything disp can handle
% name: variable A is assigned to
% Output
% s: string with expression 'name = A;'
if nargin == 0 % demo
A = randn(4);
name = 'Amat';
end
nl = sprintf('\n');
s = evalc('disp(A)');
if strfind(s, 'Columns')
disp('cannot yet handle that many columns')
disp('you can try to enlarge the Command Window')
error('');
end
s = strrep(s, '[', '');
s = strrep(s, ']', '');
s = strrep(s, nl, ['; ...', nl]);
s = [name, ' = [ ...', nl, s, '];'];

Más respuestas (0)

Categorías

Más información sobre Characters and Strings en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by