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

9 Ansichten (letzte 30 Tage)
Franck Kamga
Franck Kamga am 16 Feb. 2015
Bearbeitet: Stephen23 am 16 Feb. 2015
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.

Antworten (1)

Stephen23
Stephen23 am 16 Feb. 2015
Bearbeitet: Stephen23 am 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.

Kategorien

Mehr zu Operators and Elementary Operations finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by