Hi People, I have a matrix, to put it in a simple manner I have taken a vector version of it. A = [1 1.2 2.42 3.2] The matlab output is : A = 1.0000 1.2000 2.4200 3.2000 1. How do I get A = 1 1.2 2.42 3.2 ? 2. How can i then get 1, 1.2, 2.42, 3.2 ?

1 view (last 30 days)
Hi People, I have a matrix, to put it in a simple manner I have taken a vector version of it.
A = [1 1.2 2.42 3.2]
The matlab output is :
A = 1.0000 1.2000 2.4200 3.2000 .
How do I get
A = 1 1.2 2.42 3.2 ? 2.
How can i then get 1, 1.2, 2.42, 3.2 ? i.e '1,' in first column (value with a comma), '1.2,' in second column and so on..?

Accepted Answer

Matt J
Matt J on 8 Nov 2014
Edited: Matt J on 8 Nov 2014
>> A = [1 1.2 2.42 3.2]; num2str(A,3)
ans =
1 1.2 2.42 3.2

More Answers (1)

dpb
dpb on 8 Nov 2014
Edited: dpb on 8 Nov 2014
Depends in part on where you mean you want the output display to be...on the screen or to a file or what.
For display in the command window look at
doc num2str
doc sprintf
For file output, again just use a format string with any of the formatted file io functions. CSV files can be written simply with csvwrite but one gets default precision that way. Use dlmwrite to control the output--
dlmwrite('file.csv',A,'delimiter',',','precision','%.1f')
NB: YOu'll actually get
1.0, 1.2, 2.4, 3.2
this way.
To control a variable number of digits you'll need more logic to ascertain how many digits of precision you really want for each and every value in the array. That's probably simplest by simply using
>> A = [1 1.2 2.42 3.2].';
>> num2str(A)
ans =
1
1.2
2.42
3.2
>>
and using the resulting string representation to either write to file or display.

Categories

Find more on Characters and Strings 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!