get file name from other directory

4 ビュー (過去 30 日間)
Zakaria Sadok
Zakaria Sadok 2019 年 3 月 11 日
コメント済み: Zakaria Sadok 2019 年 3 月 12 日
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 件のコメント
Bob Thompson
Bob Thompson 2019 年 3 月 12 日
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 2019 年 3 月 12 日
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])

サインインしてコメントする。

採用された回答

Stephen23
Stephen23 2019 年 3 月 12 日
編集済み: Stephen23 2019 年 3 月 12 日
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 件のコメント
Zakaria Sadok
Zakaria Sadok 2019 年 3 月 12 日
it worked well, thank you so much sir :D

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by