Print a matrix in input form

조회 수: 14 (최근 30일)
Bardo
Bardo 2017년 11월 17일
편집: Bardo 2017년 11월 20일
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

채택된 답변

Bardo
Bardo 2017년 11월 20일
편집: Bardo 2017년 11월 20일
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, '];'];

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by