.netmatlabfor-loopoptimizationpre-allocation

Preallocating a Matrix of .NET Arrays


I'm using Matlab to setup the data acquisition process for an experiment, the function ain.DAQbufferGet is what reads the data from our hardware. It holds a series of data points such as [-200,-160,10,50,-20,40,170,160,-200... etc].

The problem is that the output of DAQbufferGet is a 1x1 System.Int16[]

Such an output can also be created by using

`NET.createArray('System.Int16', 50000)` 

Here 50000 is just an example number

I want to store the output of DAQbufferGet in a matrix without having to convert it to a different data type, and to be able to plot it later (outside the loop), as shown here:

data = int16(zeros(10, 50000));

for k = 1:10
    data(k,:) = int16(zeros(1,50000));
end

for i = 1:10
    data(i,:) = int16(ain.DAQbufferGet());
end

I have had difficulty doing something similar with the 1x1 System.Int16[] data type

How would I do this?

Specifically preallocate a matrix of .NET arrays which can later be written to in the for loop described above.


Solution

  • It seems that storing the .NET array in a cell means you can later extract it and index as such

    for k = 1:10
    data{k} = NET.createArray('System.Int16', 50000);
    end
    
    for i = 1:10
    data{i} = ain.DAQbufferGet();
    end
    

    data{i} returns a .NET array which can be converted to another data type and plotted