How to center-align uitable cells which are all numerical values?

조회 수: 7 (최근 30일)
Milos Krsmanovic
Milos Krsmanovic 2021년 8월 18일
댓글: darova 2021년 8월 21일
I've found quite a few answers on this but they mostly discuss the strings. I have numerical values.
Some answers mention setting 'ColumnFormat' to 'bank' which a) doesn't seem to work for me, the cells remain right-align and b) this changes the format of the number where I'd like to keep the default formatting.
Does anyone have an advice on how to align numerical values in my uitable (I only have numerical values - no strings)?
Thank you in advance.
  댓글 수: 2
darova
darova 2021년 8월 19일
Why don't you use sprintf? Convert numbers to strings?
s = sprintf('a = %8.2f\n',1000*rand(10,1))
s =
'a = 942.96 a = 684.09 a = 170.93 a = 314.49 a = 150.75 a = 146.01 a = 529.80 a = 589.68 a = 443.83 a = 843.92 '
Milos Krsmanovic
Milos Krsmanovic 2021년 8월 19일
편집: Milos Krsmanovic 2021년 8월 19일
I mean, I was interested in if it's possible to do it with numerical values, as in a general question.
In particular, I have a for loop that reads an array of strings, then uses them to match conditions and pull the data from a table:
crit1 = {'Some','Key','Words','Here','To','Set','The','Criterion'};
crit2 = {'More','Too'};
e = numel(crit1);
for i = 1:e
data(1,i)=num2cell(mean(tableName.As(results.TableHeading1==string(crit1(i)) & tableName.TableHeading2==string(crit2(1)))));
end
I use the data to plot the uitable on a figure. But sprintf wouldn't fit in this for loop, would it?

댓글을 달려면 로그인하십시오.

채택된 답변

darova
darova 2021년 8월 20일
Here is an example. Read more about sprintf
crit1 = {'Some','Key','Words','Here','To','Set','The','Criterion'};
s = sprintf('%10s\n',crit1{:}) % 10 places, \n - new line
s =
' Some Key Words Here To Set The Criterion '
  댓글 수: 3
Milos Krsmanovic
Milos Krsmanovic 2021년 8월 21일
All right, I'll give it a try.
I was only wondering if uitable has a property or something that would allow this to be set directly. Seems like a lot of work for something so elementary.
Nevertheless, I appreciate the inputs and I'm accepting this as an answer in case someone else might need it in the future. Cheers.
darova
darova 2021년 8월 21일
  • Seems like a lot of work for something so elementary.
I agree. But basically you don't ask for aligning but for adding some spaces left/right
Here is another approach using strjust
crit1 = {'Some','Key','Words','Here','To','Set','The','Criterion'};
sl = cellfun(@length,crit1); % length of each word (cell)
sform = ['%' num2str(max(sl)+2) 's']; % string format for sprintf
s0 = cellfun(@(x)sprintf(sform,x),crit1,'uniformoutput',0) % add blanks/spaces
s0 = 1×8 cell array
{' Some'} {' Key'} {' Words'} {' Here'} {' To'} {' Set'} {' The'} {' Criterion'}
strjust(s0,'center') % align word by center
ans = 1×8 cell array
{' Some '} {' Key '} {' Words '} {' Here '} {' To '} {' Set '} {' The '} {' Criterion '}

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by