How can I insert time from an xlsx file into Matlab and make a plot?
3 次查看(过去 30 天)
显示 更早的评论
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 个评论
YT
2019-2-6
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).
回答(1 个)
YT
2019-2-6
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Spreadsheets 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!