Dataset Import From Excel

3 views (last 30 days)
ss4johnny Hall
ss4johnny Hall on 16 Nov 2011
I have an excel file with one column of dates and several columns of data (there are some #N/As, but xlsread doesn't have a problem).
Anyway, I have some code that imports the data and then creates a dataset array with different Vars for each series (rather than as one big array). I've tried other ways to do this directly through the dataset function since it seems to say it can do something similar to this in the documentation. However, I have never been able to get it to work. For instance, if I just run: a=dataset('XLSFile',file,'ReadVarNames',true,'ReadObsNames',true);
then I get the following error:
??? Error using ==> setvarnames at 26 NEWNAMES must be a nonempty string or a cell array of nonempty strings.
Error in ==> dataset.dataset>readXLSFile at 723 a = setvarnames(a,varnames(:)'); % names will be modified to make them valid
Error in ==> dataset.dataset>dataset.dataset at 353 a = readXLSFile(a,xlsfileArg,otherArgs);
Anyway, I tried a bunch of combinations, but could never get it to work. (I thought converting the date strings in excel to numbers would work or using the actual ObsNames I already imported, but no luck).
This is the code I use currently:
file=strcat(setdir,source);
[num,txt,~]=xlsread(file);
name=txt(1,:);
date=txt(2:end,1);
date_val=datenum(date(:));
num={[date_val num]};
data=dataset([num,name{:}],'ObsNames',date);

Answers (1)

Sebastian Arslanogullari
Sebastian Arslanogullari on 26 Nov 2011
I would guess that your excel file contains cells that looks empty to the eye but, according to matlabs dataset function, is not.
Try to open your Excel sheet and delete a few (or all) rows below your current data as well as all columns to the right, even though they look empty to you. Then try again. Please let me know if it workes. For some reason, xlsread seems more robust to these kind of problems than datasets internal readXLSFile function.
Best Regards Sebastian

Categories

Find more on Data Import from MATLAB 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!