Clear Filters
Clear Filters

How to extract multiple excel tabs into MATLAB

19 views (last 30 days)
Mahnoor
Mahnoor on 22 Jun 2023
Commented: Mahnoor on 9 Aug 2023
Hello All,
I have the following code: [num,text] = xlsread('C:\Users\mahnoor.saeed\Documents\P18 Full SRDC MIDAS Simulations');
The excel sheet consists of 40 tabs (each has over 3,000 columns and 20 rows but this is the same in each sheet) but this piece of code only reads the data from the first page of the sheet.
How can I write a loop function to import the data from all 40 tabs as MATLAB variables as I later need to plot certain data.
Any help would be appreciated.
Thankyou.

Answers (1)

Stephen23
Stephen23 on 22 Jun 2023
Do not use deprecated XLSREAD.
It is very odd that your filename does not have a file extension, I fixed that for you:
F = 'C:\Users\mahnoor.saeed\Documents\P18 Full SRDC MIDAS Simulations.xlsx';
S = sheetnames(F);
D = cell(size(S));
for k = 1:numel(S)
D{k} = readtable(F, 'Sheet',S(k));
end
  39 Comments
Mahnoor
Mahnoor on 1 Aug 2023
Ah okay thanks @Stephen23, I was not aware that it was not possible. Will I have to take each input separetly (one for motor and for engine) and do an histogram accordingly and use the same technique:
C = histcounts(T.nEngine,V) * 0.01
C = histcounts(T.nEMotorR,V) * 0.01
Mahnoor
Mahnoor on 9 Aug 2023
Hi @Stephen23, would you please be able to assist with the below.
It seems that I have got some error in my plots using the code below.
clear; close all; %do not comment
P = 'C:\Users\mahnoor.saeed\Documents\P17R Test Cycles'; %do not comment %change only this line
%% Use for plotting AutoBahn Comfort
T = readtable(fullfile(P,'Autobahn Comfort Matlab.xlsx'),'Sheet','Autobahn Comfort');
%% Plot #5 for Engine Torque (MEngine against Time)
figure ; plot (T.Time,T.MEngine, 'LineWidth',0.80)
xlabel('Time (s)'); ylabel('Engine Torque (Nm))')
ylim([-220 1200]); yticks(-220:142:1200)
legend('MEngine', 'Location', 'best')
title ('Engine Torque vs Time')
exportgraphics(gcf, 'Plot #5 for Engine Torque.png','Resolution',300)
%How many seconds is the engine torque in the following conditions?
T = readtable('Autobahn Comfort.xlsx');
T.MEngine;
V = -220:142:1200;
C = histcounts(T.MEngine,V) * 0.01;
Interval_Of_Torque_Nm = V(:);
Duration_Of_Torque_s = [C(:);NaN];
Engine_Torque = table(Interval_Of_Torque_Nm, Duration_Of_Torque_s, 'VariableNames',{'Interval of Engine Torque (Nm)','Duration of Engine Torque (s)'})
figure ; histogram(T.MEngine,V);
xlabel('Engine Torque (Nm)')
legend('Time (s)', 'Location', 'best')
title ('Duration of Engine Torque')
exportgraphics(gcf, 'Histogram #7 for Duration of Engine Torque.png','Resolution',300)
%
%% Plot #6 for Motor Torque (MEMotorR against Time)
figure ; plot (T.Time,T.MEMotorR, 'LineWidth',0.80)
xlabel('Time (s)'); ylabel('MEMotorR (Nm))')
yline(200, '--r', 'Motor torque limit of 200Nm'); yline(-200, '--r', 'Motor torque limit of -200Nm')
ylim([-220 220]); yticks(-220:40:220)
legend('MEMotorR', 'Location', 'best')
title ('Motor Torque vs Time')
exportgraphics(gcf, 'Plot #6 for Motor Torque.png','Resolution',300)
%How many seconds is the motor torque in the following conditions?
T = readtable('Autobahn Comfort.xlsx');
T.MEMotorR;
V = -220:40:220;
C = histcounts(T.MEMotorR,V) * 0.01;
Interval_Of_Torque_Nm = V(:);
Duration_Of_Torque_s = [C(:);NaN];
Motor_Torque = table(Interval_Of_Torque_Nm, Duration_Of_Torque_s, 'VariableNames',{'Interval of Motor Torque (Nm)','Duration of Motor Torque (s)'})
figure ; histogram(T.MEMotorR,V);
xlabel('Motor Torque (Nm)') % The histogram function automatically chooses an appropriate number of bins to cover the range of values in y
legend('Time (s)', 'Location', 'best')
title ('Duration of Motor Torque')
exportgraphics(gcf, 'Histogram #8 for Duration of Motor Torque.png','Resolution',300)
%
For ex. the graph on the right has some additional horizaontal blue line running in the middle (this is not supposed to be there) whilst the one on the left is perfectly fine. I am not sure how this has happened as I have not changed anything in the data of the Excel sheet. I have attached the Excel sheet, would you please be able to have a look at this?
This is the warning I receive in the workspace but not sure what it entirely means:

Sign in to comment.

Categories

Find more on Preprocessing Data in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!