필터 지우기
필터 지우기

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

조회 수: 2 (최근 30일)
Ho Jung Kim
Ho Jung Kim 2022년 8월 4일
답변: Walter Roberson 2023년 10월 10일
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