get file name from other directory

4 Ansichten (letzte 30 Tage)
Zakaria Sadok
Zakaria Sadok am 11 Mär. 2019
Kommentiert: Zakaria Sadok am 12 Mär. 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 Kommentare
Bob Thompson
Bob Thompson am 12 Mär. 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]);
Zakaria Sadok
Zakaria Sadok am 12 Mär. 2019
it says error using dir
Error using dir
Characters adjacent to a ** wildcard must be file separators.
Error in ourProgram (line 22)
fullFileName =dir([folder,'*',baseFileName1])

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Stephen23
Stephen23 am 12 Mär. 2019
Bearbeitet: Stephen23 am 12 Mär. 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:
  1 Kommentar
Zakaria Sadok
Zakaria Sadok am 12 Mär. 2019
it worked well, thank you so much sir :D

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Startup and Shutdown finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by