read multiple excel files with beginning part of file name being similar

2 ビュー (過去 30 日間)
Ahisa
Ahisa 2021 年 7 月 6 日
編集済み: Ahisa 2021 年 7 月 7 日
I have 4 excel files that all start with THA and there are 4 types(THA1, THA2, THA3 & THA4) the full file name for all 4 are like this: THA1_343fdf and I want to extract all four file names by filtering it with THA if possible. I can extract the data easily but struggling with extracting all 4 file names.
  13 件のコメント
dpb
dpb 2021 年 7 月 7 日
tData=readtable(fullfile(d(i).folder,"d(i)name"))
It's doing exactly what you told it to do -- it's looking for the literal string "d(i)name" as the file name, not the next file in the dir() struct d() array that you returned.
Look at the documentation for dir and the examples there and look at what the call returns in the variable d struct at the command line to understand what you're doing here.
The code is, as I wrote initially,
tData=readtable(fullfile(d(i).folder,d(i)name));
Remove the quotes; they don't belong...
Ahisa
Ahisa 2021 年 7 月 7 日
編集済み: Ahisa 2021 年 7 月 7 日
never mind, this is how it should look, correct?
d=dir(fullfile('C:\Users\R101951\Desktop\RB\New folder','THA*.*'));
for i=1:numel(d)
tData=readtable(fullfile(d(i).folder,d(i).name));
end

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

回答 (1 件)

Jonas
Jonas 2021 年 7 月 7 日
編集済み: Jonas 2021 年 7 月 7 日
more something like that?
for type=1:4
basepath='C:\Users\R101951\Desktop\RB\New folder\';
currFile=dir([basepath 'THA' num2str(type) '*.xls']).name;
data=readtable(currFile,.....);
end
the ' * ' is a wildcard for the '_343fdf' part which I assumed is not the same for every file you are interested in
  1 件のコメント
Ahisa
Ahisa 2021 年 7 月 7 日
Error: Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for
mismatched delimiters.
thanks for that, i tried yours b
ut got this error message

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

カテゴリ

Help Center および File ExchangeSpreadsheets についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by