How to Define a Checkbox?
Afficher commentaires plus anciens
Currently when I run through a code, which I have two checkboxes in, the second is undefined. I was given the original code to update and one of the updates that I made was adding another checkbox. (I made another checkbox using GUIDE.) I have almost an identical method for defining this checkbox, but it says that it is undefined. The results of the checkbox are used in an if statement that determines how the resulting document will be saved.
Checkbox #1 that works: values1 = get(handles.checkbox1,'Value'); if values1 open_system(test_model_name) else load_system(test_model_name) end
My Checkbox: values = get(handles.checkbox2, 'Value'); if values == 0 %...goes into the rest of my statement
Going through the entire code given, the first checkbox is not mentioned anywhere else. How is it defined and the second one not!?!?
12 commentaires
Jan
le 6 Fév 2018
What do you mean with "defining a checkbox"? You can create a checkbox in GUIDe or programmatically by the uicontrol command. You can create the field handles.checkbox2 and assign the handle to the checkbox. GUIDE uses the tag of the checkbox as field name. So maybe your 2nd checkbox does not have the tag "checkbox2"?
Please post the relevant part of the code, the .fig file and a copy of the complete error message.
Lexington Stoyell
le 7 Fév 2018
Walter Roberson
le 7 Fév 2018
Your error message refers to a variable named 'checkbox2' that is not used in any of the code you posted.
The code image you posted attempts to get a property of handles.checkbox2_Callback which is not likely to exist and which would confuse people if it did exist. If you have an object with tag 'checkbox2' then you would get(handles.checkbox2, 'value')
Lexington Stoyell
le 7 Fév 2018
Adam
le 7 Fév 2018
If you create the checkbox in guide then it should create the callback for you, which will be correctly linked to the right checkbox. If you start editing the names of callbacks then things will go wrong.
The code snippet you posted is suspicious because the hint comments for the second one refer to 'checkbox1' rather than 'checkbox2' which suggests you have created it in some non-standard manner.
Lexington Stoyell
le 7 Fév 2018
Walter Roberson
le 7 Fév 2018
The error about undefined function or variable checkbox2 suggests that your code tries to do get(checkbox2, 'value') instead of get(handles.checkbox2, 'value') . But the information you are showing us is confusing because the code you show at one point uses h.checkbox2_Callback with structure name h instead of handles . We cannot debug problems if you are showing us different code than you are actually using.
Lexington Stoyell
le 7 Fév 2018
Walter Roberson
le 7 Fév 2018
Your description disagrees with your posted code https://www.mathworks.com/matlabcentral/answers/uploaded_files/103856/image.png which uses h instead of handles and uses values instead of vis1 . We therefore have reason to suspect that you have multiple code sections that disagree.
Lexington Stoyell
le 7 Fév 2018
Modifié(e) : Walter Roberson
le 7 Fév 2018
Walter Roberson
le 7 Fév 2018
That code cannot produce the error message given previously about undefined variable checkbox2 . At best it could have an error about Reference to non-existent field 'checkbox2'
We need your updated code and your current error message.
Walter Roberson
le 7 Fév 2018
"Going through the entire code given, the first checkbox is not mentioned anywhere else. How is it defined"
It was defined in GUIDE. It lives in the .fig file when not in use, and loading the fig file recreates the graphics object. GUIDE called uicontrol() on your behalf when the GUI was constructed and saved the result in the .fig file.
Réponses (1)
Jan
le 7 Fév 2018
The discussion in the comments gets more complicated that the actual problem is.
If you create a checkbox in GUIDE, the corresponding fields in the handles struct and the callback is created automatically. You can check this easily using the debugger and editing the code.
If your code fails, because a certain field in the handles struct is missing or contains unexpected values, this will be a mistake of the programmer. Then the debugger helps to identify, where the wrong value has been written to the handles struct.
Posting screenshots of the code is very inconvenient for the readers. Post the code as text or attach it instead. But it should not be hard to debug the code by your own.
My problem with helping you is the actual question, which I do not understand:
Going through the entire code given, the first checkbox is not mentioned
anywhere else. How is it defined and the second one not!?!?
What is missing where? Do you get an error message? Then please post it.
Previously I had used checkbox2_Callback in attempts to troubleshoot the issue
This sounds like wild guessing. I know this state during programming. It is the perfect time to switch the monitor off and drink a cup of coffee. Some fresh air helps. Afterwards (preferably tomorrow) start the debugging again. It is very likely, that you find a tiny typo like "chekcbox2" or reveal a small wrong assumption.
Catégories
En savoir plus sur Creating, Deleting, and Querying Graphics Objects dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!