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

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by