save to 2nd decimal, then add brackets
1 view (last 30 days)
Show older comments
I needed to round a matrix of double to the second decimal. And also convert the second row such that each entry is in brackets, and then save to mat, txt, or xls.
This is the initial matrix
A=[1.224, 1.338;
2.451, -2.367;
1.222, 4.123]
For rounding, I tried
sprintf('%.2f\n',A)
But it generates a vector not a matrix. For adding the brackets, I tried to use
strcat('(',A(2,:),')'), not working.
The change should result in this (either a mat or txt or xls)
B=[1.22, 1.34;
(2.45), (-2.37);
1.22, 4.12]
0 Comments
Accepted Answer
Stephen23
on 3 Feb 2018
Edited: Stephen23
on 3 Feb 2018
A = [1.224,1.338;2.451,-2.367;1.222,4.123];
fdir = '.'; directory
name = 'myfile.txt';
fmt = 'B=[%.2f, %.2f;\n(%.2f), (%.2f);\n%.2f, %.2f]';
[fid,msg] = fopen(fullfile(fdir,name),'wt');
assert(fid>=3,msg)
fprintf(fid,fmt,A.');
fclose(fid);
3 Comments
Stephen23
on 3 Feb 2018
The code save a file that produces exactly what you asked for:
B=[1.22, 1.34;
(2.45), (-2.37);
1.22, 4.12]
If you want something else please specify it clearly.
More Answers (0)
See Also
Categories
Find more on Dates and Time 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!