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

1 次查看(过去 30 天)
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:

回答(2 个)

Gowtham
Gowtham 2023-10-10
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 2023-10-10
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?

类别

Help CenterFile Exchange 中查找有关 Discrete-Event Simulation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by