How do you align numbers within a column in UItable?

We're using GUIDE to build financial applications for investment bankers. They expect to see a column of dollar figures right aligned but there seems to be no obvious way to right align characters in a column in UItable. Any help would be GREATLY appreciated!!!

 채택된 답변

Walter Roberson
Walter Roberson 2011년 3월 10일

1 개 추천

Golf'ing Oleg's solution:
dat = {'23234.43 $'; '234.43 $'; '0.23 $'};
dat = cellfun(@(s) sprintf('%*s',max(cellfun(@length,dat)),s),dat);
and then pick up at "Create table".

댓글 수: 3

The second I submitted the version with the strcat I thought sprintf would be more suited in this case...doh.
BTW, you're missing a 'un',0 in the last cellfun.
Ah yes, you are right, the outer cellfun should have 'un',0
so smart!

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

추가 답변 (2개)

Oleg Komarov
Oleg Komarov 2011년 3월 10일

1 개 추천

I came up with this solution:
dat = {'23234.43 $'; '234.43 $'; '0.23 $'};
Pad string amounts with blanks
l = cellfun(@length,dat);
dat = num2cell([arrayfun(@blanks,max(l)-l,'un',0) dat],1);
dat = strcat(dat{:});
Create table with courier font
f = figure('Position',[200 200 400 150]);
cnames = 'Amounts';
rnames = {'First','Second','Third'};
t = uitable('Parent',f,'Data',dat,'ColumnName',cnames,...
'RowName',rnames,'Position',[20 20 360 100],...
'fontname','courier');
Oleg

댓글 수: 2

Peter
Peter 2011년 3월 10일
Unfortunately because it's not a fixed width font the blank padding still doesn't get you to right alignment. Each number takes up a different amount of space - unless I'm missing something.
You had to change fontname to a monospaced set

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

Walter Roberson
Walter Roberson 2011년 3월 10일

0 개 추천

Does setting the ColumnFormat to 'bank' work ?

댓글 수: 2

It will work with numeric datatypes...not with strings.
Peter
Peter 2011년 3월 10일
We'd like to have commas and no 0s (dealing with large numbers in the hundreds of millions). 'Bank' format goes out to the penny and has no commas

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

Community Treasure Hunt

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

Start Hunting!

Translated by