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

2 次查看(过去 30 天)
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!

采纳的回答

Steven Lord
Steven Lord 2019-8-13
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 个评论
Matthew Trahan
Matthew Trahan 2019-8-13
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Calendar 的更多信息

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by