How can I use Textbox input from GUI in a separate script?

I want to use the input of the textbox of my gui which i have created with guide, in a seperate Matlab script, not that was which creates callbacks.
So I want to save the input into a variable and then I want to work with this variable in my own matlab script (main).

3 commentaires

you don't have to put in the same script you just have to write the name of the file and the program will automatically call it where it is
You really should consider converting it to a function though rather than a script.
Megan's answer moved here as a comment
But my main has 600 line of code I can not put it in the same script

Connectez-vous pour commenter.

Réponses (2)

you can simply call the function at the end, without the extension .m and it must be in the same folder of the graphical interface files:
% --- Executes on button press in execute.
function execute_Callback(hObject, eventdata, handles)
% hObject handle to execute (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
minVelocity = str2double(get(handles.minVelocity, 'string'));
maxVelocity = str2double(get(handles.maxVelocity, 'string'));
main

1 commentaire

I tried what you said but it didnt work. it does not see minVelocity as the same variable of the GUI.

Connectez-vous pour commenter.

Adam Danz
Adam Danz le 5 Déc 2019
Modifié(e) : Adam Danz le 6 Déc 2019
"I want to use the input of the textbox of my gui which i have created with guide, in a seperate Matlab script"
There are several ways to share data stored in a GUI with an external file. This method below involves saving the data from the callback function to the GUI figure and then extracting that data from the external function [background info].
function execute_Callback(hObject, eventdata, handles)
% hObject handle to execute (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
minVelocity = str2double(get(handles.minVelocity, 'string'));
maxVelocity = str2double(get(handles.maxVelocity, 'string'));
% Store the data in the "UserData" property of the GUI Figure
% This assumes that the hObject is a child of the GUI figure
data.minVelocity = minVelocity;
data.amxVelocity = maxVelocity;
hObject.Parent.UserData = data;
Before you can extract this data form the external function, you must give your GUI a meaningful tag so it can be found by the external function. You can set the "Tag" property of the GUI figure from within GUIDE or from within your startup function. The code below assumes you've assigned the tag MyUniqueGuiTag but you should repace that with a more meaningful name.
From the external function....
% Find the gui using your unique tag
guiHandle = findobj('Type','Figure','Tag','MyUniqueGuiTag');
% Get the stored UserData
data = guiHandle.UserData
This hasn't been tested (I don't have Matlab opened right now). If you have any questions or problems that you can't resolve please leave a comment below.

10 commentaires

I didnt get the last part with the Tag. so i have to paste this part of code or is ir already generated?
Hi Megan,
From my answer,
Before you can extract this data form the external function, you must give your GUI a meaningful tag so it can be found by the external function. You can set the "Tag" property of the GUI figure from within GUIDE or from within your startup function.
Most graphics objects have a "Tag" property where you can set some kind of identifyer for that object making it easy to find. There are two ways to do this in your GUIDE GUI.
1) in you're GUI's startup function.
% "handles.Figure" is the handle to your GUI figure.
% You'll have to replace that with your actual GUI figure handle.
handles.Figure.Tag = 'MyUniqueGuiTag'; % you can create any tag you want!
2) From within GUIDE. Open your GUI in GUIDE, select the figure backgound and set the "Tag" property of the figure to 'MyUniqueGuiTag'. Then save.
Let me know if you have any troubles.
I copied this part into my call back. It didnt work but i think i did ist wrong
% Find the gui using your unique tag
guiHandle = findobj('Type','Figure','Tag','MyUniqueGuiTag');
% Get the stored UserData
data = guiHandle.UserData
Oh I just saw your comment. let me see if it will work. I will let you know
It should work after you assign the matching tag to your gui figure.
Megan
Megan le 12 Déc 2019
Modifié(e) : Adam Danz le 12 Déc 2019
I get this error massage
No appropriate method, property, or field 'UserData' for class 'matlab.graphics.GraphicsPlaceholder'.
Error in Filter>execute_Callback (line 177)
data = guiHandle.UserData;
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in Filter (line 49)
gui_mainfcn(gui_State, varargin{:});
Error in
matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)Filter('execute_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback.
Open you GUI's m-file. Toward the top you should see a function named something like myGUI_OpeningFcn(...). The "muGIU" part will be replaced with your GUI's name.
That's the opening function. Add the tag at the end of that function.
Regarding your error, you're mixing up where the chunks of code should go.
Take a few minutes to understand what's going on with that code so it makes sense to you. That way you can implement it more easily.
This line below is used to extract the data from the GUI after it's already been saved. As my answer explains, that's called from the external function (or an function, really).
data = guiHandle.UserData;
The basic steps are
  1. Assign the tag to your GUI
  2. Assign the data to your GUI Figure using the 1st chunk of code from my answer.
  3. Use the 2nd chunk of code from my answer to extract the saved data from an external function.
Megan
Megan le 13 Déc 2019
Modifié(e) : Megan le 13 Déc 2019
Okay thank you i did it how you told me.
How can I now use the extracted data?
for example the minVelocity in an other matlab script but same project
Your main question asks how to extract a variable from a GUI into a separate script. The 2nd block of code in my answer does that. I'm not sure what you mean by "how can I use the extracted data?". Once it's extracted you can do anything you want with it.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Creating, Deleting, and Querying Graphics Objects dans Centre d'aide et File Exchange

Question posée :

le 5 Déc 2019

Modifié(e) :

le 19 Fév 2020

Community Treasure Hunt

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

Start Hunting!

Translated by