MATLAB Answers


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

Asked by Sandra Holthe on 6 Feb 2019
Latest activity Answered by 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?
datasetF00084 = xlsread('EV_F00084.xlsx','Sheet2','B1:C120000');
xF00084 = datasetF00084(:,1);
yF00084 = datasetF00084(:,2);
datasetF00085 = xlsread('EV_F00085.xlsx','Sheet2','B1:C120000');
xF00085 = datasetF00085(:,1);
yF00085 = datasetF00085(:,2);
datasetF00093 = xlsread('EV_F00093.xlsx','Sheet2','B1:C120000');
xF00093 = datasetF00093(:,1);
yF00093 = datasetF00093(:,2);


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
hold on;
hold off;
hold on;
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).
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.

1 Answer

Answer by 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)];


Sign in to comment.