use a custom made gui in another .m file

i created a gui file and i want to run it in another m file such that if questdlg is one of the 3 like blue it will run the gui and save the answers from the gui into the m file to be further evaluated.
the help would be greatly apreciated
for instance
.m script that calls the gui
cars=0;red=0;
cars=questdlg('color of car','Select','Red','blue','c');
switch cars
case 'Red'||'Blue'
cargui
end
disp(carguiredqt)
disp(carguiblueqt)
cartot=carguiredqt+carguiblueqt
within the gui are checkboxes that ask for the colors when clicked a edit box becomes visible asking for the quantity of that color. this is the value i want to use in the other script.
ive got all the values i want returned in a push button callback so when the button is pushed it gets the values entered i just dont know how to get them out of the button press to the other script to use
then if it is red or blue it will call the gui i tried using call and run. and i want to take whatever values entered in the gui and continue runing them in the m file.

2 commentaires

Walter Roberson
Walter Roberson le 28 Mai 2021
Was the GUI created with GUIDe or with App Designer?

Connectez-vous pour commenter.

Réponses (2)

Walter Roberson
Walter Roberson le 28 Mai 2021

0 votes

In GUIDE, you will need to go into cargui.m and uncomment the uiwait() or waitfor() in the Open Function Callback, and you will need to make sure that whatever value you want to have returned is selected in the Output Function Callback.
Then in your other gui, you will need to assign the output of cargui to a variable.
Normally when you call a GUIDE GUI, the output is the figure number, and the GUI does not wait, but there is commented code in the Open Function Callback to make it wait, and in that case what gets returned is controlled by the Output Function Callback.

12 commentaires

Joseph Catanese
Joseph Catanese le 28 Mai 2021
it said dot indexing is not suported for variables of this type
Joseph Catanese
Joseph Catanese le 28 Mai 2021
Modifié(e) : Joseph Catanese le 28 Mai 2021
only when i put the uiwait in it
Joseph Catanese
Joseph Catanese le 28 Mai 2021
ive got all the values i want returned in a push button callback so when the button is pushed it gets the values entered i just dont know how to get them out of the button press to the other script to use
Walter Roberson
Walter Roberson le 28 Mai 2021
Modifié(e) : Walter Roberson le 28 Mai 2021
Usually you would store the the values to handles.output . For example, taken from working code:
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)
handles.output = get(hObject,'String');
% Update handles structure
guidata(hObject, handles);
% Use UIRESUME instead of delete because the OutputFcn needs
% to get the updated handles structure.
uiresume(handles.figure1);
Joseph Catanese
Joseph Catanese le 28 Mai 2021
Modifié(e) : Joseph Catanese le 28 Mai 2021
function carok_Callback(hObject, eventdata, handles)
guidata(hObject,handles);
clc
carred=get(handles.chkcred,'Value'); % car color
carblue=get(handles.chkcblue,'Value');
cargreen=get(handles.chkcgreen,'Value');
if carred==1 %if the box is checked or not
redstr=get(handles.chkcred,'string');% the name of the box
redqt1=get(handles.redqt,('string'));% how many red cars
redqt=str2double(redqt1) %how many red cars turned to number
end
if carblue==1
bluestr=get(handles.chkcblue,'string');
blueqt1=get(handles.blueqt,('string'));
blueqt=str2double(blueqt1)
end
if cargreen==1
greenstr=get(handles.chkcgreen,'string');
greenqt1=get(handles.greenqt,('string'));
greenqt=str2double(greenqt1)
end
closereq()
this is what i have and i want to get the redqt, blueqt, greenqt values and use them in another script file how would i do that or store them
function carok_Callback(hObject, eventdata, handles)
results = struct('redqt', nan, 'greenqt', nan, 'blueqt', nan);
if get(handles.chkcred,'Value') %if the box is checked or not
redqt1 = handles.redqt.String;% how many red cars
results.redqt = str2double(redqt1) %how many red cars turned to number
end
if get(handles.chkcblue,'Value')
blueqt1 = handles.blueqt.String;
results.blueqt = str2double(blueqt1)
end
if get(handles.chkcgreen,'Value')
greenqt1 = handles.greenqt.String;
results.greenqt = str2double(greenqt1)
end
handles.output = results;
uiresume(ancestor(hObject, 'figure'))
The output that will be returned will be a struct with fields redqt, blueqt, greenqt . The values for the boxes not checked will be nan; the values will also be nan if the user entered something that was not a valid number.
Joseph Catanese
Joseph Catanese le 29 Mai 2021
thanks ill try that and how to i call it into another script to use for calcuations and stuff?
cars = questdlg('color of car','Select','Red','blue','c');
switch cars
case {'Red', 'Blue'}
result = cargui;
otherwise
result = struct('redqt', nan, 'blueqt', nan, 'greenqt', nan);
end
carguiredqt = result.redqt;
carguiblueqt = result.blueqt;
carguigreenqt = result.greenqt;
disp(carguiredqt)
disp(carguiblueqt)
cartot = sum([carguiredqt,carguiblueqt,carguigreenqt], 'omitnan');
Joseph Catanese
Joseph Catanese le 29 Mai 2021
yea its not working i cant figure it out lol thank you though
Walter Roberson
Walter Roberson le 29 Mai 2021
What error are you getting?
Joseph Catanese
Joseph Catanese le 29 Mai 2021
i cant get the variables to get pulled to the script but i can get the fig to work
Joseph Catanese
Joseph Catanese le 1 Juin 2021
Modifié(e) : Joseph Catanese le 1 Juin 2021
cars = questdlg('color of car','Select','Red','blue','c');
switch cars
case {'Red', 'Blue'}
result = cargui;%-------This
otherwise
result = struct('redqt', nan, 'blueqt', nan, 'greenqt', nan);
end
carguiredqt = result.redqt;
carguiblueqt = result.blueqt;
carguigreenqt = result.greenqt;
disp(carguiredqt)
disp(carguiblueqt)
cartot = sum([carguiredqt,carguiblueqt,carguigreenqt], 'omitnan');
@Walter Roberson on this i set it up like this but when it runs it it shows the answer for carguiredqt before it allows me to enter a number in the figure.

Connectez-vous pour commenter.

Image Analyst
Image Analyst le 29 Mai 2021

0 votes

You need to call guidata() at the end of the function, not the beginning. guidata() is needed anytime you change a field of handles that is not connected to a specific control on the form, like handles.output -- it's just a field, not a widget on the GUI so you need guidata() after you make a change to handles.output.

Catégories

En savoir plus sur App Building dans Centre d'aide et File Exchange

Produits

Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by