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];
};
};

Réponses (1)

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 commentaires

Reminder you can num2cell and cell2struct to convert an array to struct.
Biza Ferreira
Biza Ferreira le 27 Sep 2022
Déplacé(e) : Steven Lord le 27 Sep 2022
Walter Roberson,
Thanks for your answer, my problem is the following... I have develop a code in C to read the ouput of my ECG data aquisition, in my output I built a file with name INPUT.txt, this file is a raw data file, with C code I have convert it into a hexadecimal file,, now I need do the same to ploat it to a graph, this way I decide use the matlab app designer, to make a GUI were the user can read the own signs. Can anyone help me with this and construct the same for matlab code?
Thanks in advance...
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>
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
typedef struct {
uint32_t id; // uint32_t id = 4 bytes
volatile int32_t sDaqVals[8]; // long = 4 bytes * 8 = 32 bytes
bool leadoffDetected; // bool = 1 bytes = alinhamento => 4 bytes
int32_t sresultTempResp; // long = 4 bytes
float vbattery;
float temperature;
RTC_C_Calendar datetime;
} ads1292OutputValues; // 4(id) + 32(sDaq) + 1(bool) + 4(int32) = 41 bytes = alinhamento(bool) => 44 bytes + 28(RTC) + 4(float) + 4(float) = 80 bytes
struct long_byte {
union {
uint32_t val;
uint8_t id[4];
};
};
int main(void)
{
FILE *fp;
int ch;
int i;
int j;
int lineSize = 4;
struct long_byte id;
ads1292OutputValues rawData;
if ((fp = fopen("INPUT.txt", "rb+")) == NULL) {
printf("O arquivo nao pode ser aberto.\n");
}
else {
i = 0;
j = 0;
while (!feof(fp)) {
fread(&rawData, sizeof(ads1292OutputValues), 1, fp);
printf("id: %d, val[8]: %08x, %08x, %08x, %08x, %08x, %08x, %08x, %08x, %d, %08x, %.6f[v], %.6fºC, %02d, %02d-%02d-%04d : %02d:%02d:%02d\n",
rawData.id,
rawData.sDaqVals[0], rawData.sDaqVals[1], rawData.sDaqVals[2], rawData.sDaqVals[3],
rawData.sDaqVals[4], rawData.sDaqVals[5], rawData.sDaqVals[6], rawData.sDaqVals[7],
rawData.leadoffDetected, rawData.sresultTempResp,
rawData.vbattery, rawData.temperature,
rawData.datetime.dayOfWeek,
rawData.datetime.dayOfmonth, rawData.datetime.month, rawData.datetime.year,
rawData.datetime.hours, rawData.datetime.minutes, rawData.datetime.seconds);
}
}
fclose(fp);
printf("\n");
printf("Finished!\n");
return 0;
}
For operations on your long_byte use typecast, potentially in conjunction with swapbytes.
x = int8([12, 34, 56, 78])
x = 1×4
12 34 56 78
y = typecast(x, 'int32')
y = int32 1312301580
format hex
x
x = 1×4
0c 22 38 4e
y
y = int32
4e38220c
swapbytes(y)
ans = int32
0c22384e
Why convert it to hexadecimal?
Perhaps read the binary using memmapfile()?
Hello Walter Roberson,
can you give me some help with this? I dont have a solid knowledge to do this in matlab
See the example at https://www.mathworks.com/help/matlab/ref/memmapfile.html#bt3tmd4-1 for handling effectively a repeated struct

Connectez-vous pour commenter.

Catégories

En savoir plus sur Manage Products dans Centre d'aide et File Exchange

Produits

Version

R2022a

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by