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

2 ビュー (過去 30 日間)
Manoj Abhishetty
Manoj Abhishetty 2018 年 7 月 23 日
コメント済み: Manoj Abhishetty 2020 年 4 月 26 日
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 件のコメント
Image Analyst
Image Analyst 2018 年 7 月 23 日
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 2018 年 7 月 23 日
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.

サインインしてコメントする。

採用された回答

Walter Roberson
Walter Roberson 2018 年 7 月 23 日
編集済み: Walter Roberson 2018 年 7 月 23 日
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 件のコメント
Walter Roberson
Walter Roberson 2018 年 7 月 23 日
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 2020 年 4 月 26 日
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!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeEntering Commands についてさらに検索

製品


リリース

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by