using output of one button as an input in another button

7 vues (au cours des 30 derniers jours)
ali hassan
ali hassan le 28 Jan 2022
Commenté : Rik le 28 Jan 2022
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 commentaires
DGM
DGM le 28 Jan 2022
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.

Connectez-vous pour commenter.

Réponse acceptée

Rik
Rik le 28 Jan 2022
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 commentaires
Rik
Rik le 28 Jan 2022
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 le 28 Jan 2022
[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

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Historical Contests dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by