using output of one button as an input in another button

7 visualizaciones (últimos 30 días)
ali hassan
ali hassan el 28 de En. de 2022
Comentada: Rik el 28 de En. de 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 comentarios
DGM
DGM el 28 de En. de 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.

Iniciar sesión para comentar.

Respuesta aceptada

Rik
Rik el 28 de En. de 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 comentarios
Rik
Rik el 28 de En. de 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 el 28 de En. de 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

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Historical Contests en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by