Reading excel data from file and using datetick

2 views (last 30 days)
Below is a screenshot of an excel file I have and I am trying to plot the first column as x-axis and second column as y-axis.
So, I wrote the following code
function letsplot
exl = actxserver('excel.application');
exlWkbk = exl.Workbooks;
folder = 'C:\Users\shankyaw\Desktop\icandoit';
FilesNeeded = dir(fullfile(folder,'\*.xlsx'))
sd = length(FilesNeeded)
lBoxList = cell(sd,1);
myRange1 = 'A1:A36000';
myRange2 = 'B1:B36000';
for i = 1:sd
exlFile = exlWkbk.Open(fullfile(folder,FilesNeeded(i).name));
exlSheet1=exlFile.Sheets.Item('sheet1');
if i == 1
time_myar = exlSheet1.Range(myRange1).Value; %%problem is here??
Efield(:,i) = cell2mat(exlSheet1.Range(myRange2).Value);
else
Efield(:,i) = cell2mat(exlSheet1.Range(myRange2).Value);
end
lBoxList{i,1} = FilesNeeded(i).name;
end
timetime = datenum(time_myar); %%problem is here??
stem(timetime,Efield(:,x));
datetick('x','keeplimits','keepticks');
exlWkbk.Close
exl.Quit
end
However, I get the error message that says
Error using datenum (line 179) DATENUM failed.
Error in EFieldPlotting (line 10) timetime = datenum(time_myar);
Caused by: Error using datevec (line 104) The input to DATEVEC was not an array of strings.
I tried both cellstr and datestr as below:
time_myar = cellstr(exlSheet1.Range(myRange1).Value); time_myar = datestr(exlSheet1.Range(myRange1).Value);
But, the error is still there. Can some kind soul help me guide how to overcome this error message.
Thanks.

Accepted Answer

dpb
dpb on 11 Aug 2015
Dates were read as numeric Excel dates; not as string values. See exceltime to convert to Matlab-consistent date numbers if have late revision otherwise, read the notes on the differences between Excel and Matlab and make the fixup manually.
NB: though, unless you have more resolution than displayed in the spreadsheet (always a possibility, but not a given), there isn't enough precision to make the values unique for a major portion of the data shown.

More Answers (0)

Categories

Find more on Dates and Time in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!