필터 지우기
필터 지우기

Reading excel data from file and using datetick

조회 수: 2 (최근 30일)
Ye
Ye 2015년 8월 11일
답변: dpb 2015년 8월 11일
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.

채택된 답변

dpb
dpb 2015년 8월 11일
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.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by