Indexing Error: Index in position 1 exceeds array bounds (must not exceed 1) in For loop

Hello,
I am trying to add color to the rows in my UI Table in the GUI, but I am getting this error
"Index in position 1 exceeds array bounds (must not exceed 1)."
This is my code
AssemblyData= {'Name1',0, 0};
AssemblyTable= uitable(f, 'Units', 'centimeters','Position', [1,1,9,5], 'Data', AssemblyData,'ColumnEditable',[false, true, true], 'ColumnName',{'Name'; 'Offset w.r.t FirstBody[m]';'MeshSize[m]'},'CellEditCallback', {@AssemblyEdit_Callback});
data1 = get(AssemblyTable, 'data'); % pic is attached above
b =[0 0 0;1 0 1; 0 1 1;1 0 0;0 1 0;0 0 1;1 1 1;0 0 0;1 1 0; 1 0 1; 0 1 1;1 0 0;0 1 0;0 0 1;1 1 1;0 0 0;1 1 0; 1 0 1; 0 1 1;1 0 0;0 1 0;0 0 1;1 1 1;0 0 0;1 1 0; 1 0 1; 0 1 1;1 0 0;0 1 0;0 0 1;1 1 1;0 0 0]
for up =1:size(data1,1)
set(AssemblyTable(up,1),'ForegroundColor',b(up,: ))
end
I dont know why i am getting this error..
Does anyone know?

5 Comments

Hamzah - which version of MATLAB are you using? If AssemblyTable a handle to the uitable, then it is a scalar and not the array you are thinking it could be and so trying to access
AssemblyTable(up,1)
will fail since it is a 1x1 scalar. Also, is it possible to change the foreground color of individual cells (I don't think so given the information at table properties and in particular the section for ForegroundColor)? Or maybe you want to change the background colour instead?
for up =1:size(data1,1)
bgColor = get(AssemblyTable,'BackgroundColor');
bgColor(up,:) = b(up,: );
set(AssemblyTable, 'BackgroundColor', bgColor);
end
Hi,
I am using 2019 a version. I am making a gui on a figure
Hi,
this works, but then I cannot see the text in the table :)
I am attaching a picture
Old
New after the code
It works now,
I changed the intensity to less value, now it works
Maybe you have some different solution?
You could probably change the foreground colour (the colour of the text) to white or some other colour that is distinct from the set of colours in your cells.

Sign in to comment.

Answers (0)

Asked:

on 28 Jul 2020

Commented:

on 1 Aug 2020

Community Treasure Hunt

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

Start Hunting!