Is it possible to optimize converting a C# array of serial times (double) to a Matlab array of datetimes?

1 view (last 30 days)
Here's an example of what I'm doing:
function outputArg = TimeArray(obj)
timeArray = DotnetClass.GetTimeArray(obj.DotnetArray);
count = timeArray.Length;
outputArg = NaT(1, count);
for i=1:count
netTime = timeArray(i);
matTime = datetime(netTime, "ConvertFrom", "datenum");
outputArg(i) = matTime;
end
end
I'm converting an array of DateTimes in C# to a serial date which comes across as a double. I then need to convert these to a matlab array of datetimes for plotting.
The above implementation is taking about 30 seconds for ~110k datenums. I've used cellfun and it takes about 4 times as long, so I've ruled that out.
Thanks for any help!

Accepted Answer

Steven Lord
Steven Lord on 13 Aug 2019
datetime can convert an array of serial date numbers at once, and this avoids the overhead of calling datetime repeatedly.
>> D = datenum(datetime('now') + 10*randn(110000, 1));
>> timeit(@() datetime(D, 'ConvertFrom', 'datenum'))
This took a (small) fraction of a second on my machine.
  2 Comments
Matthew Trahan
Matthew Trahan on 13 Aug 2019
This would be perfect, but for me, timeArray is a System.Object[] (Object because the cell(timeArray) method requires it to be an object, but I have the same results with a System.Double[]).
I can convert timeArray to a cell array with
cellTimeArray = cell(timeArray);
but it ends up in a format like
{[7.3725e+05]} {[7.3725e+05]} {[7.3725e+05]} {[7.3725e+05]}
so I guess it's not quite converted how I need.

Sign in to comment.

More Answers (0)

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!