MATLAB: How to retrieve data from a callback of an in-built function

callbackdialogdirectoryhandlesMATLABuigetdir

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.
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.

Best Answer

  • 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.