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

1 Ansicht (letzte 30 Tage)
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!

Akzeptierte Antwort

Steven Lord
Steven Lord am 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 Kommentare
Matthew Trahan
Matthew Trahan am 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.
Matthew Trahan
Matthew Trahan am 13 Aug. 2019
Scratch that, when I return it as a System.Double[], it works. Thank you so much!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Produkte


Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by