Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
default value of check box

Subject: default value of check box

From: Rajesh

Date: 23 Mar, 2010 07:40:25

Message: 1 of 14

Hi everyone,
I have a checkbox in my GUI. we know when it is checked the value returned is 1 and when unchecked it returns 0. but if it is left unattended then irrespective of its status the value returned is an empty matrix. How to use/set a default value for a checkbox based on its status.

Subject: default value of check box

From: ImageAnalyst

Date: 23 Mar, 2010 10:38:08

Message: 2 of 14

On Mar 23, 3:40 am, "Rajesh " <rv_acha...@rediffmail.com> wrote:
> Hi everyone,
> I have a checkbox in my GUI. we know when it is checked the value returned is 1 and when unchecked it returns 0. but if it is left unattended then irrespective of its status the value returned is an empty matrix. How to use/set a default value for a checkbox based on its status.  

--------------------------------------------------------------------------------
I'm not sure what you mean. A checkbox has to be either checked or
unchecked doesn't it? I'm not aware of any "unattended" state.
Perhaps does that mean you have the checkbox disabled?

And I'm not sure what you mean by "setting or getting a checkbox value
based on its status." Isn't the "status" the same thing as its value
(unless you mean the status to be something else such as the "enable"
or "visible" property)?

Subject: default value of check box

From: Rajesh

Date: 25 Mar, 2010 07:01:22

Message: 3 of 14


hi,
sorry for the delay i am afraid i dint get a notification. anyway.

may be my question was not clear. let me elaborate. I have a checkbox on a GUI. when i run the GUI i see the checkbox unchecked on the interface. Lets call it the default state. Now in the callback function of the box i have written

global var1
var1=get(hObject,'Value');

further, I am using var1 as a condition to skip (if var1==0) or perform (if var1==1) a certain loop in another callback.

Now, if i intend to skip this particular loop i would simply leave the box unchecked but when i do this all i get is an empty matrix for var1 whereas what i want is var1=0 since the box was unchecked.

However, if i check the box and then uncheck it again i get var1=0.

am i missing something here. 'cause if an innocent user wants an unchecked box why would/should he check it first and then uncheck it to get the desired results??

I feel i would be required to read the current status of all such boxes on the UI when i launch it but i dont know how to do it.
hope its clear now.

Subject: default value of check box

From: Walter Roberson

Date: 25 Mar, 2010 07:11:30

Message: 4 of 14

Rajesh wrote:


> I have a checkbox on
> a GUI. when i run the GUI i see the checkbox unchecked on the interface.
> Lets call it the default state.

> Now, if i intend to skip this particular loop i would simply leave the
> box unchecked but when i do this all i get is an empty matrix for var1
> whereas what i want is var1=0 since the box was unchecked.
>
> However, if i check the box and then uncheck it again i get var1=0.

I recall seeing that on an earlier version; I haven't checked to see if
it is still an issue.

The solution for me was simply to explicitly set the Value of the box at
the time the uicontrol was created. As that is better programming
practice anyhow, I just always do that now.

Subject: default value of check box

From: Rajesh

Date: 26 Mar, 2010 09:09:02

Message: 5 of 14


> The solution for me was simply to explicitly set the Value of the box at
> the time the uicontrol was created. As that is better programming
> practice anyhow, I just always do that now.

God, How i wish Mr. Walter roberson could also tell how to set it at the time of creation.
I changed the 'Value' field in the property inspector to 1.0 and the default state changed to 'checked' from previous 'unchecked' but i am afraid my var1 is still an empty matrix.
:(
I am using R2007a btw.

Subject: default value of check box

From: Marc Crapeau

Date: 26 Mar, 2010 10:45:06

Message: 6 of 14

"Rajesh " <rv_acharya@rediffmail.com> wrote in message <hohtje$ecl$1@fred.mathworks.com>...
>
> > The solution for me was simply to explicitly set the Value of the box at
> > the time the uicontrol was created. As that is better programming
> > practice anyhow, I just always do that now.
>
> God, How i wish Mr. Walter roberson could also tell how to set it at the time of creation.
> I changed the 'Value' field in the property inspector to 1.0 and the default state changed to 'checked' from previous 'unchecked' but i am afraid my var1 is still an empty matrix.
> :(
> I am using R2007a btw.

Just change your "if var1==0" with "if var1==0 | isempty(var1)" :p

Subject: default value of check box

From: ImageAnalyst

Date: 26 Mar, 2010 13:06:37

Message: 7 of 14

On Mar 26, 5:09 am, "Rajesh " <rv_acha...@rediffmail.com> wrote:
> God, How i wish Mr. Walter roberson could also tell how to set it at the time of creation.
> I changed the 'Value' field in the property inspector to 1.0 and the default state changed to 'checked' from previous 'unchecked' but i am afraid my var1 is still an empty matrix.
> :(
> I am using R2007a btw.  
--------------------------------------------------------------------------------
Well you could use GUIDE like I do. I never have that problem. I can
create a checkbox and it's value is either 0 or 1 depending on what it
looks like in GUIDE (unchecked or checked). It's never empty as far
as I know. If you don't want to use GUIDE, then maybe you can use it
to just create a sample GUI and then tell it to build an m-file from
it (File -> Export...) and look to see how it instantiates the
checkbox. Then use the code in your code where you tediously
construct a GUI from individual components. In fact I did that and
here is how it does it:

h2 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'Callback',@(hObject,eventdata)checkboxtest_export('checkbox1_Callback',hObject,eventdata,guidata(hObject)),...
'Position',[11.8 24.4615384615385 20.2 3.15384615384615],...
'String',{ 'Check Box' },...
'Style','checkbox',...
'Tag','checkbox1',...
'CreateFcn', {@local_CreateFcn, blanks(0), appdata} );

If I don't do anything to the check state (it starts out unchecked)
and just run it, and retrieve the value like this
checkboxValue = get(handles.checkbox1, 'Value')
it says that checkboxValue is a 1x1 double with a value of 0.

Subject: default value of check box

From: Rajesh

Date: 23 Apr, 2010 10:57:05

Message: 8 of 14

@Marc:
I am using the same thing (isempty).
@imageanalyst
I did not follow you. How can i use GUIDE only for a part of the GUI when all other controls are generated not through GUIDE. Can you elaborate.
Thanks anyways friends. I was stuck somewhere hence the delay.

Subject: default value of check box

From: Bruno Luong

Date: 23 Apr, 2010 11:53:21

Message: 9 of 14

"Rajesh " <rv_acharya@rediffmail.com> wrote in message <hof1o2$sug$1@fred.mathworks.com>...
>
> hi,
> sorry for the delay i am afraid i dint get a notification. anyway.
>
> may be my question was not clear. let me elaborate. I have a checkbox on a GUI. when i run the GUI i see the checkbox unchecked on the interface. Lets call it the default state. Now in the callback function of the box i have written
>
> global var1
> var1=get(hObject,'Value');
>
> further, I am using var1 as a condition to skip (if var1==0) or perform (if var1==1) a certain loop in another callback.
>

My guess is the problem caused by or missused of GLOBAL, it has nothing to do with checkbox state. The value of checkbox must be 0 or 1, never empty.

For example such error happens as following: "var1" would never set if you the callback is not invoked, so it remains empty. This is programming error because you don't fully understand the data flow of your program and GUI callback mechanism.

Bruno

Subject: default value of check box

From: Walter Roberson

Date: 23 Apr, 2010 16:15:28

Message: 10 of 14

Bruno Luong wrote:
> The value of checkbox must be 0 or 1, never
> empty.

It has been noted by some users that in at least some versions, if you
construct a checkbox without setting an initial value, then the value of
the checkbox when fetched will be the empty array.

My experience has been that if the checkbox is given an initial value
when constructed, or via set(), that the checkbox will then operate
normally, but there have been some claims that in at least some
versions, no matter what initial value is given to a checkbox, it will
be fetched as the empty array until there is user interaction with the
checkbox.

If I recall correctly, I encountered the uninitialized -> empty value
behaviour myself in at least one version.

Subject: default value of check box

From: Bruno Luong

Date: 23 Apr, 2010 16:35:05

Message: 11 of 14

Walter Roberson <roberson@hushmail.com> wrote in message <hqsh31$jo1$1@canopus.cc.umanitoba.ca>...
>
> It has been noted by some users that in at least some versions

Could be, but OP's ML version is 2007a and it does surely not suffer this bug according to my test (generated by either uicontrol or guide).

Bruno

Subject: default value of check box

From: Jan Simon

Date: 23 Apr, 2010 17:03:04

Message: 12 of 14

Dear Marc Crapeau!

> Just change your "if var1==0" with "if var1==0 | isempty(var1)"

Although the behaviour equals the expectations if var1 is the empty matrix, I would not use it. The replies for the following statements are equal, if var1 is empty:
  if var1 == 0 | isempty(var1), disp('TRUE'); end
  if var1 == 0 | ~isempty(var1), disp('TRUE'); end
Both evaluate:
  if [] | anything
which is equal to:
  if []

Better use:
  if any(var1)

However, Bruno hits the point: The OPs problem is not the checkbox, but his program uses a global variable, which is not initialized correctly if the callback of the checkbox has not been called (twice).
The solution is to create a possibility to identify the checkbox and check the value when the program starts - e.g. by storing its handle in the figures UserData or using the unique tag name of the checkbox.

Good luck, Jan

Subject: default value of check box

From: Walter Roberson

Date: 23 Apr, 2010 19:28:48

Message: 13 of 14

Bruno Luong wrote:
> Walter Roberson <roberson@hushmail.com> wrote in message
> <hqsh31$jo1$1@canopus.cc.umanitoba.ca>...
>>
>> It has been noted by some users that in at least some versions
>
> Could be, but OP's ML version is 2007a and it does surely not suffer
> this bug according to my test (generated by either uicontrol or guide).

I am unable to verify this myself, as our 2007a license files have been
cleaned up out of existence.

(Q: What's the difference between a hot young hacker and an
old-before-their-time software engineer? A: the hot young hacker says "If I
ever need this kind of code again, I'll re-write it from scratch: that'll be
faster and more interesting", whereas the worn software engineer says "Don't
delete that old tool; I may need it later to do regression testing or to
support a customer with an older version!, and I need to ensure that our
program produces the same answers for the old tool version and the new tool
version!)

What I can say is that Rakesh posted approximately March 29 specifically
indicaing he was having this difficulty with 2007a. That was the "Oh God, I
wish that Walter Roberson would tell me how to..." post.

Subject: default value of check box

From: Rajesh

Date: 3 May, 2010 08:52:06

Message: 14 of 14

Ahem ahem.... If i may dare to interrupt a family discussion,

@Bruno,
>For example such error happens as following: "var1" would never set if you the callback is >not invoked, so it remains empty. This is programming error because you don't fully >understand the data flow of your program and GUI callback mechanism.

My question: why would a user invoke the callback fn of a checkbox if it is in the desired state. i assume here that the callback is invoked only when i click the box. i've already said that when i do so it works but why should i be required to do it.
Now another question: is there another way of doing it? answer: i dont know (novice afterall) I am not trying to prove that its a bug. I dont care. I need to resolve this issue. period.

@Jan
>The solution is to create a possibility to identify the checkbox and check the value when the program starts - e.g. by storing its handle in the figures UserData or using the unique tag name of the checkbox.

thanks a million. Can u elaborate. how to store the handle in user data? OR How to use the unique tag name to set the value of my variable "var1" without interupting the checkbox?

@ Walter,
thanks for the support. Again, I dont wish to win an argument here (bug or no bug) just want a solution.
Wonder what happens to the "hot young hacker" when he grows old.
 

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us