using output of one button as an input in another button

조회 수: 7 (최근 30일)
ali hassan
ali hassan 2022년 1월 28일
댓글: Rik 2022년 1월 28일
first i am using following command to get path and filename:
[filename,pathname] = uigetfile('*txt');
then i want to give these output (pathname and filename) as an input to another push button. how can i do so to link it?
  댓글 수: 3
DGM
DGM 2022년 1월 28일
How and whether it's practical depends entirely on how your code is structured and what you're trying to do. If you're trying to call another CBF directly without a button press, then you'd just pass it like you would any other function. If you're not trying to call the CBF directly, it probably wouldn't make sense to pass arguments directly like that.
I imagine most people might recommend using guidata, but I tend to use shared variables in GUI code.

댓글을 달려면 로그인하십시오.

채택된 답변

Rik
Rik 2022년 1월 28일
If you really want to, you can call the callback function like any other function.
However, it makes much more sense to only use buttons to call functions. There is no rule against two callbacks ending up calling the same function. Something like this:
function callback1(hObject,eventdata)
[filename,pathname] = uigetfile('*txt');
filename=fullfile(pathname,filename);
somefunction(filename)
end
function callback2(hObject,eventdata)
handles=guidata(hObject);
somefunction(handles.filename)
end
function somefunction(filename)
disp(fileread(filename))
end
As DGM mentions, there are several ways to share data between different parts of your GUI, using guidata or nested functions are only two. For general advice and examples for how to create a GUI (and avoid using GUIDE), have look at this thread.
  댓글 수: 2
Rik
Rik 2022년 1월 28일
Comment posted as answer by @ali hassan:
from open selected menu, i am running command:
[filename,pathname] = uigetfile('*.txt');
then i am using it in my function update button, but i want to know that how can i do so?
Rik
Rik 2022년 1월 28일
[filename,app.pathname] = uigetfile('*.txt');
% ^^ that will probably work
You do need to make sure that openMenuSelected is called befor UpdateButtonPushed runs. You can do that fairly easily:
function UpdateButtonPushed(app,event)
%make sure the file path is selected
if isempty(app.pathname),UpdateButtonPushed(app,event),end
...
end

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Historical Contests에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by