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)
Show older comments
Gurubasav Yellur
on 8 Nov 2014
Commented: Gurubasav Yellur
on 8 Nov 2014
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..?
1 Comment
dpb
on 8 Nov 2014
Fix the tags entry please -- it could be "formatting" or the like, but don't put the question content there.
Accepted Answer
More Answers (1)
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.
See Also
Categories
Find more on Characters and Strings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!