Is there a way to simulate a struct with a array?
显示 更早的评论
Hello friends?
I need to implement some code I have done in C, and conver it to matlab. Can someone help me to simulate the following struct in matlab array?
typedef struct _RTC_C_Calendar
{
uint32_t seconds;
uint32_t minutes;
uint32_t hours;
uint32_t dayOfWeek;
uint32_t dayOfmonth;
uint32_t month;
uint32_t year;
} RTC_C_Calendar; // 28 bytes
struct long_byte {
union {
uint32_t val;
uint8_t id[4];
};
};
回答(1 个)
Walter Roberson
2022-9-27
RTC_C_Calendar = zeros(Rows, 7,'uint32');
Now create variable names like dayOfWeek = 4;
and then you can index RTC_C_CALENDAR(row, dayOfWeek) and then the rest of your code does not need to know what the order of the fields is.
6 个评论
Walter Roberson
2022-9-27
Reminder you can num2cell and cell2struct to convert an array to struct.
Biza Ferreira
2022-9-27
移动:Steven Lord
2022-9-27
For operations on your long_byte use typecast, potentially in conjunction with swapbytes.
x = int8([12, 34, 56, 78])
y = typecast(x, 'int32')
format hex
x
y
swapbytes(y)
Walter Roberson
2022-9-27
Why convert it to hexadecimal?
Perhaps read the binary using memmapfile()?
Biza Ferreira
2022-9-27
Walter Roberson
2022-9-27
See the example at https://www.mathworks.com/help/matlab/ref/memmapfile.html#bt3tmd4-1 for handling effectively a repeated struct
类别
在 帮助中心 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!