フィルターのクリア

opening multiple excel files in different location with a loop

1 回表示 (過去 30 日間)
Tat Onn Huen
Tat Onn Huen 2021 年 8 月 26 日
編集済み: Tat Onn Huen 2021 年 8 月 26 日
I am trying to write a script to take in multiple xlsx files and concatenate them.
It will have to take in different number of xlsx files depending on the situation. For example, if I input 3 location, there will be 3 xlsx file to read.
The files generally have the same name, just that they are in separate folders and are numbered. For example, if there are 5 locations, they will be numbered from 1 to 5.
% Input number of sites
N = input('Number of Location? ', 's')
% n = str2double(N)
% Loop to read all files
for i = 1:N
i = readtable(append('C:\Users\tathuen\Desktop\Data\Sample\Location', i, '\Results\Variable ', i, '.xlsx'), 'PreserveVariableNames', true)
end
I am unsure if I am heading in the right direction to write this script.
But I am stuck here as I will either get the error that 'Input must be text.' or 'For colon operator with char operands, first and last operands must be char'. What should I do?
Thanks for the help in advance :)

採用された回答

Wan Ji
Wan Ji 2021 年 8 月 26 日
編集済み: Wan Ji 2021 年 8 月 26 日
Try following code
% Input number of sites
N = input('Number of Location? ', 's');
N = str2double(N)
% Loop to read all files
T = table;
for i = 1:N
Ti = readtable(['C:\Users\tathuen\Desktop\Data\Sample\Location', num2str(i),...
'\Results\Variable ', num2str(i), '.xlsx'], 'PreserveVariableNames', true);
T = [T;Ti];
end
  5 件のコメント
Wan Ji
Wan Ji 2021 年 8 月 26 日
編集済み: Wan Ji 2021 年 8 月 26 日
Hi Tat Onm Huen,
Is the Number of Location an array or just a single number?
If it is an array then
% Input number of sites
N = input('Number of Location? ', 's');
N = str2num(N)
% Loop to read all files
T = table;
for k = 1:numel(N)
i = N(k);
Ti = readtable(['C:\Users\tathuen\Desktop\Data\Sample\Location', num2str(i),...
'\Results\Variable ', num2str(i), '.xlsx'], 'PreserveVariableNames', true);
T = [T;Ti];
end
Tat Onn Huen
Tat Onn Huen 2021 年 8 月 26 日
編集済み: Tat Onn Huen 2021 年 8 月 26 日
Yes, it is an array. Thanks for your help! It works now :) @Wan Ji

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

その他の回答 (0 件)

カテゴリ

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