フィルターのクリア

For loop to run code multiple times for different excel sheets

12 ビュー (過去 30 日間)
Fatemah Ebrahim
Fatemah Ebrahim 2020 年 5 月 11 日
回答済み: Harsha Priya Daggubati 2020 年 5 月 14 日
I have a code that I wrote for one excel sheet and I need it to run for 5 more excel sheets how can I do this using a for loop?
  2 件のコメント
Geoff Hayes
Geoff Hayes 2020 年 5 月 11 日
Fatemah - what does your code look like for the one sheet? Are you just changing the sheet name on each iteration? What should be done with the data that you read?
Fatemah Ebrahim
Fatemah Ebrahim 2020 年 5 月 11 日
This is my code looks like for the first sheet
% Unit A:
clc;
clear;
data = readtable('A_minute.xlsx');
% step 2: store date and time data as a datenum (DaT) and then convert to datetime (t)
DaT = datenum(data.Var1);
t = datetime(DaT, 'ConvertFrom','datenum');
TimeofDay = timeofday(t);
TimeofDay = ceil(TimeofDay,'hours');
[UniqueTimeSteps,~,pos] = unique(TimeofDay);
UniqueTimeSteps = UniqueTimeSteps(2:end);
pos =pos -1;
pos(pos==0) = 24;
% step 3: store the other columns from the excel data file
AptA = data(:,4);
AptA = table2array(AptA);
% step 4: for loop extracting the hour and indexing it through for weekday versus weekend
Consumption_Weekday = zeros(1,length(UniqueTimeSteps));
Consumption_Weekend = zeros(1,length(UniqueTimeSteps));
wd = weekday(t) > 1 & weekday(t) < 7;
for i = 1:length(UniqueTimeSteps)
idx = (pos == i & wd);
Consumption_Weekday(i) = mean(AptA(idx));
idx = (pos == i & (~wd));
Consumption_Weekend(i) = mean(AptA(idx));
end
MinuteValue= UniqueTimeSteps;
% step 5: plot
figureA = figure;
plot(MinuteValue,Consumption_Weekday,'-b');
hold on
plot(MinuteValue,Consumption_Weekend,'-r');
xlabel('Time'), ylabel('Electric Demand (kW)'), ...
title('Unit A - Average Aggregated HVAC Electric Demand (kW)'), ...
legend('Weekdays','Weekends')

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

回答 (1 件)

Harsha Priya Daggubati
Harsha Priya Daggubati 2020 年 5 月 14 日
Hi,
If your excel workbook has multiple sheets, then this thread might help you give an idea about how to proceed.
If your data is spread across multiple excel files, then you can get access the excel files using 'dir' command. This thread might help you.

カテゴリ

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