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

5 次查看(过去 30 天)
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