integrating a gui with a program

4 visualizzazioni (ultimi 30 giorni)
Patrick Brown
Patrick Brown il 25 Gen 2017
Commentato: Jan il 28 Gen 2017
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

Risposta accettata

Jan
Jan il 25 Gen 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 Commenti
Jan
Jan il 25 Gen 2017
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.
Patrick Brown
Patrick Brown il 25 Gen 2017
thank you very much Jan, I will try to work on it :)

Accedi per commentare.

Più risposte (1)

Patrick Brown
Patrick Brown il 28 Gen 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 Commento
Jan
Jan il 28 Gen 2017
@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.

Accedi per commentare.

Categorie

Scopri di più su Environment and Settings in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by