Problem with "ls" command

19 views (last 30 days)
Ali Razavi
Ali Razavi on 13 Nov 2022
Edited: Marcel on 17 Nov 2022
Hello,
I have encountered a strange issue.
There is a directory that has 29 files, as an example 2 of them are listed here.
However, when I try listing them indivisually only one of them gets listed:
>> T=ls('D:\2022_11_08_001_FWHM\20221011_FB0031471-BCC_PSQ007_8\AF_CALIBRATION\2022-10-11-12-58-56-876\L02\S02\T054\')
T =
'seq_Z_-00231.tiff '
'seq_Z_-00273.tiff '
>> ls('D:\2022_11_08_001_FWHM\20221011_FB0031471-BCC_PSQ007_8\AF_CALIBRATION\2022-10-11-12-58-56-876\L02\S02\T054\seq_Z_-00273.tiff')
seq_Z_-00273.tiff
>> ls('D:\2022_11_08_001_FWHM\20221011_FB0031471-BCC_PSQ007_8\AF_CALIBRATION\2022-10-11-12-58-56-876\L02\S02\T054\seq_Z_-00231.tiff')
'D:\2022_11_08_001_FWHM\20221011_FB0031471-BCC_PSQ007_8\AF_CALIBRATION\2022-10-11-12-58-56-876\L02\S02\T054\seq_Z_-00231.tiff' not found.

Answers (3)

Image Analyst
Image Analyst on 13 Nov 2022
Moved: Image Analyst on 15 Nov 2022
It seems ls() thinks there is a space or two after the extension. You are not adding that space when you call ls() for individual files.

Marcel
Marcel on 15 Nov 2022
Edited: Marcel on 16 Nov 2022
I think there is a small error. This works fine because the file path is a directory.
>> T=ls('D:\2022_11_08_001_FWHM\20221011_FB0031471-BCC_PSQ007_8\AF_CALIBRATION\2022-10-11-12-58-56-876\L02\S02\T054\')
T =
'seq_Z_-00231.tiff '
'seq_Z_-00273.tiff '
Tho on this one in your second example, you included a filename. I guess the problem is that you try to list a file that you've already specified in the path in your ls command.
>> ls('D:\2022_11_08_001_FWHM\20221011_FB0031471-BCC_PSQ007_8\AF_CALIBRATION\2022-10-11-12-58-56-876\L02\S02\T054\seq_Z_-00273.tiff')
seq_Z_-00273.tiff
If seq_Z[...].tiff is a directory, then this is a strange issue.
  4 Comments
Image Analyst
Image Analyst on 15 Nov 2022
Well when you said the reason why it said "file not found" when he gave LS the name of a particular file was "the problem is that you try to list a file that you've already specified." I assumed you meant that the file was already found/listed when he first used ls to get a list of all files in the folder. What did you mean when you said it was "already specified"? And why would specifying arguments to ls twice make it not work the second time?
Marcel
Marcel on 16 Nov 2022
Edited: Marcel on 16 Nov 2022
Oh no sorry.
I meant like if you call ls("path\to\smth\file.txt") then you would've already specified the filein the ls command, so it would only return that filename of course. earlier he made a ls call like ls("path\to\smth\") without a filename, so the command would return all the files in the directory specified.
See this example. When calling dir it shows me all the files etc in that directory, but if i use dir with the filename it only returns that same filename

Sign in to comment.


Ali Razavi
Ali Razavi on 16 Nov 2022
Edited: Ali Razavi on 16 Nov 2022
Hello,
Thanks for input.
The strange issue disappeared on its own.
For a while, for some unkown reason "ls" would list all files in directory correct if the "directory" was its argument. However, it would not see "some" of those file when "ls" was applied to "individual" files in that directory. Oddly, the files shared same name length and naming convention (their difference was in enumeration, e.g., 231, 273, ...). Attempting to read those files in MATLAB was impossible, while outside MATLAB they were accessible & readable. For some reason, MATLAB could not see certain files.
As strange as it started, it disappeared. Maybe after disconnecting/reconnecting external hard drive and re-starting MATLAB and PC? Not sure. But for now things are good.
Thanks for your attention and all the help.
Have a great day.
  3 Comments
Image Analyst
Image Analyst on 16 Nov 2022
ls can also list one or more filenames if you give a file pattern. It does not just work on directories alone. For example these both list the same files:
dir('test.*')
ls('test.*') % File name pattern, not a folder.
@Ali Razavi didn't say what he was going to do with the files once he got their name(s) but if he wants to open them or something, then he can change the inside of the loops in the code snippets in the FAQ:
to do whatever he wants to do with the file.
Marcel
Marcel on 17 Nov 2022
Edited: Marcel on 17 Nov 2022
Well yes of course same as with the windows explorer.
It seems like he wanted to see whats inside a file so i made a "simplified" answer to that, and so far my statement isnt wrong as well as yours :P (meaning yours is correct too)

Sign in to comment.

Categories

Find more on File Operations in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!