save variable from callback function of GUI

13 ビュー (過去 30 日間)
Fabio Righetti
Fabio Righetti 2019 年 5 月 5 日
回答済み: Murugan C 2019 年 5 月 5 日
I'm trying to build a simple gui for my program, now i have to save the user input, but i can't retrieve data from callback function.
h.d = figure('Position',[300 300 400 200],'Name','Elaborazione di Mesh poligonali'); % position = [ x y xDim yDim ]
h.txt1 = uicontrol('Parent',h.d,...
'Style','text',...
'Position',[20 150 120 20],...
'String','Inserisci nome:');
h.txtBox1 = uicontrol('Parent',h.d,...
'Style','edit',...
'Position',[140 150 50 20],...
'String','Esempio',...
'Callback',@store_img);
h.btnOk = uicontrol('Parent',h.d,...
'Style','pushbutton',...
'Position',[85 20 70 25],...
'String','Avanti',...
'Callback',@btn_ok);
uiwait;
disp(h.txtBox1.UserData)
% Salvataggio input dell'utente
function store_img(hObject, event)
% hObject handle to pushbutton1 (see GCBO)
% event reserved - to be defined in a future version of MATLAB
data = hObject.String;
hObject.UserData = data;
end
function btn_ok( hObject, event)
close(gcf);
end
Command Window:
Invalid or deleted object.
Error in prova (line 20)
disp(h.txtBox1.UserData)

採用された回答

Murugan C
Murugan C 2019 年 5 月 5 日
I think, we can get the vaiable using global variable. so we first decalre a gloabal varible ,here "data" is decared as lobal .
try this code.
function simple_gui
global data; % declare global varibale
h.d = figure('Position',[300 300 400 200],'Name','Elaborazione di Mesh poligonali'); % position = [ x y xDim yDim ]
h.txt1 = uicontrol('Parent',h.d,...
'Style','text',...
'Position',[20 150 120 20],...
'String','Inserisci nome:');
h.txtBox1 = uicontrol('Parent',h.d,...
'Style','edit',...
'Position',[140 150 50 20],...
'String','Esempio',...
'Callback',@store_img);
h.btnOk = uicontrol('Parent',h.d,...
'Style','pushbutton',...
'Position',[85 20 70 25],...
'String','Avanti',...
'Callback',@btn_ok);
uiwait(gcf);
disp(data)
end
% Salvataggio input dell'utente
function store_img(hObject, event)
% hObject handle to pushbutton1 (see GCBO)
% event reserved - to be defined in a future version of MATLAB
global data
data = get(hObject,'String');
set(hObject,'UserData', data)
end
function btn_ok( hObject, event)
close(gcf);
end

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMigrate GUIDE Apps についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by