How to read a custom file with data, and search and parse
조회 수: 44 (최근 30일)
이전 댓글 표시
I am trying to read files with a custom format, where I need (lack of better word) the raw data of the file so I can search and parse it. So far I am doing trying with fopen and fread to get the data.
fileID = fopen('myfile.customformat');
binaryString = fread(fileID);
So the files I need to parse have a header with a string (4 bytes) followed by uint32 containing the size of the following subsequent data. The data of interest, you could say, is an array of data sets, all with their own header string (4 bytes), and size (uint32) for all the points in one set. Essentially I just need to search for the header for each data set and extract the data, but the way I am reading the data right now with fread, I get the data in an array of dec, which does not seem that well to parse e.g. searching for the header strings.
What approach should I take instead? Below is, hopefully, a better overview of the data structure in the file.
Example of the data structure
- file header (string, 4 bytes)
- size of data package (uint32, 4 bytes)
- .. random irrelevant data
- dataset1_header (string, 4 bytes )
- dataset1_size (uint32, 4 bytes)
- dataset1_data (20x float)
- dataset2_header (string, 4 bytes )
- dataset2_size (uint32, 4 bytes)
- dataset2_data (20x float)
댓글 수: 0
답변 (1개)
Dave Behera
2016년 4월 4일
fread is used to read from a binary file. Can you try using fscanf and fileread?
참고 항목
카테고리
Help Center 및 File Exchange에서 String Parsing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!