Combination of two colors to form a new color entirely using " IF-ELSEIF-ELSE-END" statements

조회 수: 7 (최근 30일)
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.

답변 (4개)

DGM
DGM 2021년 12월 17일
편집: DGM 2021년 12월 17일
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}
ans = 'blue'
colornames{selection2}
ans = 'green'
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
outcolor = 'cyan'
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
DGM
DGM 2022년 1월 5일
편집: DGM 2022년 1월 5일
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
DGM 2022년 4월 26일
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
Gbola 2022년 4월 25일
Dear all,
The following controls: listbox1, listbox2, one edit, and one pushbutton are meant for combining two primary colours to produce a secondary colour and display same inside the edit, e, g., red + green = yellow.
Find the .m and .fig files below for debugging.
Cheers.
  댓글 수: 1
Rik
Rik 2022년 4월 25일
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
Gbola 2022년 4월 25일
Hi DGM,
As per your request of 05/01/2022, the UI controls: listbox1, listbox2, one edit, and one pushbutton are meant for combining two primary colours to produce a secondary colour as well as display the name of the secondary colour formed inside the edit, e, g., red + green = yellow; red + blue = magenta; green + blue = cyan. While red + red, green + green or blue + blue will display you have chosen the same colour therefore a new colour cannot be formed.
Find the .m and .fig files below for debugging.
Cheers.
  댓글 수: 1
Rik
Rik 2022년 4월 25일
This looks very similar to your other answer, but, like that previous answer, it looks more like a comment. Are you sure you meant to post them as answers to your question?

댓글을 달려면 로그인하십시오.


Gbola
Gbola 2022년 4월 26일
Hi all,
I could not still find a solution to this problem.
Can someone help me out to find what is wrong with the attached .m or / and .fig files? I ran the code in Matlab R2018a using the GUIDE GUIs.
Alternatively, you may wish to solve the question below using your own approach outrightly to solve it without having to debug the attached .m or .fig file.
THE PROBLEM: 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).
While selection of red + red, green + green or blue + blue will display "You have chosen the same colour hence a new colour could not be formed" 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..
  댓글 수: 2
Rik
Rik 2022년 4월 26일
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
DGM 2022년 4월 26일
These new files are identical to the ones that were posted prior to my last recommendation.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by