How to add different colors in particular rows and columns?

30 views (last 30 days)
I have a matrix
data_set_pro =
Columns 1 through 18
20 700 -14017 702.1 14017 20 700 20 702.1 12 698.6 8364.2 12 698.6 8 708.7 5652.7 -5652.4
23 698.6 -16086 700.6 16087 43 699.25 43 701.3 14 697.2 9739.8 26 697.85 9 707.1 6347 -6346.7
28 697.2 -19545 699.2 19546 71 698.44 71 700.47 17 660.95 11214 43 683.26 11 759.15 8331.7 -8331.4
32 660.95 -21175 662.75 21175 103 686.79 103 688.75 19 626.6 11882 62 665.9 13 716.4 9293.2 -9293
43 626.6 -26976 628.25 26976 146 669.07 146 670.93 26 594 15418 88 644.65 17 681.3 11559 -11558
61 594 -36277 595.55 36280 207 646.94 207 648.72 37 533.4 19704 125 611.72 24 691.7 16572 -16572
66 533.4 -35246 534.75 35246 273 619.49 273 621.17 40 479 19130 165 579.55 26 620.95 16117 -16116
82 479 -39325 480.25 39329 355 587.04 355 588.62 49 430.15 21045 214 545.34 33 554.85 18281 -18280
116 430.15 -49956 431.25 49962 471 548.4 471 549.86 70 364.75 25495 284 500.83 46 532.6 24464 -24462
124 364.75 -45283 365.7 45289 595 510.13 595 511.48 74 309.3 22854 358 461.24 50 449.3 22431 -22429
175 309.3 -54192 310.05 54192 770 464.49 770 465.7 105 262.3 27502 463 416.12 70 381.85 26691 -26690
161 262.3 -42280 263 42289 931 429.52 931 430.65 97 209.3 20271 560 380.3 64 344.45 22011 -22009
129 209.3 -27032 209.85 27032 1060 402.72 1060 403.78 77 167.05 12839 637 354.52 52 273.45 14194 -14193
133 167.05 -22244 167.5 22244 1193 376.45 1193 377.44 80 133.3 10642 717 329.84 53 219.35 11602 -11602
197 133.3 -26291 133.7 26301 1390 341.99 1390 342.89 118 99.7 11741 835 297.32 79 184.55 14553 -14550
363 99.7 -36234 99.95 36234 1753 291.82 1753 292.58 218 74.6 16236 1053 251.21 145 138.15 20000 -19998
298 74.6 -22257 74.85 22272 2051 260.26 2051 260.95 179 55.8 9967.2 1232 222.82 119 103.5 12293 -12290
397 55.8 -22179 55.95 22179 2448 227.1 2448 227.7 238 38.95 9250.1 1470 193.05 159 81.5 12934 -12928
751 38.95 -29286 39.1 29323 3199 182.93 3199 183.43 451 27.2 12244 1921 154.11 300 56.9 17042 -17042
263 27.2 -7161.9 27.3 7162 3462 171.1 3462 171.57 158 18.95 2980.3 2079 143.84 105 40 4185.2 -4181.6
1377 18.95 -26125 19 26125 4839 127.8 4839 128.15 826 12.3 10139 2905 106.44 551 29.1 16007 -15986
483 12.3 -5948.1 12.35 5948.2 5322 117.32 5322 117.64 290 7.95 2292.8 3195 97.498 193 19.05 3661.9 -3655.4
337 7.95 -2682.3 8.05 2699.1 5659 110.81 5659 111.12 202 5.15 1028.6 3397 92.007 135 12.35 1654.6 -1653.6
275 5.15 -1417.3 5.2 1418.3 5934 105.91 5934 106.21 165 3.1 499.86 3562 87.888 110 8.45 917.84 -917.45
Columns 19 through 21
8 -5652.4 708.7
17 -11999 707.2
28 -20330 727.25
41 -29623 723.55
58 -41182 710.95
82 -57754 705.2
108 -73870 684.8
141 -92150 654.35
187 -1.1661e+05 624.3
237 -1.3904e+05 587.35
307 -1.6573e+05 540.45
371 -1.8774e+05 506.6
423 -2.0193e+05 477.95
476 -2.1354e+05 449.1
555 -2.2808e+05 411.45
700 -2.4808e+05 354.8
819 -2.6037e+05 318.3
978 -2.733e+05 279.8
1278 -2.9034e+05 227.45
1383 -2.9452e+05 213.2
1934 -3.1051e+05 160.75
2127 -3.1417e+05 147.9
2262 -3.1582e+05 139.8
2372 -3.1674e+05 133.7
i want to put Green colours in the column 1,2,3,6,7,10,11,12 And red colours in column 4,5,8,9,15,16,17,19,20,21. Bold Green in 13,14 and Bold red in 18. Please help me showing colors in command window. Kindly help me for array2table also. is is possible to show colours in table after converting matrix to table.
  7 Comments
Triveni
Triveni on 30 Nov 2022
Sir, rows are optimized after calculation based on algorithm and their size are varry. Basically it is a stock market loss minimizing problem. To view buy and sell data fastly i want to add green and red color respectively. And the optimization has never been easy in spreadsheet, thats why we use Matlab. I hope you will understand user problems and will try to make Matlab easy for future generations.
DGM
DGM on 30 Nov 2022
So far, I think Rik and Walter's suggestions are probably the most appropriate. A simple custom ui would make more sense, and it would probably also afford you a way to conveniently present some other task-relevant functionality.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 30 Nov 2022
I suggest that you use App Designer to create a scrollable uitable with the data. You can then use uistyle to create a color style, and then use addStyle to add the style to a column of the uitable.
  2 Comments
Triveni
Triveni on 1 Dec 2022
Moved: Stephen23 on 1 Dec 2022
fig = uifigure;
Error using matlab.internal.lang.capability.Capability.require
This functionality is not available on remote platforms.

Error in matlab.ui.internal.uifigureImpl (line 32)
Capability.require(Capability.WebWindow);

Error in uifigure (line 26)
window = matlab.ui.internal.uifigureImpl(varargin{:});
fig.Position = [500 500 520 220];
uit = uitable(fig);
uit.Data = data_set_pro;
uit.Position = [20 30 480 135];
s = uistyle("BackgroundColor","green");
addStyle(uit,s,"column",1);
addStyle(uit,s,"column",2);
addStyle(uit,s,"column",3);
addStyle(uit,s,"column",6);
addStyle(uit,s,"column",7);
addStyle(uit,s,"column",10);
addStyle(uit,s,"column",11);
addStyle(uit,s,"column",12);
addStyle(uit,s,"column",13);
addStyle(uit,s,"column",14);
s = uistyle("BackgroundColor","red");
addStyle(uit,s,"column",4);
addStyle(uit,s,"column",5);
addStyle(uit,s,"column",8);
addStyle(uit,s,"column",9);
addStyle(uit,s,"column",15);
addStyle(uit,s,"column",16);
addStyle(uit,s,"column",17);
addStyle(uit,s,"column",18);
addStyle(uit,s,"column",19);
addStyle(uit,s,"column",20);
addStyle(uit,s,"column",21);
Thank you so much for helping and providing right suggestion. This works.
Walter Roberson
Walter Roberson on 1 Dec 2022
note that you can pass a vector of indices for addstyle() which would make your code more compact

Sign in to comment.

More Answers (1)

DGM
DGM on 29 Nov 2022
Moved: DGM on 29 Nov 2022
A numeric array doesn't have a color. The numbers can represent colors, but the array itself has no color properties.
Even if you were to use another array to associate colors with the elements of the given data array and wanted to programmatically print everything to console with colored formatting, you may find that printing colored text to console is limited, largely undocumented, and generally pretty glitchy. See cprintf() on the File Exchange if you're still interested.
A = rand(10,4); % some numeric array
isbig = A>0.85; % describe what gets colored somehow
issmall = A<0.15;
% describe the colors somehow
smallcolor = [0 0.3 0.6];
bigcolor = [0.9 0 0];
% plot it in some way using cprintf() and hope it works
for r = 1:size(A,1)
for c = 1:size(A,2)
if isbig(r,c)
cprintf(bigcolor,'%0.4f ',A(r,c))
elseif issmall(r,c)
cprintf(smallcolor,'%0.4f ',A(r,c))
else
cprintf('Text','%0.4f ',A(r,c))
end
end
cprintf('Text','\n')
end
  3 Comments
DGM
DGM on 29 Nov 2022
Edited: DGM on 30 Nov 2022
You will still have to conditionally print each different element. There is no "make the array green". The console is not a spreadsheet program. It's up to you to define the colors, where they go, and the formatting used.
A = rand(10,4); % some numeric array
% describe the colors somehow
colortable = [0 0.3 0.6; 0.9 0 0];
% plot it in some way using cprintf() and hope it works
for r = 1:size(A,1)
for c = 1:size(A,2)
switch c
case 2
cprintf(colortable(1,:),'%0.4f ',A(r,c))
case 4
cprintf(colortable(2,:),'%0.4f ',A(r,c))
otherwise
cprintf('Text','%0.4f ',A(r,c))
end
pause(0.01)
end
cprintf('Text','\n')
pause(0.01)
end
@Steven Lord's question should be given some attention. In my experience, trying to babysit cprintf() is a lot of work for something that's not very robust and is ultimately not very productive either. Information dumped to console should be seen as ephemeral. It should be concise amounts of content for the user to visualize some particular information and then act upon it.
If instead you want to save the output for any reason, dumping it to console is as absurd as saving images by taking a photograph of the monitor. If you want to save it to a formatted spreadsheet, or to an image, or to something else, then that's the question that needs to be asked.
DGM
DGM on 1 Dec 2022
I think you meant that for Walter. He's the one who recommended using uistyles.

Sign in to comment.

Tags

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!