How to give output of one push button as input to another push button??

10 ビュー (過去 30 日間)
Shweta Mahajan
Shweta Mahajan 2019 年 4 月 16 日
コメント済み: Dennis 2019 年 4 月 16 日
im doing image processing code and using various push buttons. i need to carry the output of one pushbutton to another so that i can do further continues processsing. can you please help. i tried everthing but its not working.

回答 (1 件)

Dennis
Dennis 2019 年 4 月 16 日
It would be really helpful to get any information about what you have tried and why it is not working (wrong result, error messages?). Are you using guide, appdesigner or do you create your gui programmatically?
There are several ways to pass data between functions in Matlab, detailed information about this topic can be found here.
A MWE featuring guidata:
%this creates 2 buttons
for i=2:-1:1
pb(i)= uicontrol('style','pushbutton','string',sprintf('Button%d',i),'position',[20+80*i 50 80 40]);
pb(i).Callback={@pb_callback};
end
%button1 creates a 5x5 matrix with random values, button2 displays them
function pb_callback(hObj,~)
A=guidata(hObj); %<----retrieve data
if strcmp(hObj.String,'Button1')
A=randi(10,5,5);
else
disp(A)
end
guidata(hObj,A) %<-----store data
end
  2 件のコメント
Shweta Mahajan
Shweta Mahajan 2019 年 4 月 16 日
im using MATLAB gui for creating user interface and we tried using commands like,
handles.myVar = someValue;
guidata(hObject, handles); // for first push button and for next push button to access tha same data i used,
if isfield(handles,'myVar')
//my code
end
but its not working.
it shows error like "error in matlab.graphics.internal.figfile.figfile/read @(hobject eventdata)".
i have to use consecutive outputs for next process's input.
Dennis
Dennis 2019 年 4 月 16 日
Those code snippets are most likely not the cause of the error (probably would be easy to tell if you copied the entire error message). I am a little confused that there is no ',' between hobject and eventdata though.
However the example i posted earlier should work.
for i=2:-1:1
pb(i)= uicontrol('style','pushbutton','string',sprintf('Button%d',i),'position',[20+80*i 50 80 40]);
pb(i).Callback={@pb_callback};
end
function pb_callback(hObj,~)
handles=guidata(hObj); %<----retrieve data
if strcmp(hObj.String,'Button1')
handles.myVar=randi(10);
else
if isfield(handles,'myVar')
disp(handles.myVar)
end
end
guidata(hObj,handles) %<-----store data
end

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLarge Files and Big Data についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by