How to pass a value to a function which is external to GUI?

I have designed a program to calculate values and display graphs using multiple functions and global variables. the code works very fine. Now i want to add a GUI to make it more user friendly but cannot pass values and strings into the functions that are external to the GUI. Am using a pushbotton to invoke the function and pass values to it.
My Push button callback looks like this:
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
Any method on how to go by this?

Answers (1)

What constraints are blocking passing values in to the external functions? Do the external functions not take parameters ?

4 Comments

Thank you. They do take parameters. This is the how i passed the values from the GUI push button using a switch
function pushbutton1_Callback(hObject, eventdata, handles)
% declaring global functions
global patchr_config;
global design_patchr;
global rect_array;
global patchr;
% passing variables from other functions in de same GUI
Ny = get(handles.yNum,'Value');
Nx = get(handles.xNum,'Value');
Sy = str2double(get(handles.ySpacing,'String'));
Sx = str2double(get(handles.xSpacing,'String'));
Er = str2double(get(handles.dConstant,'String'));
h = str2double(get(handles.thickness,'String'));
freq1 = str2double(get(handles.freq,'String'));
freq_config = freq1 * 1e6; %mutiplying by Mega Hertz
Ang = 0; % Initialize Ang to 0
Pattern = get(handles.geom, 'Value'); % obtaining value from 'geom' function
% the switch function
switch Pattern
case 1
patchr_config = design_patchr(Er,h,freq_config);
rect_array(Nx,Ny,Sx,Sy,'patchr',Ang); % Define Square array
....
My error message after running the GUI and imputing the various parameters:
Attempted to access design_patchr(1,3,1.222e+09); index out of bounds
because size(design_patchr)=[0,0,1].
Error in Planer>pushbutton1_Callback (line 236)
patchr_config = design_patchr(Er,h,freq_config);
Error in gui_mainfcn (line 96)
feval(varargin{:});
Error in Planer (line 43)
gui_mainfcn(gui_State, varargin{:});
Error in
@(hObject,eventdata)Planer('pushbutton1_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback
Like it says, your index is out of range. Are you sure you want to be indexing the array design_patchr with a third index that is in the range of millions ? Or are you expecting that design_patchr is a function handle?
Where did you initialize design_patchr? In that routine did you remember to declare design_patchr as global ?
Thank you Walter.
design_patchr is a function which I have initialized outside the GUI as global. It receives (Er,h,freq_config) and does some other computations and returns the value to another function called patchr_config which also does some computations and returns values to another function.
Maybe I am not getting your queries right. But is it possible to pass values to an external function that is of itself global?
I learnt about GUIdata but don't know how to incorporate it into the programe
But the other option I was thinking about is to run the main m.file from the push button.
How about that also?

Sign in to comment.

Categories

Find more on App Building in Help Center and File Exchange

Products

Asked:

on 25 Jan 2014

Commented:

on 25 Jan 2014

Community Treasure Hunt

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

Start Hunting!