Convert C code to Matlab code for reading binary file
이전 댓글 표시
I'm looking for help reading a binary file in the most efficient way (fast). Here is the C-code. Thanks.
#define MAX_LAYERS 100
typedef struct
{
char string[1000];
} STRINGARRAY_STRUCT;
typedef struct
{
double rangeFinals;
double crossFinals;
STRINGARRAY_STRUCT time[MAX_LAYERS]; // Sample: xx:xx:xx
double dir[MAX_LAYERS]; // degrees
double speed[MAX_LAYERS]; // knots
int flagSpeed[MAX_LAYERS]; // flag speed
int flagDir[MAX_LAYERS]; // flag direction
int flagTime[MAX_LAYERS]; // flag time
int percentTraversed;
int year[MAX_WIND_LAYERS];
int jDay[MAX_WIND_LAYERS];
} CURRENT_STRUCT;
typedef struct
{
char string[1000];
} STRINGARRAY_STRUCT;
int main (int argc, char **argv)
{
FILE *fp;
CURRENT_STRUCT winds;
fp = fopen (FileName, "rb");
if (fp != NULL)
{
fread (&winds, sizeof(CURRENT_STRUCT), 1, fp);
fclose (fp);
}
}
답변 (1개)
Les Beckham
2023년 3월 21일
Your Matlab struct doesn't match the definition in the C code. For example, the third element is a char array of 100x1000 in the C code and a 100x100 array in the Matlab code, flagDir and flagTime are arrays in the C code (of some unknown length, since you didn't show the definition of MAX_WIND_LAYERS), but scalar in the Matlab code. Also, some of the names don't match, which may or may not be an issue.
If you provide a sample of the data file it will be easier to get an accurate answer.
I think you are going to have to read the struct in pieces and copy those pieces into your struct.
For example:
rangeFinals = fread(fp, 1, 'double');
crossFinals = fread(fp, 1, 'double');
time = fread(fp, [100, 1000], 'char');
...
s.rangeFinals = rangeFinals;
s.crossFinals = crossFinals;
...
댓글 수: 2
Mike D.
2023년 3월 21일
Les Beckham
2023년 3월 21일
Unfortunately, I don't think you can do it all at once in Matlab like you can in C.
카테고리
도움말 센터 및 File Exchange에서 Large Files and Big Data에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!