Meanwhile solved using a C MEX function, but may be there is a trick to do this directly in Matlab?
Convert System.Byte[] memory dump to numeric data without fwrite/fread?
조회 수: 3 (최근 30일)
이전 댓글 표시
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개)
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
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!