Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

uigetdir on Mac fails - worked on WinXP

146 views
Skip to first unread message

Jeff Eriksen

unread,
Dec 11, 2010, 2:10:07 PM12/11/10
to
While waiting for my new Mac, I installed Matlab on WinXP and began to learn GUI building with GUIDE. I wrote two GUIs that worked OK on WinXP, which use uigetdir to choose files to open. However, when I move this code to the Mac, I can go so far as choosing a file, but then I get an error message that it cannot find the file. I assume there is probably a simple little trick to fix this - anybody know what is going on here? Thanks,
-Jeff

Walter Roberson

unread,
Dec 11, 2010, 2:17:29 PM12/11/10
to

uigetdir() chooses a directory, not a file -- files would be uigetfile() .

Matlab invoked in Mac OS does not necessarily start with your current
directory or home directory as the current directory (especially if you
started it from the icon), so you probably need to either cd to the
chosen directory or else ensure that you concatenate the chosen
directory together with the chosen file names, preferably via
fullfilename().

Dusan

unread,
Dec 13, 2010, 5:12:07 AM12/13/10
to
> Matlab invoked in Mac OS does not necessarily start with your current
> directory or home directory as the current directory (especially if you
> started it from the icon), so you probably need to either cd to the
> chosen directory or else ensure that you concatenate the chosen
> directory together with the chosen file names, preferably via
> fullfilename().

How come the following lines do not work on Mac:

Folder=uigetdir('C:\Documents and Settings\John\My Documents\MATLAB',
'Find image files');
cd(Folder);
FolderImages=[Folder,'\*.jpg'];
imgFiles=dir(fullfile(FolderImages));

I keep getting error message that I narrowed down to the last line.
Something about dir(fullfile(.... or more precisely fullfile() is not
working right.
Any ideas on what might be wrong ? Sorry, I can't remember the exact
error message, but each time I end up with an empty structure for
imgFiles whereas I should get a list of jpeg image file names.
Many thanks
Dusan

Dusan

unread,
Dec 13, 2010, 8:25:39 AM12/13/10
to
> How come the following lines do not work on Mac:
>
> Folder=uigetdir('C:\Documents and Settings\John\My Documents\MATLAB',
> 'Find image files');
> cd(Folder);
> FolderImages=[Folder,'\*.jpg'];
> imgFiles=dir(fullfile(FolderImages));

In the meantime I found the error message:


??? Index exceeds matrix dimensions.

Error in ==> ACropKanaliDANICAnaj at 11
path=fullfile(Folder,imgFiles(1).name);

and I've figured this one out. The reason is that ML on Mac includes
the "." and ".." in the structure so that file name is always stated
on the top of the list.
My main concern is why do I get an empty structure with number of
images being 0.

Thanks

Jos (10584)

unread,
Dec 13, 2010, 9:15:08 AM12/13/10
to
Dusan <dusan....@gmail.com> wrote in message <b51016bf-d417-494d...@q18g2000vbm.googlegroups.com>...

Real OS's use forward slashes ...
In addition, your use of FULLFILE is somewhat shaky. Try this:

FolderImages=fullfile(Folder,'*.jpg');
imgFiles=dir(FolderImages);

hth
Jos

Dusan

unread,
Dec 15, 2010, 6:38:36 AM12/15/10
to
> > How come the following lines do not work on Mac:
>
> > Folder=uigetdir('C:\Documents and Settings\John\My Documents\MATLAB',
> > 'Find image files');
> > cd(Folder);
> > FolderImages=[Folder,'\*.jpg'];
> > imgFiles=dir(fullfile(FolderImages));

> Real OS's use forward slashes ...
> In addition, your use of FULLFILE is somewhat shaky. Try this:
>
> FolderImages=fullfile(Folder,'*.jpg');
> imgFiles=dir(FolderImages);
>
> hth
> Jos

I tried this but still I get the same error message related to empty
structure imgFiles.
I also changer bw to fw slashes. Also nothing...
Any new ideas ?
Thanks a bunch...
Dusan

Steven_Lord

unread,
Dec 15, 2010, 9:41:00 AM12/15/10
to

"Dusan" <dusan....@gmail.com> wrote in message

news:bb06ea82-10c6-4c41...@15g2000vbz.googlegroups.com...


>> > How come the following lines do not work on Mac:
>>
>> > Folder=uigetdir('C:\Documents and Settings\John\My Documents\MATLAB',
>> > 'Find image files');
>> > cd(Folder);
>> > FolderImages=[Folder,'\*.jpg'];
>> > imgFiles=dir(fullfile(FolderImages));

I would pull the FULLFILE one line earlier, but that's me.

FolderImages=fullfile(Folder, '*.jpg');
imgFiles=dir(FolderImages);

>> Real OS's use forward slashes ...
>> In addition, your use of FULLFILE is somewhat shaky. Try this:
>>
>> FolderImages=fullfile(Folder,'*.jpg');
>> imgFiles=dir(FolderImages);
>>
>> hth
>> Jos
>
> I tried this but still I get the same error message related to empty
> structure imgFiles.

So display the contents of the FolderImages variable and navigate to that
directory outside MATLAB. Does the directory contain any files with the
extension .jpg? If not, then imgFiles will be (and should be) empty.

--
Steve Lord
sl...@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlab.wikia.com/wiki/FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Kelly Kearney

unread,
Dec 15, 2010, 10:37:23 AM12/15/10
to
Dusan <dusan....@gmail.com> wrote in message <bb06ea82-10c6-4c41...@15g2000vbz.googlegroups.com>...

> > > How come the following lines do not work on Mac:
> >
> > > Folder=uigetdir('C:\Documents and Settings\John\My Documents\MATLAB',
> > > 'Find image files');
> > > cd(Folder);
> > > FolderImages=[Folder,'\*.jpg'];
> > > imgFiles=dir(fullfile(FolderImages));
<snip>

> I tried this but still I get the same error message related to empty
> structure imgFiles.
> I also changer bw to fw slashes. Also nothing...
> Any new ideas ?

Are you sure you're choosing the correct folder at the uigetdir prompt? You seem to have left a Windows path in there as the startpath variable. The Mac behavior isn't documented in the help, but I find that on a Mac if you call uigetdir with an invalid path, it opens in the current directory. So perhaps you're expecting to choose a folder in /Users/you/Documents/Matlab (the Mac startup location), but instead are choosing one relative to a different location?

-Kelly

Dusan

unread,
Dec 15, 2010, 12:51:13 PM12/15/10
to
First of all, Steve and Kelly thank you for your answers.

Steve:
The directory contains 100s of .jpg images plus an xls document.
That's all. I've separated fullfile and it worked. Thanks.

The main reason why I'm bugging you with this question is that:
1. it doesn't work with a '*.jpg' specifier. I also played with '/'
and '\' but in both cases I get the same error message because
structure imgFiles remains empty.
I can overcome this by putting only .jpg-s into my FolderImages
but a specifier is a neat thing and I'm annoyed by why such a simple
thing doesn't work.

2. it also doesn't work as dir(fullfile()) which is another simple
thing that should work (and the help file also says it should). It
works on my XP but not on Mac and, strangely enough, it doesn't even
seem to work on some XPs (but that's beyond my current interest).

I thought there's something obviously different about Mac OS...

Kelly:
Yes I have tried to replace the existing path with exactly the one
you've suggested but everything stays the same.

I could do without dir(fullfile()), but I'm gonna miss the
specifier ... :)
Thank you guys...
Dusan

Kelly Kearney

unread,
Dec 15, 2010, 1:31:05 PM12/15/10
to
Dusan <dusan....@gmail.com> wrote in message <424c929e-575a-4568...@m35g2000vbn.googlegroups.com>...

Try running just these lines of code, choosing your image folder at the uigetfile prompt:

folder = uigetdir;
allfiles = dir(folder);
jpgfiles = dir(fullfile(folder, '*.jpg'));

Are the contents of folder and allfiles what you expect them to be? Is jpgfiles still empty? Also check that your jpegs have a .jpg and not a .jpeg extension. I'm working on a Mac, and I don't notice any problems when I run the above with an image-containing folder on my computer.

Steven_Lord

unread,
Dec 15, 2010, 3:20:54 PM12/15/10
to

"Dusan" <dusan....@gmail.com> wrote in message

news:424c929e-575a-4568...@m35g2000vbn.googlegroups.com...


> First of all, Steve and Kelly thank you for your answers.
>
> Steve:
> The directory contains 100s of .jpg images plus an xls document.
> That's all. I've separated fullfile and it worked. Thanks.

Be careful -- are you certain that you have image files that have the
specific extension .jpg or do you have files containing images compressed
using JPEG (which according to Wikipedia could have extension .jpg, .jpeg,
.jpe, .jif, .jfif, or .jfi) that are being described by your OS as JPEG
images?

http://en.wikipedia.org/wiki/JPEG

> The main reason why I'm bugging you with this question is that:
> 1. it doesn't work with a '*.jpg' specifier. I also played with '/'

Define "doesn't work".

> and '\' but in both cases I get the same error message because
> structure imgFiles remains empty.

Show the EXACT lines of code you're using, please, along with the contents
of the variable that you're passing into DIR and (at least a section of) the
output of "!ls" (without the quotes) in the directory to which that variable
refers.

> I can overcome this by putting only .jpg-s into my FolderImages
> but a specifier is a neat thing and I'm annoyed by why such a simple
> thing doesn't work.
>
> 2. it also doesn't work as dir(fullfile()) which is another simple
> thing that should work (and the help file also says it should).

This assumes that you've implemented it in your code correctly, which the
group can check if you post the information I requested above.

Dusan

unread,
Dec 17, 2010, 4:59:55 PM12/17/10
to
Steve:

> Be careful -- are you certain that you have image files that have the
> specific extension .jpg or do you have files containing images compressed
> using JPEG (which according to Wikipedia could have extension .jpg, .jpeg,
> .jpe, .jif, .jfif, or .jfi) that are being described by your OS as JPEG
> images?

The images come from Canon IXUS 85 camera. So that should mean .jpg ?
When I mouse point to these files in WinExplorer they are shown
as .jpeg files but I used uigetdir on my XP with .jpg and I never
received a warning or an error mess.

> Define "doesn't work".

By "doesn't work" I mean that imgFiles structure is empty.

> Show the EXACT lines of code you're using, please, along with the contents

Ok. I apologize for not posting previously:
Here's the part of the code that "doesn't work"

Folder=uigetdir('C:\Documents and Settings\Dusan\My Documents
\MATLAB','Find the folder with images');


cd(Folder);
FolderImages=[Folder,'\*.jpg'];
imgFiles=dir(fullfile(FolderImages));

put=fullfile(Folder,imgFiles(1).name);


And here's the same part of the code that "works":

Folder=uigetdir('','Find the folder with images');
cd(Folder);
imgFiles=dir(Folder);
put=fullfile(Folder, imgFiles(3).name);

Kelly:
I ran the code you suggested but I still get an empty imgFiles
structure :(
allfiles is not empty but jpgfiles is....

Sorry for not replying sooner.
Thnx

Steven_Lord

unread,
Dec 17, 2010, 5:04:00 PM12/17/10
to

"Dusan" <dusan....@gmail.com> wrote in message

news:f438f396-ecb3-4761...@d8g2000yqf.googlegroups.com...


> Steve:
>> Be careful -- are you certain that you have image files that have the
>> specific extension .jpg or do you have files containing images compressed
>> using JPEG (which according to Wikipedia could have extension .jpg,
>> .jpeg,
>> .jpe, .jif, .jfif, or .jfi) that are being described by your OS as JPEG
>> images?
>
> The images come from Canon IXUS 85 camera. So that should mean .jpg ?

I would make certain that they are actually files with the .jpg extension.

> When I mouse point to these files in WinExplorer they are shown
> as .jpeg files but I used uigetdir on my XP with .jpg and I never
> received a warning or an error mess.

Double-check specifically what extension those files have.

>> Define "doesn't work".
> By "doesn't work" I mean that imgFiles structure is empty.
>
>> Show the EXACT lines of code you're using, please, along with the
>> contents
> Ok. I apologize for not posting previously:
> Here's the part of the code that "doesn't work"
>
> Folder=uigetdir('C:\Documents and Settings\Dusan\My Documents
> \MATLAB','Find the folder with images');

And you're running this exact code on the Mac? Unless Apple's done some
Windows compatibility work of which I'm unaware, it probably won't recognize
"c:\Documents and Settings...".

*snip*

Walter Roberson

unread,
Dec 17, 2010, 5:16:57 PM12/17/10
to
On 10-12-17 03:59 PM, Dusan wrote:

> Here's the part of the code that "doesn't work"
>
> Folder=uigetdir('C:\Documents and Settings\Dusan\My Documents
> \MATLAB','Find the folder with images');
> cd(Folder);
> FolderImages=[Folder,'\*.jpg'];
> imgFiles=dir(fullfile(FolderImages));
> put=fullfile(Folder,imgFiles(1).name);

You cannot use \ as a separator in Mac OS. You can, though, use / as a
separator in MS Windows.

On Mac OS X, something that started with C: followed by the folder separator
would refer to a directory with the literal name "C:" that is relative to the
current directory. In order to refer to a drive on OS X, you would need to
start the path with a / (or possibly with a resource identifier such as
"http:" in some contexts.)

Note that you can use ispc() to distinguish between running on Windows or not.
I also suggest you check out what

getenv('HOME')

gives you on the two systems, as that might allow you to construct the paths
rather than hard-coding them is in,

imgdir = '/Users/home/dusan/Documents/Matlab';
if ispc()
imgdir = 'C:/Documents and Settings/Dusan/My Documents/Matlab';
end
Folder = uigetdir(imgdir,'Find the folder with images');
cd(Folder);
imgFiles = dir('*.jpg');
put = fullfile(Folder,imgFiles(1).name);

Note that when you have cd'd to the folder, there is no need to include the
folder name on the path that you send to dir() as dir() will assume relative
paths.

Dusan

unread,
Dec 17, 2010, 5:46:10 PM12/17/10
to
> > Folder=uigetdir('C:\Documents and Settings\Dusan\My Documents
> > \MATLAB','Find the folder with images');

Sorry about that also. No I have changed this to Mac's /User...
path... no luck

Kelly Kearney

unread,
Dec 20, 2010, 11:16:05 AM12/20/10
to

> Kelly:
> I ran the code you suggested but I still get an empty imgFiles
> structure :(
> allfiles is not empty but jpgfiles is....
>
> Sorry for not replying sooner.
> Thnx

Well, what's in allfiles? Does it contain all the files you expected to see (lots of jpegs, one .xls)? Run the following code, then post everything that's printed to the command line:

% Code to get files:

folder = uigetdir;
allfiles = dir(folder);
jpgfiles = dir(fullfile(folder, '*.jpg'));

folder
{allfiles.name}'
{jpgfiles.name}'

0 new messages