Changing the color if text in uitable

Hi all, I have a uitable and I want to display negative numbers in red, in my code I pass the numbers to text in order to get the format I want from java, heres my code:
function ColoredTable
table = uitable;
data = randn(3,6)*1000; %Just to generate some numbers with negative
DataSet = cell(size(data));
%I put the number format I want, but it makes the number a string
for i = 1:numel(data)
DataSet{i} = char(java.text.DecimalFormat('#,###,##0.00').format(Data(i)));
end
%In this line I try to acomplish the coloring, but fails.
DataReady = cellfun(@(x) colText(x, 'red'),strfind(DataReady,'-'),'UniformOutput',false);
%To set the table
set(table,'Data', DataReady,'Position', [1,1,800,100],'ColumnWidth',{86})
end
function outHtml = colText(inText, inColor)
% return a HTML string with colored font
outHtml = ['<font color="', ...
inColor, ...
'">', ...
inText, ...
'</font>'];
end
What I end up getting is a table with red squares, the "good" thing is that at least it's the actual cells that get changed, I thing that the cellfun function is not getting the actual text from the strfind, instead it get something else.

 Accepted Answer

strfind() does not return the text that is located: it returns the index of the start of the match.

4 Comments

So, what function returns the text of the index match?
regexp() with the 'match' option will return the match instead of the index.
But if you return the matched text then because your search is for '-' then all you are going to get back is '-' as the match.
You also need to reconsider how you are handling cells that do not start with '-'. Your current code does not copy them unchanged, so if your cellfun was successful at selecting only the negative cells, then the output of the cellfun would be only the colored negative cells and when you set() that as the Data you would have missed out on the cells that are not to be colored.
I was aware of that, so for the cells not starting with '-' I was thinking making a loop to put them back using the index from the match something like: ~cellfun(@isempty x), because I don't know how can cellfun manage to options, or isn't cellfun the way to aproach it at all? What would you recommend to solve this, I've got little experience with matlab and it's my first time making a GUI, so I'm kind of lost at this point, still I've got much further with Matlab than R. Thanks for your help.
DataReady = cellfun(@colneg, DataReady);
function CS = colneg(S)
if length(S) > 0 & S(1) == '-'
Cs = colText(S, 'red');
else
Cs = S;
end

Sign in to comment.

More Answers (0)

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!