What is the simplest way to write header on each column of an array?

조회 수: 3 (최근 30일)
Franck Kamga
Franck Kamga 2015년 2월 16일
편집: Stephen23 2015년 2월 16일
Here is the format that I know for an array with 2 column, and I would like to improve to a much simplest way.
header = {x column, y column}
fprintf( '%s %s\n', header{:})
M = [(fprintf('%d\n',x)),(fprintf('%1.2E\n',y))];
The problem with this code is it doesn't actually show me the array as 2 column.

답변 (1개)

Stephen23
Stephen23 2015년 2월 16일
편집: Stephen23 2015년 2월 16일
If x and y have the same number of elements, try this instead:
fprintf('%d %1.2e\n', [x(:),y(:)].')
Shown here in a complete working example:
>> y = 0:pi/4:pi;
>> x = 1:numel(A);
>> fprintf('%d %1.2e\n', [x(:),y(:)].')
1 0.00e+00
2 7.85e-01
3 1.57e+00
4 2.36e+00
5 3.14e+00
Also note that you have unnecessary parentheses around the fprintf statements, and that according to the documentation, M will contain an array giving the number of bytes printed.

카테고리

Help CenterFile Exchange에서 Operators and Elementary Operations에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by