How to visualise a dynamic array
显示 更早的评论
Hello,
My problem is as follows. I have four pushbuttons made in the GUI which are blue, green, orange and black I also created 12 edit boxes in a row in order to change it's background colour.
So everytime I click one of the four push buttons I store it's value in an array. Reason being is that I want to create a colour pattern that goes up to 12.
For example: I click blue = 1, green = 2, blue = 1, orange = 3 etc which creates the array;
[1 2 1 3 ... up to 12 elements ]
I want to dynamically update and display the colours by changing the colours of the edit boxes. So everytime I press a pushbutton the corresponding editbox (out of the 12) will be coloured.. Any suggestions?
4 个评论
Stalin Samuel
2015-5-9
What you have tried so far..
Image Analyst
2015-5-9
Please attach a screenshot. And why didn't you just use colormapeditor()?
Izuru
2015-5-10
Izuru
2015-5-10
回答(1 个)
Walter Roberson
2015-5-10
function testit
colorlist = [0 0 1; 0 1 0; 1 .4 0; 0 0 0]; %RGB table. blue, green, orange; black
NextEditBoxNum = 1; %shared variable!!
color_record = zeros(1,12); %shared variable!!
pb = zeros(1,12); %pushbuton handles
eb = zeros(1,12); %edit box handles %shared variable!!
function pb_callback(src, obj) %nested function!!
if NextEditBoxNum <= 12
this_colorid = get(src, 'UserData');
color_record(NextEditBoxNum) = this_colorid;
set(eb(NextEditBoxNum), 'BackgroundColor', colorlist(this_colorid, :) );
NextEditBoxNum = NextEditBoxNum + 1;
end
end
for K = 1 : 4
pb(K) = uicontrol('style', 'pushbutton', 'Position', [....], 'UserData', K, 'BackgroundColor', colorlist(K, :), 'Callback', @NextEditBoxNum );
end
for K = 1 : 12
eb(K) = uicontrol('Style', 'edit', 'Position', [....], 'BackgroundColor', [1 1 1]);
end
end
That is, you need something shared between the routines to tell you which is the next edit box to affect (that is, how many times a button has been pushed, and you need something shared that is keeping a record of which color was selected for each. The pushbutton callback figures out which color identifier it corresponds to, adds that to the list recorded so far, and sets the edit box to be the color corresponding to that color identifier.
I am not sure why you are using an edit box to hold the color selected -- are you expecting the user to be entering some text there? If you just want to hold a swath of the color, there are other uicontrol styles that are better suited.
类别
在 帮助中心 和 File Exchange 中查找有关 Startup and Shutdown 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!