How can I retrieve data from a callback of an in-built function?
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Manoj Abhishetty
el 23 de Jul. de 2018
Comentada: Manoj Abhishetty
el 26 de Abr. de 2020
Hello. I am trying to write a script that, when executed, allows the user to click on a button in a dialog box and select a directory. Furthermore, I want to store the directory path as a string. The problem I have is that the 'callback' I am using is to an in-built function and the 'help' I have looked at seems to be for user-created functions.
For example: https://uk.mathworks.com/help/matlab/creating_guis/share-data-among-callbacks.html#bt9p4qp.
I believe the advice on 'guidata' and 'guihandles' is for cases where you can write 'guihandles' into a separate section of code where the function is defined, which I don't have. I saw the same thing in the section for storing data in UserData.
Here is what I have so far:
% Setting up a dialog box
box = dialog('WindowStyle','normal','Name','My Dialog');
% Setting up 'uicontrol'
uicontrol(box,'Style','pushbutton','String','Click here','Position',[50 50 50 50],'Callback',@(src,event)uigetdir('C:\','Pick a folder:'));
So, how can I retrieve the directory path from the callback of the 'uigetdir' function? Any solutions or links to documentation that already has the answer would be greatly appreciated.
Many Thanks.
2 comentarios
Image Analyst
el 23 de Jul. de 2018
Why not simply use GUIDE or App Designer? Why do it the hard way by going into all these gory details yourself?
Respuesta aceptada
Walter Roberson
el 23 de Jul. de 2018
Editada: Walter Roberson
el 23 de Jul. de 2018
You cannot retrieve any value at all from the result of a Callback. Any value returned by a Callback will be discarded, always.
The only exception to this rule is that there are limited number of circumstances in which a callback function is used as a filter, such as providing constraint information for the imroi class of functions. Also, the UpdateFcn of a datecursormode() object must return a character vector or cell array of character vectors (or perhaps string array these days; I haven't looked into that detail.)
What you need to do is code the callback function so that it saves information in a known location, such as using guidata() or setappdata(), or set the UserData property of an object.
3 comentarios
Walter Roberson
el 23 de Jul. de 2018
Correct, you need to write some extra code to save the results of the uigetdir() call.
If you were only interested in the first output value from uigetdir() then there are potentially methods you could use within an anonymous function. However, there is no method that can be used in an expression to collect multiple outputs of a function: at some point there would have to be an assignment statement of the form
[filename, pathname] = uigetdir(....)
so at least one non-anonymous function would have to be used, even if it was just an auxillary function
function result = call2(f, varargin)
[val1, val2] = f(varargin{:});
result = {val1, val2};
with the result of the work done through expression syntax.
The alternative instead of anonymous function would be to specify the callback as a character vector, in which case it will be executed as if by evalin('base', ...)
so you could code the callback as
'[filename, pathname] = uigetdir(....); set(gcbo, ''UserData'', {filename, pathname});'
Más respuestas (0)
Ver también
Categorías
Más información sobre Entering Commands en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!