Clear Filters
Clear Filters

How to manipulate text in uitable?

20 views (last 30 days)
Mark Golberg
Mark Golberg on 31 Aug 2020
Edited: Adam Danz on 1 Sep 2020
Hello,
I have a uitable which is based on a cell.
Please see attached an example column.
I would like to do the following:
  1. Align ALL the fields to be in the CENTER.
  2. To make all the numbers in the following format: XXX.XXXEXX (I mean, no more than 3 digits after the dot).
Can someone post a simple code the the 2 tasks above? THANKS !!!

Accepted Answer

Adam Danz
Adam Danz on 31 Aug 2020
Edited: Adam Danz on 31 Aug 2020
Three methods of centering data containing strings and numbers within a uitable column and setting the format of the numbers for Matlab releases
  1. >= r2019b
  2. >= r2016b
  3. >= r2013a
For Matlab r2019b or later
This will only work if the uitable is within a uifigure (not a regular figure) and requires Matlab r2019b or later.
It converts numeric values to string which often causes more work because if you need to access those values, you must convert them back to numeric.
% Create demo data
C = {'Normal';rand;rand;'Pass';rand;rand};
% Convert numbers to string in %3.2E format
isNum = cellfun(@isnumeric,C);
C(isNum) = compose('%3.2E ',[C{isNum}])
% Load data into UITable
fig = uifigure();
uit = uitable(fig, "Data", C);
% Center all values (requires r2019b or later & use of uifigure)
uis = uistyle('HorizontalAlignment', 'center');
addStyle(uit, uis, 'Column', 1)
For Matlab 2016b or later
This method uses regular figures and use pad() which wasn't available until 2016b.
It also converts numeric values to string which often causes more work because if you need to access those values, you must convert them back to numeric.
% Create demo data
C = {'Normal';rand;rand;'Pass';rand;rand};
% Convert numbers to string in %3.2E format
isNum = cellfun(@isnumeric,C(:,1));
C(isNum,1) = strsplit(strtrim(sprintf('%3.2E ',[C{isNum,1}])));
% Pad left and right with spaces to equate lengths
C(:,1) = pad(C,'both');
% Load data into UITable
fig = figure();
uit = uitable(fig, "Data", C);
For Matlab r2013a or later
Time to upgrade!
This version uses sprintf instead of compose and it uses a low-level method of padding instead of pad. It also uses strsplit which became available in r2013a.
It also converts numeric values to string which often causes more work because if you need to access those values, you must convert them back to numeric.
% Create demo data
C = {'Normal';rand;rand;'Pass';rand;rand};
% Convert numbers to string in %3.2E format
isNum = cellfun(@isnumeric,C(:,1));
C(isNum,1) = strsplit(strtrim(sprintf('%3.2E ',[C{isNum,1}])));
% produce pre and post pads
nchar = cellfun(@numel, C(:,1));
nSpaces = bsxfun(@minus, max(nchar), nchar)/2; % *
prePad = arrayfun(@(n){repmat(' ',1,n)},floor(nSpaces));
postPad = arrayfun(@(n){repmat(' ',1,n)},ceil(nSpaces));
% Pad the cell array
C(:,1) = strcat(prePad, C(:,1),postPad)
% Load data into UITable
fig = figure();
uit = uitable(fig, "Data", C);
*Thanks Walter Roberson for the bsxfun reminder!
  13 Comments
Adam Danz
Adam Danz on 1 Sep 2020
Edited: Adam Danz on 1 Sep 2020
Sounds like you got it working. Figuring out what works 10-releases back can be challenging and time comsuming.
I briefly looked into the column width fitting, specifically for threads in the Answers forum that dated back to 2015 or prior. It's unclear whether the auto-column-with is expected to work or not but some people were setting the column widths based on the max number of characters in each column. Again, the html code poses a problem for your data since that adds a lot of extra characters that don't actually appear in the table.
These 4 lines below find the max number of characters per column while ignoring any cells with html
nonHTML = cellfun(@isempty, regexp(C, '\<html\>'));
strlen = cellfun(@numel, C);
strlen(~nonHTML) = nan;
maxLen = max(strlen,[],1);
% maxLen =
% 9 9 9 9 86 86 10 10 9 9 11 86 86 32 27 86 31 26 86
As you can see, there are some columns with large numbers of characters even though we're ignoring html cells. I looked into this and apparently you've got some cells in the example data that contain a large number of empty spaces. Maybe that doesn't exist in your data.
If you use this method, note that column width is specified in units of pixesl whereas the values above are in units of characters. Unless you use a fixed-width font, the pixel width of characters vary. So you can either estimate the avg pixel-width of all character and then add a bit of space (I'm sure there's data out there already) or you can used a fixed width font and figure out how many pixels you need per character.
Mark Golberg
Mark Golberg on 1 Sep 2020
Thank you. Indeed that's what I'm using, estimating column width based on maxLen in each column (multiplying by some apriory found factor).
I agree, it's quite frustrating working with such an old version, knowing that alot of this could be easly solved in the newer ones. But, chanllenges are what we're here for, right?! :-)
THANK you very much for all your assitance, Adam.
---
I've posted another question, in a different thread. regarding freezing row/column in a uitable? Perhaps you could have a look.

Sign in to comment.

More Answers (0)

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Products


Release

R2015a

Community Treasure Hunt

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

Start Hunting!