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 ?

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

Fix the tags entry please -- it could be "formatting" or the like, but don't put the question content there.

Sign in to comment.

 Accepted Answer

More Answers (1)

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.

1 Comment

Thank you very much. The code just worked perfect. In future I'll take care of the formatting while asking a question

Sign in to comment.

Categories

Find more on Get Started with MATLAB 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!