Hi! I am making a simple gui, in the code below i am using the global variables rs ,rsh, a1,a2 . On execution these variables are not taking the value entered they turn to 0. Please advise.

global rs;
rs=double(get(handles.edit1,'Value'))
guidata(hObject,handles);
global rsh
rsh=double(get(handles.edit2,'Value'))
guidata(hObject,handles);
global a1;
a1=double(get(handles.edit3,'Value'))% for Shading
guidata(hObject,handles);
global a2;
a2=double(get(handles.edit4,'Value'))
guidata(hObject,handles);
Main(rs,rsh,a1,a2);
guidata(hObject,handles);

Answers (1)

SHAMBHAVI - it is unclear from your above code where each line fits into the GUI. I suspect that every three lines or so fits into a callback of some kind though it is unclear why
guidata(hObject,handles);
is repeated (since you never update the handles structure) or where the function Main gets called from.
As for why the global variables are initialized to zero when you try to make use of them, this usually happens when you try to make use of global variables before they have been initialized. This isn't the case for you. Instead the problem is that you are using the Value from the edit text control and not its String property.
If the Main function gets called in response to a push button callback event, then there is no need to use global variables to retrieve the variables. Just use the handles structure to retrieve the data from each edit text box.
For example,
function pushbutton1_Callback(hObject, eventdata, handles)
rs = str2double(char(get(handles.edit1,'String')));
rsh = str2double(char(get(handles.edit2,'String')));
a1 = str2double(char(get(handles.edit3,'String')));
a2 = str2double(char(get(handles.edit4,'String')));
Main(rs,rsh,a1,a2);
Note how the str2double function is used to convert each string input to a double. Try the above and see what happens!

Categories

Tags

Asked:

on 3 Jul 2015

Answered:

on 4 Jul 2015

Community Treasure Hunt

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

Start Hunting!