How can I retrieve data from a callback of an in-built function?

2 Ansichten (letzte 30 Tage)
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.
  2 Kommentare
Image Analyst
Image Analyst am 23 Jul. 2018
Why not simply use GUIDE or App Designer? Why do it the hard way by going into all these gory details yourself?
Manoj Abhishetty
Manoj Abhishetty am 23 Jul. 2018
I actually only discovered GUIDE after going down the above route. GUIDE does seem easier, but this exercise is mainly for interest. I would just like to know if and how it is possible to do it without using GUIDE.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 23 Jul. 2018
Bearbeitet: Walter Roberson am 23 Jul. 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 Kommentare
Walter Roberson
Walter Roberson am 23 Jul. 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});'
Manoj Abhishetty
Manoj Abhishetty am 26 Apr. 2020
Thanks so much Walter - I ended up using the UserData property and it worked fine.
Sorry for how late this response is but I wanted to thank you!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Entering Commands finden Sie in Help Center und File Exchange

Produkte


Version

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by