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

3 views (last 30 days)
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.

Answers (1)

Stephen23
Stephen23 on 16 Feb 2015
Edited: Stephen23 on 16 Feb 2015
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.

Categories

Find more on Operators and Elementary Operations in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!