I'm reading a bunch of excel files and loading them into a SQL database using matlab. I'm reading in columns as a cell arrays, and then I want to go through and get rid of rows for which the 'dateColumn' column of dates has a bad value.
Currently, I am making an vector of booleans where 1 means the date is good:
arrayOfBools = cellfun(@(x) ~isnan(datenum(x)),dateColumn);
And then truncating the rest of the columns by that vector. For example:
Ticker = Ticker(arrayOfBools);
The problem is that I have 35,000 rows in each spreadsheet, and the arrayOfBools = cellfun(@(x) ~isnan(datenum(x)),dateColumn); takes 55 seconds, which is too long given the number of spreadsheets I have to go through. Is t开发者_StackOverflow社区here a way I can do this more efficiently?
You should be able to pass your cell array of strings dateColumn
directly to DATENUM without having to use CELLFUN, which will likely give you an improvement in speed:
arrayOfBools = ~isnan(datenum(dateColumn));
However, DATENUM appears to have undergone a few updates in some of the recent revisions, and so its behavior (and the validity of the above solution) will depend greatly on which version of MATLAB you are using. In the newest version (R2011a) it appears that passing a cell array of strings as input will return a character array of converted data strings instead of a vector of date numbers (see the last example in the Examples section). This is different from R2010b, where passing a cell array of strings returns a vector of date numbers.
However, when trying out some different date strings in R2010b I never got a NaN
resulting from an invalid date string. It simply throws an error. I'm guessing the code you are running (which looks for NaN
output) is for an older MATLAB version. You should specify that version, along with some sample code, in your question to make it easiest for us to help you.
精彩评论