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

조회 수: 3 (최근 30일)
Frank Moldenhauer
Frank Moldenhauer 2019년 1월 11일
답변: Steven Remington 2019년 1월 15일
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
Frank Moldenhauer
Frank Moldenhauer 2019년 1월 14일
Meanwhile solved using a C MEX function, but may be there is a trick to do this directly in Matlab?

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

답변 (1개)

Steven Remington
Steven Remington 2019년 1월 15일
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

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by