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

조회 수: 1 (최근 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.
Matthew Trahan
Matthew Trahan 2019년 8월 13일
Scratch that, when I return it as a System.Double[], it works. Thank you so much!

댓글을 달려면 로그인하십시오.

추가 답변 (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