フィルターのクリア

Read binary from a string instead of using fread

9 ビュー (過去 30 日間)
Jack Bryant
Jack Bryant 2023 年 8 月 30 日
回答済み: MYBLOG 2023 年 8 月 30 日
I have a binary file of data, where the binary data is split up periodically by a keyword to indicate segments. I've read in that entire string and split it up around the keywords so that I have each segment individually. I have then joined all these segments into one larger binary string. Is there a way I can read this binary string as if I was using fread on a binary file?
I know in theory I can write the entire string to a new file, and then read that, but is there a more efficient way?
Beautiful paint diagram trying to illustrate my point.

回答 (1 件)

MYBLOG
MYBLOG 2023 年 8 月 30 日
Yes, you can achieve this without writing the entire string to a new file. Instead, you can use the memmapfile function in MATLAB to create a memory-mapped file object that allows you to treat a portion of memory as a binary file. This can be an efficient way to read the concatenated binary data as if you were using fread on a binary file.
Here's how you can do it:
  1. Convert your concatenated binary string into a uint8 array.
  2. Create a memory-mapped file object using memmapfile and specify the Format as 'uint8'.
  3. Use the memory-mapped file object to read the data as if it were a binary file.
Here's an example:
% Sample concatenated binary string (replace this with your actual data)
concatenated_binary_string = '...'; % Your concatenated binary string here
% Convert binary string to uint8 array
binary_data = uint8(concatenated_binary_string);
% Define the size of each segment and the total number of segments
segment_size = 1000; % Example segment size
total_segments = numel(binary_data) / segment_size;
% Create a memory-mapped file object
mmf = memmapfile('temp.dat', 'Writable', true, 'Format', 'uint8');
% Write the binary data to the memory-mapped file
mmf.Data = binary_data;
% Read a segment from the memory-mapped file
segment_number = 1; % Example segment number
start_index = (segment_number - 1) * segment_size + 1;
end_index = start_index + segment_size - 1;
segment = mmf.Data(start_index:end_index);
% Use the segment as needed
disp(segment);
% Clean up: Close the memory-mapped file
clear mmf;
Keep in mind that memory-mapped files provide a way to access large data sets without loading the entire data into memory at once, which can be more memory-efficient. However, they do involve reading data from disk as needed, so their performance can depend on factors such as disk speed and the size of the segments you are reading.

カテゴリ

Help Center および File ExchangeLarge Files and Big Data についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by