How to center-align uitable cells which are all numerical values?
8 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Milos Krsmanovic
am 18 Aug. 2021
Kommentiert: darova
am 21 Aug. 2021
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 Kommentare
darova
am 19 Aug. 2021
Why don't you use sprintf? Convert numbers to strings?
s = sprintf('a = %8.2f\n',1000*rand(10,1))
Milos Krsmanovic
am 19 Aug. 2021
Bearbeitet: Milos Krsmanovic
am 19 Aug. 2021
Akzeptierte Antwort
darova
am 20 Aug. 2021
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
3 Kommentare
darova
am 21 Aug. 2021
- 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
strjust(s0,'center') % align word by center
Weitere Antworten (0)
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!