Combination of two colors to form a new color entirely using " IF-ELSEIF-ELSE-END" statements
11 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Respuestas (4)
DGM
el 17 de Dic. de 2021
Editada: DGM
el 17 de Dic. de 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 comentarios
DGM
el 5 de En. de 2022
Editada: DGM
el 5 de En. de 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
el 26 de Abr. de 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
el 25 de Abr. de 2022
1 comentario
Rik
el 25 de Abr. de 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
el 26 de Abr. de 2022
2 comentarios
Rik
el 26 de Abr. de 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
el 26 de Abr. de 2022
These new files are identical to the ones that were posted prior to my last recommendation.
Ver también
Categorías
Más información sobre Migrate GUIDE Apps en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!