Convert System.Byte[] memory dump to numeric data without fwrite/fread?

2 views (last 30 days)
Hi,
from a .NET application I get System.Byte[] array data that contain memory dumps with numerical data of several types (int16, int32, ieee float) inside. I want to process these data in Matlab.
>> whos data
Name Size Bytes Class Attributes
data 1x1 8 System.Byte[]
>> data
data =
Byte[] with properties:
Length: 65536
LongLength: 65536
Rank: 1
SyncRoot: [1×1 System.Byte[]]
IsReadOnly: 0
IsFixedSize: 1
IsSynchronized: 0
The following code does the job for single precision float data, it returns a 16384 array fdata made from the 4 x 16384 = 65536 bytes (other data types similar):
% convert to matlab array, see
% https://de.mathworks.com/matlabcentral/answers/46827-fast-transfer-of-net-array-to-matlab-array
bdata = data.uint8;
% write memory dump to file
fid = fopen('tmp.bin', 'w');
fwrite(fid, bdata);
fclose(fid);
% read file e.g. as ieee single precision float
fid = fopen('tmp.bin', 'r');
fdata = fread(fid, inf, 'single');
fclose(fid);
Is there a way to to this faster in memory without using a temporary file?
Thanks & regards,
Frank
  1 Comment
Frank Moldenhauer
Frank Moldenhauer on 14 Jan 2019
Meanwhile solved using a C MEX function, but may be there is a trick to do this directly in Matlab?

Sign in to comment.

Answers (1)

Steven Remington
Steven Remington on 15 Jan 2019
There are a couple options. MATLAB has a typecasting function which allows you to convert data types.
More information about that process can be found at the follow doc page:
With that you should be able to select columns from your data in order to convert to the correct data type.
Another option would be to use "textscan" with a format to bulk read data of different data types from the file if you have a complex formatted file. Some examples of this is shown below:
https://www.mathworks.com/help/matlab/ref/textscan.html

Categories

Find more on Large Files and Big Data in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!