Thread Subject: If Condition Issue - R2009b

Subject: If Condition Issue - R2009b

From: PinkLab

Date: 29 Oct, 2009 19:45:20

Message: 1 of 14

I have entered a push button callback

if handles.radiobutton1 == get(hObject,'Max')
% do this task
end

if handles.radiobutton2 == get(hObject,'Max')
% do this task
end

if handles.radiobutton3 == get(hObject,'Max')
% do this task
end


When push button is pressed, it goes through all three task and doesn't check which radiobutton is Max

Any help or alternate way to approach this issue??

Subject: If Condition Issue - R2009b

From: Matt Fig

Date: 29 Oct, 2009 20:37:01

Message: 2 of 14

The way I read your code is this:

switch get(hObject,'Max')
case handles.radiobutton1
      % Do one thing.
case handles.radiobutton2
      % Do another.
case handles.radiobutton3
      % Do yet a third.
end


Now, did you at some point prior to pushing the pushbutton set the 'max' property of hObject to a handle of one of the radiobuttons? If not, there will be no match. Why are you doing this anyway? What are you trying to accomplish?

Subject: If Condition Issue - R2009b

From: Steven Lord

Date: 29 Oct, 2009 21:21:11

Message: 3 of 14


"PinkLab " <hussains_k@hotmail.com> wrote in message
news:hccrcg$1ju$1@fred.mathworks.com...
>I have entered a push button callback
>
> if handles.radiobutton1 == get(hObject,'Max')
> % do this task
> end
>
> if handles.radiobutton2 == get(hObject,'Max')
> % do this task
> end
>
> if handles.radiobutton3 == get(hObject,'Max')
> % do this task
> end
>
>
> When push button is pressed, it goes through all three task and doesn't
> check which radiobutton is Max

It does what you told it to do (check to see if one of the handles of the
radiobuttons has the same numeric values as the Max property of the object
whose handles is hObject) not what I suspect you want it to do (check the
Value property of the radiobutton to determine if it is the same as the
value of the Max property of the object whose handle is hOjbect.) To do the
latter, use:

if isequal(get(handles.radiobutton, 'Value'), get(hObject, 'Max'))
% do something
end

and similarly for the other two.

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ

Subject: If Condition Issue - R2009b

From: PinkLab

Date: 1 Nov, 2009 13:49:01

Message: 4 of 14

Thanks Steve, that worked. I am trying to compare two text boxes but for some how reason it is not working as i want it to...

user_entry = str2double(get(hObject,'string'));
if user_entry == handles.editbox1
% disply this message
end

but it is not displaying any error message
Is there any other way to check the value?


"Steven Lord" <slord@mathworks.com> wrote in message <hcd0uk$los$1@fred.mathworks.com>...

Subject: If Condition Issue - R2009b

From: ImageAnalyst

Date: 1 Nov, 2009 15:40:10

Message: 5 of 14

On Nov 1, 8:49 am, "PinkLab " <hussain...@hotmail.com> wrote:
> Thanks Steve, that worked. I am trying to compare two text boxes but for some how reason it is not working as i want it to...
>
> user_entry = str2double(get(hObject,'string'));
> if user_entry == handles.editbox1
> % disply this message
> end
>
> but it is not displaying any error message
> Is there any other way to check the value?
>
>
>
> "Steven Lord" <sl...@mathworks.com> wrote in message <hcd0uk$lo...@fred.mathworks.com>...- Hide quoted text -
>
> - Show quoted text -

------------------------------------------------
This does not compare the contents of two edit text boxes. You're
taking one (editbox2 I presume) and converting it to double, but then
comparing it to the HANDLE of the editbox1, NOT the contents of
editbox1. The handle is not the same as the characters that the user
typed inside of the edit box. You would need to convert the contents
of editbox1 to double just as you for the other editbox.

Plus check out this on the FAQ:
http://matlabwiki.mathworks.com/MATLAB_FAQ#Why_is_0.3-0.2-0.1_not_equal_to_zero_.28or_similar.29.3F
which is useful information if you're testing for exact matches
between floating point numbers. Maybe you should just compare the
text strings (after trimming off any white space) using strcmpi()
instead of converting to doubles.

Subject: If Condition Issue - R2009b

From: PinkLab

Date: 1 Nov, 2009 16:15:21

Message: 6 of 14

Yes, right...but that went over my head... could you please correct my syntax? thanks

Subject: If Condition Issue - R2009b

From: ImageAnalyst

Date: 1 Nov, 2009 16:48:05

Message: 7 of 14

On Nov 1, 11:15 am, "PinkLab " <hussain...@hotmail.com> wrote:
> Yes, right...but that went over my head... could you please correct my syntax? thanks

How about something like this (untested)
text1 = get(handles.editbox1, 'String');
text1(isspace(text1)) = []; % Remove all spaces.

text2 = get(handles.editbox2, 'String');
text2(isspace(text2)) = []; % Remove all spaces.

if strcmpi(text1, text2)
  % Will equal a logical true (a 1) if they match
  msgbox('They match');
else
  % Will equal a logical false (a 0) if they do not match
  msgbox('They do not match');
end

Subject: If Condition Issue - R2009b

From: PinkLab

Date: 1 Nov, 2009 17:55:19

Message: 8 of 14

It is good for two comparison but i have more edit boxes to compare with each others.

any similar solution for comparing multiple edit boxes?

Subject: If Condition Issue - R2009b

From: ImageAnalyst

Date: 1 Nov, 2009 18:06:52

Message: 9 of 14

On Nov 1, 12:55 pm, "PinkLab " <hussain...@hotmail.com> wrote:
> It is good for two comparison but i have more edit boxes to compare with each others.
>
> any similar solution for comparing multiple edit boxes?

------------------------------------------------
Yes. Just expand it for more edit boxes. You could make it a
function if you want where you just pass in the two handles that you
want to compare.

function theyMatch = CompareEditBoxes(handle1, handle2)
  text1 = get(handle1, 'String');
  text1(isspace(text1)) = []; % Remove all spaces.

  text2 = get(handle2, 'String');
  text2(isspace(text2)) = []; % Remove all spaces.

  theyMatch = strcmpi(text1, text2) ;
  return; % from CompareEditBoxes()

Then you just call it a bunch of times:
% See which of 4 edit fields match.
match12 = CompareEditBoxes(handles.editbox1, handles.editbox2);
match13 = CompareEditBoxes(handles.editbox1, handles.editbox3);
match14 = CompareEditBoxes(handles.editbox1, handles.editbox4);
match23 = CompareEditBoxes(handles.editbox2, handles.editbox3);
match24 = CompareEditBoxes(handles.editbox2, handles.editbox4);
match34 = CompareEditBoxes(handles.editbox3, handles.editbox4);

Subject: If Condition Issue - R2009b

From: PinkLab

Date: 1 Nov, 2009 18:57:00

Message: 10 of 14

I tried but unfortunately didn't work with no error messages.

I have declared the function as mentioned and then i have entered the

match12 = CompareEditBoxes(handles.editbox1, handles.editbox2);
match13 = CompareEditBoxes(handles.editbox1, handles.editbox3);
match14 = CompareEditBoxes(handles.editbox1, handles.editbox4);
match23 = CompareEditBoxes(handles.editbox2, handles.editbox3);
match24 = CompareEditBoxes(handles.editbox2, handles.editbox4);
match34 = CompareEditBoxes(handles.editbox3, handles.editbox4);

in edit box callbacks

Subject: If Condition Issue - R2009b

From: ImageAnalyst

Date: 1 Nov, 2009 19:43:04

Message: 11 of 14

No error messages - that's good. Exactly what didn't work? How do
you know it didn't work? Did you set break points at each line and
see if match12, etc. were either true or false?

The edit box callbacks only get called once focus leaves the edit
box. Did you know that? So if you type abc into edit box 1 and then
click inside edit box 2, then at that point the callback for 1 will
execute. Now if you type abc in the edit field 2, nothing will happen
immediately even though they match. This is because MATLAB does not
expose the "Change" event for the field so you can't check it as you
type. You can only execute the callback for edit box 2 if you click
somewhere else, such as on a push button or back in edit box 1.

What are you going to do next, assuming you've correctly figured out
if two edit fields have identical contents or not?

Subject: If Condition Issue - R2009b

From: PinkLab

Date: 1 Nov, 2009 21:05:19

Message: 12 of 14

well it does says match12 is unused variable tho...

i do need to put that under edit box callback so it validate and compare the value of the boxes when entered

Subject: If Condition Issue - R2009b

From: PinkLab

Date: 3 Nov, 2009 15:34:02

Message: 13 of 14

if user_entry < handles.editbox1
% display error message
end

this works find but when the value is even greater than editbox1 it still show the error message... any way round or another way to write the same code?

Subject: If Condition Issue - R2009b

From: ImageAnalyst

Date: 3 Nov, 2009 16:29:25

Message: 14 of 14

On Nov 3, 10:34 am, "PinkLab " <pink...@pinklab.com> wrote:
> if user_entry < handles.editbox1
> % display error message
> end
>
> this works find but when the value is even greater than editbox1 it still show the error message... any way round or another way to write the same code?

----------------------------------------------------------------------------------------------------
I don't see how that can work. Why are you comparing user_entry,
which is presumably a string or number, to a handle, which is just the
ID number of the graphical element? Why did you ignore the way I told
you to do it that would work and go back to your original way which I
told you would not work???????????????????????

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

rssFeed for this Thread

Contact us at files@mathworks.com