Thread Subject: uigetfile: check for Cancel button pressed.

Subject: uigetfile: check for Cancel button pressed.

From: Andrey Kazak

Date: 22 Dec, 2008 12:53:02

Message: 1 of 7

Greetings!

I write a script and use uigetfile function to select file to open:

  [InputFileName,InputFilePath] = uigetfile(...

and right after check for a Cancel button pressed:

  if (InputFileName == 0) && (InputFilePath == 0)
    error('Input file is not selected!')
  end

Now, in case of Cancel button pressed I get 'Input file is not selected!' error message and my script is terminated. However if I select a file from dialog and push Open button I get 'Operands to the || and && operators must be convertible to logical scalar values.' error message and my script is terminated also...

How to solve the problem?

P. S. Help for uigetfile function states "If the user clicks Cancel or closes the dialog window, FileName and PathName are set to 0.".

Subject: uigetfile: check for Cancel button pressed.

From: Doug Schwarz

Date: 22 Dec, 2008 13:06:55

Message: 2 of 7

In article <gio2je$32p$1@fred.mathworks.com>,
 "Andrey Kazak" <AK@nospam.ru> wrote:

> Greetings!
>
> I write a script and use uigetfile function to select file to open:
>
> [InputFileName,InputFilePath] = uigetfile(...
>
> and right after check for a Cancel button pressed:
>
> if (InputFileName == 0) && (InputFilePath == 0)
> error('Input file is not selected!')
> end
>
> Now, in case of Cancel button pressed I get 'Input file is not selected!'
> error message and my script is terminated. However if I select a file from
> dialog and push Open button I get 'Operands to the || and && operators must
> be convertible to logical scalar values.' error message and my script is
> terminated also...
>
> How to solve the problem?
>
> P. S. Help for uigetfile function states "If the user clicks Cancel or closes
> the dialog window, FileName and PathName are set to 0.".

Use

  if isequal(InputFileName,0)

(You only need to test one of the variables.)

--
Doug Schwarz
dmschwarz&ieee,org
Make obvious changes to get real email address.

Subject: uigetfile: check for Cancel button pressed.

From: us

Date: 22 Dec, 2008 13:09:03

Message: 3 of 7

"Andrey Kazak"
> [InputFileName,InputFilePath] = uigetfile(...
> if (InputFileName == 0) && (InputFilePath == 0)
> error('Input file is not selected!')
> end
> Now, in case of Cancel button pressed I get 'Input file is not selected!' error message and my script is terminated. However if I select a file from dialog and push Open button I get 'Operands to the || and && operators must be convertible to logical scalar values.'...

a hint:

% what does this snippet do
     fn='myfile.m'; % <- assume you selected this file in the box...
     fn==0
% ans =

does it look like a scalar?
us

Subject: uigetfile: check for Cancel button pressed.

From: us

Date: 22 Dec, 2008 13:15:05

Message: 4 of 7

Doug Schwarz
> > P. S. Help for uigetfile function states "If the user clicks Cancel or closes
> > the dialog window, FileName and PathName are set to 0.".

> if isequal(InputFileName,0)

another one of the many solutions

if ischar(fn)
% a file
else
% returned DOUBLE(0)
end

us

Subject: uigetfile: check for Cancel button pressed.

From: Andrey Kazak

Date: 22 Dec, 2008 13:24:03

Message: 5 of 7

Thank you guys!

Topic is closed...

Subject: uigetfile: check for Cancel button pressed.

From: someone

Date: 22 Dec, 2008 15:43:02

Message: 6 of 7

"Andrey Kazak" <AK@nospam.ru> wrote in message <gio2je$32p$1@fred.mathworks.com>...
> Greetings!
>
> I write a script and use uigetfile function to select file to open:
>
> [InputFileName,InputFilePath] = uigetfile(...
>
> and right after check for a Cancel button pressed:
>
> if (InputFileName == 0) && (InputFilePath == 0)
> error('Input file is not selected!')
> end
>
> Now, in case of Cancel button pressed I get 'Input file is not selected!' error message and my script is terminated. However if I select a file from dialog and push Open button I get 'Operands to the || and && operators must be convertible to logical scalar values.' error message and my script is terminated also...
>
> How to solve the problem?
>
> P. S. Help for uigetfile function states "If the user clicks Cancel or closes the dialog window, FileName and PathName are set to 0.".

% One more solution:

if (InputFileName(1) == 0) && (InputFilePath(1) == 0)

Subject: uigetfile: check for Cancel button pressed.

From: Walter Roberson

Date: 22 Dec, 2008 17:22:43

Message: 7 of 7

someone wrote:
 
> % One more solution:
>
> if (InputFileName(1) == 0) && (InputFilePath(1) == 0)

Does Microsoft promise to follow the POSIX standard that filenames may not have
embedded NUL characters (binary zeros that is)? Matlab preserves NUL characters
in strings.

>> char([0 65 66 67])

ans =

 ABC

>> length(char([0 65 66 67]))

ans =

     4

>> char([0 65 66 67]) + 0

ans =

     0 65 66 67

--
.signature note: I am now avoiding replying to unclear or ambiguous postings.
Please review questions before posting them. Be specific. Use examples of what you mean,
of what you don't mean. Specify boundary conditions, and data classes and value
relationships -- what if we scrambled your data or used -Inf, NaN, or complex(rand,rand)?

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
ischar us 22 Dec, 2008 08:20:05
code us 22 Dec, 2008 08:10:09
logical operator us 22 Dec, 2008 08:10:09
uigetfile cancel Andrey Kazak 22 Dec, 2008 07:55:06
rssFeed for this Thread

Contact us at files@mathworks.com