matlab how use format bank in cell arrays

71 views (last 30 days)
Caroline on 22 Jan 2020
Commented: Guillaume on 22 Jan 2020
I am sorry if I repeated any of the questions, but I did not find an answer to my problem anywhere.
I would like the data displayed in my cell array to be in a decimal place.
I used the bank format but it doesn't work.
I tried several solutions with rounding, tahat I found here, but I got the error that the solution is only possible for a 2D table.
I also tried to assign a format to individual rows in the arrays - it doesn't work
I have an cell array of data retrieved from outside (% f)
I need to paste some data to Excel and when I copy it I get the result:
0.763361344537815 6.71428571428571 -4.43000000000000 -0.0363619047619048 0.170616560896359 10 100 2.79400000000000 1.43163922691482
I need many files and formatting in Excel will take a long time, that's why I wanted to change the format directly in matlab.

Answers (2)

Guillaume on 22 Jan 2020
There's several things here:
Firstly, format only affects the way matlab displays numbers at the command prompt. It does not affect what is stored, and it does not affect how they're displayed in the variable editor. Since it does not affect what is stored, a corrolary is that it does not affect the way numbers are exported to excel, text files, or any other file format.
If you set format bank and display your cell array at the command line (with e.g celldisp) then numbers will indeed show with 2 decimals. If that's not the case for you, then you've found a bug. But again, that doesn't affect the numbers themselves, matlab still store them to full precision (~16 decimal digits)
If you want to change the precision with which the numbers are stored, you do need to use round. If your data is stored in a cell array, then round needs to be applied to each cell of the cell array, not to the cell array itself. It's not very clear what you have exactly, something like:
roundedcellarray = cellfun(@(m) round(m, 2), yourcellarray, 'UniformOutput', false);
may work.
You should not be pasting data between matlab and excel (or vice-versa), there are much more reliable ways of transfering data between the two, such as writetable, readtable, writematrix, readmatrix, etc. Again, it's not very clear what you are doing.
Guillaume on 22 Jan 2020
@stephen, you didn't include my (roughly) in your quote which was meant as some big hand-waving to avoid talking about floating point representation. For the purpose of changing 0.763361344537815 to (again hand-waving away actual FP representation) 0.76 rounding would work.
But it's really not clear what caroline is after.

Sign in to comment.

Caroline on 22 Jan 2020
I choose the data I need from previously saved workspace cell arrays and transfer to Excel.
I thought I could do matlab formatting, but it would be better in Excel.
Thank you for the guidance :)

Community Treasure Hunt

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

Start Hunting!