MATLAB Answers

How can I avoid displaying "0" in my text box when cancel is pressed using UIGETDIR

20 views (last 30 days)
Spigge
Spigge on 3 Aug 2014
Commented: Spigge on 4 Aug 2014
Hi,
The dialog box UIGETDIR returns either the selected folder name as a string if OK is pressed or the number 0 if Cancel is pressed. In my application I have an edit box with a callback function that calls UIGETDIR and then displays the selected folder string, something like this:
editboxhandle.callbackfcn = @set(editboxhandle,'String',uigetdir);
However, when Cancel is pressed, "0" is displayed in my edit box but while I would like the string to be empty in such case. I'm am restricted to a singel expression callback like the one above. How can I solve this?

  0 Comments

Sign in to comment.

Answers (1)

Azzi Abdelmalek
Azzi Abdelmalek on 3 Aug 2014
k=uigetdir;
if k==0;
k=[];
end

  3 Comments

Spigge
Spigge on 3 Aug 2014
OK, but as I mentioned, I need to be able to do this in a single expression inside the (anonymous type) callback function. Your solution has more than one epression. Nevertheless, I seem to have cracked it myself by using the char command, like this:
editboxhandle.callbackfcn = @char(set(editboxhandle,'String',uigetdir));
When the number 0 is returned by UIGETDIR, char(0) becomes empty.
Azzi Abdelmalek
Azzi Abdelmalek on 3 Aug 2014
Why do you need to do it in a single line?
k=uigetdir;
if k==0;
k=[];
end
editboxhandle.callbackfcn = @char(set(editboxhandle,'String',k))

Sign in to comment.

Sign in to answer this question.