MATLAB Answers

How can I insert time from an xlsx file into Matlab and make a plot?

2 views (last 30 days)
Sandra Holthe
Sandra Holthe on 6 Feb 2019
Answered: YT on 6 Feb 2019
I am trying to read an excel file and plot the data with time on the x-axis and energy on the y-axis. The y-axis works fine but the the time is given in this format: 0.569039351851852, instead of 10:26:39. Also, I am inserting data from several xlsx files and trying to plot the information in seperate graphs, instead, figure 1 works fine but 2 and 3 are placed on top of the information from figure 1. Does anyone have a solution two my two problems?
Best.
%-----------------------------------EV_F00084--------------------------------------------------
datasetF00084 = xlsread('EV_F00084.xlsx','Sheet2','B1:C120000');
xF00084 = datasetF00084(:,1);
yF00084 = datasetF00084(:,2);
figure(1)
plot(xF00084,yF00084,'*')
%-----------------------------------EV_F00085--------------------------------------------------
datasetF00085 = xlsread('EV_F00085.xlsx','Sheet2','B1:C120000');
xF00085 = datasetF00085(:,1);
yF00085 = datasetF00085(:,2);
figure(2)
plot(xF00085,yF00085,'*')
%-----------------------------------EV_F00093--------------------------------------------------
datasetF00093 = xlsread('EV_F00093.xlsx','Sheet2','B1:C120000');
xF00093 = datasetF00093(:,1);
yF00093 = datasetF00093(:,2);
figure(3)
plot(xF00093,yF00093,'*')

  2 Comments

YT
YT on 6 Feb 2019
I'm not sure why the plots not working correctly. Have you tried to use hold on / hold off to see if that helps? And did try to use close all at the beginning of your script to close previous figures?
close all; %closes previous figures
%... other code
figure;
hold on;
plot(xF00085,yF00085,'*')
hold off;
figure;
hold on;
plot(xF00093,yF00093,'*')
hold off;
Maybe it's also useful if you provide one of the .xlsx files as an attachment to your question (to help you with importing the time correctly).
Sandra Holthe
Sandra Holthe on 6 Feb 2019
Thanks for the tips. Unfortunately it did not work when closing and using the hold on/off commands. I attached an example of one of the excel files for the time problem.

Sign in to comment.

Answers (1)

YT
YT on 6 Feb 2019
Even though I still haven't figured out your plotting issue, the importing can be solved like so
% first time;
opts = detectImportOptions('EVdata_test.xlsx');
opts.VariableNames = {'Time','EnergyStored'};
opts = setvartype(opts,'Time','duration'); %read in as duration type
opts = setvartype(opts,'EnergyStored','double');
opts.Sheet = 'Sheet1';
opts.DataRange = 'B2:C69';
T = readtable('EVdata_test.xlsx',opts);
T = [table(datestr(T{:,1},'hh:MM:SS'),'VariableNames',{'Time'}) T(:,2)];
% second time; options only need to be set once if sheet and range stays the same
T = readtable('EVdata_test2.xlsx',opts);
T = [table(datestr(T{:,1},'hh:MM:SS'),'VariableNames',{'Time'}) T(:,2)];

  0 Comments

Sign in to comment.