get file name from other directory
24 views (last 30 days)
Show older comments
Zakaria Sadok
on 11 Mar 2019
Commented: Zakaria Sadok
on 12 Mar 2019
hello all :
folder = uigetdir
baseFileName1 = 'MTL.txt'
fullFileName =dir(fullfile(folder,['*', baseFileName1]))
fullFileName= fullfile(folder,fullFileName.name)
if exist(fullFileName)
this is my program i was using it under MAC OS , but now that i am using windows this program does not give me the real path of the file, and it shows the name of the file 2 times here is what i get
fullFileName =
2×1 struct array with fields:
name
folder
date
bytes
isdir
datenum
enmpty
fullFileName =
'C:\Users\itach\Desktop\Fmask_3_3 matlab\LC08_L1TP_196035_20181101_20181115_01_T2\._LC08_L1TP_196035_20181101_20181115_01_T2_MTL.txt\LC08_L1TP_196035_20181101_20181115_01_T2_MTL.txt'
as you see the first file is empty and the second one has the path wrong it shows a directory that does not exist
(._LC08_L1TP_196035_20181101_20181115_01_T2_MTL.txt)
can you help me to fix it and get the right directory and right name
2 Comments
Bob Thompson
on 12 Mar 2019
Why are you using 'fullfile' twice? Most likely this is why you are having your problem. I would make the following adjustment.
fullfile = dir([folder,'*',baseFileName1]);
Accepted Answer
Stephen23
on 12 Mar 2019
Edited: Stephen23
on 12 Mar 2019
The problem is on this line:
fullFileName= fullfile(folder,fullFileName.name)
% ^^^^^^^^^^^^^^^^^ comma-separated list
where you take the (badly-named) structure fullFileName and convert it into a comma-separated list. Read these to learn what comma-separated lists are:
This comma-separated list means that line is exactly equivalent to:
fullFileName= fullfile(folder,fullFileName(1).name,fullFileName(2).name,...,fullFileName(end).name)
which gives exactly the output that you have shown us, and is unlikely to be very useful.
"can you help me to fix it and get the right directory and right name"
You will have to use a loop to iterate over those filenames, e.g.:
D = uigetdir()
B = 'MTL.txt'
S = dir(fullfile(D,['*',B]))
for k = 1:numel(S)
F = fullfile(D,S(k).name)
if exist(F)
...
end
end
Note that this is based on the method shown in the MATLAB documentation:
More Answers (0)
See Also
Categories
Find more on File Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!