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

10 次查看(过去 30 天)
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 个评论
Adam
Adam 2019-12-5
You really should consider converting it to a function though rather than a script.
Adam Danz
Adam Danz 2019-12-5
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

请先登录,再进行评论。

回答(2 个)

JESUS DAVID ARIZA ROYETH
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 个评论
Megan
Megan 2019-12-5
I tried what you said but it didnt work. it does not see minVelocity as the same variable of the GUI.

请先登录,再进行评论。


Adam Danz
Adam Danz 2019-12-5
编辑:Adam Danz 2019-12-6
"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 个评论
Megan
Megan 2019-12-13
编辑:Megan 2019-12-13
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
Adam Danz
Adam Danz 2019-12-13
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Migrate GUIDE Apps 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by