Combination of two colors to form a new color entirely using " IF-ELSEIF-ELSE-END" statements
8 views (last 30 days)
Show older comments
Combination of two colors to form a new color entirely using " IF-ELSEIF-ELSE-END" statements
I have two listboxes (listbox1 and listbox2) each of them contains three strings, namely; “red”, “blue” and “green”; one pushbutton (pushbutton1); and one edittext (edittext1) placed on a figure (figure1).
If I press the pushbutton1, I need It to display the name of the new colour (resultant cikiur) formed in the edittext1, as a result of mixing (combining) the selected colour from listbox1 with the selected colour from listbox2.
The desired outputs are:
red + green = yellow (that is, selection of “red” from listbox1 and selection of “green” from listbox2 will display “yellow” in the edittext1, when the pushbutton1 is pressed).
Similarly:
red + blue = magenta (that is, selection of “red” from listbox1 and selection of “blue” from listbox2 will display “magenta” in the edittext1, when the pushbutton1 is pressed).
blue + green = cyan (that is, selection of “blue” from listbox1 and selection of “green” from listbox2 will display “cyan” in the edittext1, when the pushbutton1 is pressed).
The Callback needs to be implemented using If-elseif-else-end ,statements or the Case-Switch structure to run it.
Many thanks.
0 Comments
Answers (4)
DGM
on 17 Dec 2021
Edited: DGM
on 17 Dec 2021
Something like this should be a start:
colornames = {'red','green','blue'}; % i assume this order
% placeholder for the listboxes
selection1 = randi([1 3],1,1);
selection2 = randi([1 3],1,1);
% display randomly selected input
colornames{selection1}
colornames{selection2}
if selection1==selection2
outcolor = colornames{selection1};
else
if all(sort([selection1 selection2]) == [1 2])
outcolor = 'yellow';
elseif all(sort([selection1 selection2]) == [2 3])
outcolor = 'cyan';
elseif all(sort([selection1 selection2]) == [1 3])
outcolor = 'magenta';
end
end
% display the output
outcolor
Which can probably be simplified further.
Obviously, you'll have to adapt this to use the listbox value properties and update the editbox properties and all that.
6 Comments
DGM
on 5 Jan 2022
Edited: DGM
on 5 Jan 2022
Attached is a copy of the mfile which should work. Since the listbox callbacks automatically update the output, the pushbutton is redundant. You can choose to disable the automatic update on listbox changes, that way the button has something to do. If the pushbutton is supposed to do something else, let me know.
List boxes should contain valid items. You have a bunch of instructions and empty lines in the list boxes. This creates a bunch of invalid menu items that can be selected. It would be appropriate to remove them. If you want instruction text, use a text object outside of the listboxes.
Currently, the updatecolor() function handles this problem by mapping the locations of the valid menu items and ignoring the rest. If you decide to clean up the listbox contents, the updatecolor() function can be simplified, or you can simply just change the menuidx vector to [1 2 3].
I would have edited the listboxes and added text labels, but that requires editing the .fig file. Since you're using a different version than I am, I would risk breaking the GUI if I edited it. That's one of the limitations of using GUIDE. I'm assuming that the changes made to the mfile shouldn't break anything though.
DGM
on 26 Apr 2022
The recommendations about fixing the menu items is something you'll have to do. If I use GUIDE to edit the .fig file in a different version than you have, chances are that you won't be able to edit it anymore without problems -- at least that's been my experience with trying to deal with GUIDE between versions. You can try the attached files that I edited, but like I said, don't be surprised if they break.
All I did was clean up the list boxes, add static text labels, and change the output box to a static text box. I disabled the update routine on listbox selection so that the redundant pushbutton has something to do. Because I deleted all the invalid listbox entries, the index remapping and checking routine was removed. It's still full of a bunch of superfluous GUIDE clutter, but that's to be expected.
Gbola
on 25 Apr 2022
1 Comment
Rik
on 25 Apr 2022
Is this a question, or an anwer?
For general advice and examples for how to create a GUI (and avoid using GUIDE), have look at this thread.
Gbola
on 26 Apr 2022
2 Comments
Rik
on 26 Apr 2022
Please post this comment as a comment and delete your 3 answers. The order of answers can change.
Is there a reason you want to keep using GUIDE? It generates a lot of code that isn't needed and it requires you to maintain a fig file along with your m file.
DGM
on 26 Apr 2022
These new files are identical to the ones that were posted prior to my last recommendation.
See Also
Categories
Find more on Migrate GUIDE Apps in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!