Can I use fwrite for .mat file in Simevents block?

1 visualización (últimos 30 días)
Ho Jung Kim
Ho Jung Kim el 4 de Ag. de 2022
Respondida: Walter Roberson el 10 de Oct. de 2023
I have a .mat file called 'counter.mat' and there is one value called 'var' initialized to 0. I would like to change the value and save it back to the .mat file, but it seems like Simulink/Simevents doesn't support 'save' command (I'm getting code generation error). I found out Simulink/Simevents support fopen and fwrite, however, the value doesn't change using fwrite. Is there a way to make this work? Or should I make a .bin file to do this? If so, could you please show me how to do it? Thank you.
I have posted my code below:

Respuestas (2)

Gowtham
Gowtham el 10 de Oct. de 2023
Hello
I understand you want to edit (update) a variable ‘var’ in a MAT file, ‘counter.mat’ in Simulink / Simevents.
It is worth noting that the current workflow may not be the most efficient. It is advisable to avoid writing to a file at every time step.
However, if your intention is to keep track of a variable, there are alternative approaches available. You can utilize persistent variables or Data Store Memory (DSM). If you need to save the data for future reference, you can output the variable and log it accordingly.
Kindly refer to the following MATLAB documentation for further information on how to use ‘Persistent Variables’ and ‘Data Store Memory’
Hope this helps.
Best Regards,
Gowtham

Walter Roberson
Walter Roberson el 10 de Oct. de 2023
It is possible ... but it isn't at all nice.
If you create the file with a save() command with the -nocompression option, then the value of the variable will be saved at a predictable offset into the file. You can fseek() to that location and fwrite() a new value and fclose()
... But if you are going to go to that trouble, then why not just use a binary file to start with?

Categorías

Más información sobre Discrete-Event Simulation en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by