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 Center および File ExchangeCalendar についてさらに検索

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by