John Doe
John Doe

Reputation: 229

How to display numbers in colour in a matrix (MATLAB)

Currently I have a matrix, say something like:
A=[1 2 3 4 5; 4 7 3 2 2; 4 5 5 9 1]

What I want is to somehow display the final 'n' columns of this in a certain colour, where n can be 1,2,3,etc. So if n=1, I'd want to display the matrix as it is, but with the column [5;2;1] to be coloured red. Is it possible to do this in MATLAB? If so, how?

Thanks for any help

Upvotes: 2

Views: 64

Answers (1)

OmG
OmG

Reputation: 18838

A simple solution is using cprintf and for:

certainColor = 'green';
rowLength = size(A,1);
columnLength = size(A,2);
for idx = 1 : rowLength
    for jdx = 1 : (columnLength - n)
        cprintf('text', '%d\t', A(idx,jdx))
    end
    for jdx = n : -1 : 1
        cprintf(certainColor, '%d\t', A(idx, columnLength - jdx + 1))
    end
    cprintf('text', '\n')
end

Upvotes: 3

Related Questions