integrating a gui with a program

function varargout = interface(varargin)
function interface_OpeningFcn(hObject, eventdata, handles, varargin)
function varargout = interface_OutputFcn(hObject, eventdata, handles)
function Vel_Callback(vel, eventdata, handles)
function Vel_CreateFcn(vel, eventdata, handles)
this is the gui structure I have created, inside each function there is more code (some of them automatically created by MatLab) the idea is that I want to select a value of velocity from a pop up menu and I want to use the value chosen in a big program with tons of calculations where I must introduce the program to have the results of the program in the workspace
the idea a what to develop is 1.introduce some values with pop-up menus transfer the values to the workspace and there use them as constant for the program I have developed in other script
2. used the values in some calculation
I know is tricky but I have to programs a GUI and m file and I don't know how I can integrate them together

 Accepted Answer

Jan
Jan on 25 Jan 2017
Do not integrate the GUI and the calculations. Exporting variables to the base workspace is not clean and safe also. Prefer keeping the values inside the GUI, e.g. by using guidata, setappdata or by setting the UserData of the figure or GUI elements. Then start the calculations by hitting a "Start" button. Now the GUI extracts the values (guidata, getappdata or obtaining the UserData) and creates the input arguments for the function.
This startegy allows to modify the GUI afterwards without touching the calculations and vice versa. The base workspace is not polluted, such that you can open several GUIs without the danger of confusion.

5 Comments

you mean create push button to start, and inside the callback function you write the code(the programm). and is possible that variables of this code are values you selected from the pop-up menu? and the code generate a lot of values these values will be available in the workspace?
I would write all code for the calculations in a separate M-function. This allows to call it e.g. from a batch job also with a bunch of automatically created variables.
Then the GUI is used to create the inputs only:
function MainGUI
H.Fig = figure('Position', [100, 100, 640, 480]);
H.Pop1 = uicontrol('Style', 'Popupmenu', ...
'Position', [10, 10, 200, 25], ...
'String', {'1', '2', '3'});
H.Pop2 = uicontrol('Style', 'Popupmenu', ...
'Position', [10, 40, 200, 25], ...
'String', {'100', '200', '300'});
H.Button = uicontrol('Style', 'Pushbutton', ...
'Position', [10, 400, 80, 25], ...
'String', 'Start', ...
'Callback', @StartCB);
guidata(H.Figure, H);
end
function StartCB(ButtonH, EventData)
H = guidata(ButtonH);
Pop1Value = get(H.Pop1, 'Value');
Pop1String = get(H.Pop1, 'String');
Pop1Number = sscanf(Pop1String{Pop1Value}, '%g');
Pop2Value = get(H.Pop2, 'Value');
Pop2String = get(H.Pop2, 'String');
Pop2Number = sscanf(Pop2String{Pop1Value}, '%g');
Result = YourCalculations(Pop1Number, Pop2Number);
disp(Result) % How ever you want to show the results
end
% And now a dummy for the calculations:
function Result = YourCalculations(a, b)
Result = a ^ 2 + b ^ 2;
end
Now you can call your calculations from either the GUI or inside a loop or whatever. The GUI part can be changed according to your taste or completely redesigned, e.g. by using GUIDE or the AppDesigner. The idea is to separate the GUI from the calculations, and the program from the data. This strategy increases the flexibility.
thank you very much for your time the idea is this but in my case, I would like to have all the results of the 'dummy function' in the workspace I understand that you call the third function in the second one but the results of the third function are not display in the workspace. the variable result from the third function is not display in the workspace. thanks again :)
The question is: which workspace? Each function has its own workspace and the "Result" is caught inside the callback of the button. This would be the right location to store it in the figure:
Result = YourCalculations(Pop1Number, Pop2Number);
disp(Result) % How ever you want to show the results
H.Result = Result;
guidata(ButtonH, H);
Then you can obtain the Result from the outside, when the figure can be found e.g. by the tag:
FigH = findobj(allchild(groot), 'flat', 'Tag', 'myGUITag');
H = guidata(FigH);
Result = H.Result;
Or if you really have good reasons to pollute the base workspace (the one, which is visible in the command window), you can do this by:
Result = YourCalculations(Pop1Number, Pop2Number);
assignin('base', 'Result', Result);
Brrrr. The same problem as global variables: You have no chance to debug this efficiently and when you e.g. open several GUIs which create the same variable "Result", there is no chance to check, which GUI has created the final value. But of course, if you never get any confusions and open a single GUI only and are really sure, that you want to do this (and keep the warnings in your mind), assignin solves your problem.
thank you very much Jan, I will try to work on it :)

Sign in to comment.

More Answers (1)

Patrick Brown
Patrick Brown on 28 Jan 2017
hi jan in the example above I am trying to understand how you connect both functions with the function guidata guidata(H.Figure, H); function StartCB(ButtonH, EventData) H = guidata(ButtonH); how it works

1 Comment

@Patrick: guidata(H, Data) uses setappdata internally to store the variable "Data" in the 'ApplicationData' property of the GUI object with the handle H. To obtain the stored data use: Data = guidata(H), which calls getappdata internally. For further explanations and examples read doc guidata.

Sign in to comment.

Categories

Find more on App Building in Help Center and File Exchange

Asked:

on 25 Jan 2017

Commented:

Jan
on 28 Jan 2017

Community Treasure Hunt

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

Start Hunting!