Storing fewer decimal places in an array to display using a uitable

I have a list of varaibles that are required to be displayed however, I can't work out how to manipulate the values so that they are presentable when dispalyed in the uitable. I need to decrease the number of decimal places dispalyed and centralise them in the table however I can't work out, nor find on previous and related forums, how to do this.
Thanks yoh in advance for your help.

2 Comments

Is this for "traditional figures" (GUIDE) or "uifigure" (App Designer) ? I would need to research for uifigure(); for traditional figures, the method of centering each entry requires converting to a cell array of character vectors, each of which has to have some HTML 1.1 code added to it.
This is just for traditional figures, although from what you're saying this method sounds a bit above my pay grade. I will reserach into converting the entries to a cell array of character vectors to see whether I can implement this into my code.
Thank you for your help.

Sign in to comment.

 Accepted Answer

Change the number to a string by using function sprintf as follows:
uit1 = uitable(uif,'Position',[5 155 500 100]);
uit1.Data = rand(3,5); % Upper uitable using number
uit2 = uitable(uif,'Position',[5 10 500 100]);
uit2.Data = arrayfun(@(x) sprintf('%.2f',x),rand(3,5),'uni',0); % Lower uitable using string

7 Comments

That's perfect, thank you so much for your help!
You may accept the answer if you find it useful. Thank you.
This will work fine for using a fixed number of decimal places.
In the special case of exactly 2 decimal places, you could instead have set the column format to 'bank'
Unfortunately, using this method of sprintf() will typically result in the values being left-justified inside the entries, rather than center justified (as requested in the question) or right justified (which would tend to look more natural as it would line up the decimal places.) Also be sure to use a fixed-width font if you are lining up the decimal point.
Thank you for the comment.
Yes, a liitle bit more work for right justified.
uit1 = uitable(uif,'Position',[5 155 500 100]);
uit1.Data = rand(3,5); % Upper uitable using number
uit2 = uitable(uif,'Position',[5 10 500 100]);
uit2.Data = arrayfun(@(x) sprintf('%.2f',x),rand(3,5),'uni',0); % Lower uitable using string
s2 = uistyle('HorizontalAlignment','right');
addStyle(uit2,s2);
uistyle is for uifigure only and cannot be used for traditional figures
uif = figure(); %traditional figure not uifigure
uit1 = uitable(uif,'Position',[5 155 500 100]);
uit1.Data = rand(3,5) * 100; % Upper uitable using number
uit2 = uitable(uif,'Position',[5 10 500 100]);
uit2.Data = arrayfun(@(x) sprintf('<HTML><CENTER>%.2f</CENTER>',x),uit1.Data,'uni',0); % Lower uitable using string
You might notice that the alignment is not center in practice. And if you use
uit2.Data = arrayfun(@(x) sprintf('<HTML><TBODY><TR><TD ALIGN="RIGHT">%.2f</TD></TR></TBODY>',x),uit1.Data,'uni',0); % Lower uitable using string
then in practice the alignment is not right (or center if you use that.) But you can tell that TBODY was used: a heavier weight of font is used for the characters.
In theory you can use HTML 1.1 in that context; in practice it can be difficult to get to work.
I seem to remember that I did get it to work in some earlier releases. Unfortunately, it would probably be difficult for me to find the appropriate postings now.
I do really appreicate all of the effort that you've put into trying to help me, so thank you so much for that. I have now got a very good way of presenting my data, and what I am going after is merely for improving aesthetics.
Thanks both for your vital contributions!

Sign in to comment.

More Answers (0)

Categories

Products

Community Treasure Hunt

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

Start Hunting!