For Programmers: Free Programming Magazines  


Home > Archive > Matlab > January 2008 > About Pushbuttons and Callbacks









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author About Pushbuttons and Callbacks
Mafalda

2008-01-23, 7:54 pm

Hi people,
I am creating a simple GUI. I need a pushbutton that when
pressed, should call a function.
A part of my code in one script called Test.m is:
global data1
global data2
global data3
global data4
push_Ok=uicontrol(fig,'Style','push','Po
sition',[50 50 50
50],'String','OK','Callback','myfunc
(data1,data2,data3,data4)');

In other script called myfunc.m I have the instructions
that my function should do.
The first line of this second file is:
funtion[output]=myfunc(input,data2,data3
,data4)

My problem is that when I have tested myfunc.m with values
from other text file, the function has worked perfectly,
but when I have run the program Test.m
I have two error messages saying:
"Subscript indices must either be real positive integers or
logicals".
"Error while evaluating uicontrol Callback"

My questions are:
Should I declare all indices that I use in myfunc.m as
global in the file Test.m?
Should I put any other instruction in my file Test.m like
eval('callback') or declare myfunc as global in the Test.m
script to solve the second error?

Many thanks in advance
Steven Lord

2008-01-23, 7:54 pm


"Mafalda " <mafalda3876.nospam@mathworks.com> wrote in message
news:fn7ne0$8js$1@fred.mathworks.com...
> Hi people,
> I am creating a simple GUI. I need a pushbutton that when
> pressed, should call a function.
> A part of my code in one script called Test.m is:
> global data1
> global data2
> global data3
> global data4
> push_Ok=uicontrol(fig,'Style','push','Po
sition',[50 50 50
> 50],'String','OK','Callback','myfunc
> (data1,data2,data3,data4)');


Note that when you specify the callback like this, the string is evaluated
as though you typed it at the command line. Your variables data1, data2,
data3, and data4 don't exist in that scope unless you defined them to be
available in the base workspace, so it will likely error.

*snip*

> My questions are:
> Should I declare all indices that I use in myfunc.m as
> global in the file Test.m?


Global variables can be dangerous.

> Should I put any other instruction in my file Test.m like
> eval('callback') or declare myfunc as global in the Test.m
> script to solve the second error?


Assuming you're using a version that has anonymous functions, I'd use them.

data1 = 1;
data2 = 2;
data3 = 3;
data4 = 4;
push_Ok=uicontrol(fig,'Style','push','Po
sition',[50 50 50
50],'String','OK','Callback', @(h, eventdata)
myfunc(data1,data2,data3,data4));

If you're using an older version, I'd store the data in the handles
structure and use GUIDATA/GUIHANDLES inside MYFUNC to extract the necessary
data.

--
Steve Lord
slord@mathworks.com


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com