フィルターのクリア

Print a matrix in input form

4 ビュー (過去 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 Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by