- Ensure the data you want to log and an iteration counter (if needed) are inputs to the MATLAB ‘Function block’.
- You might need to use additional blocks (like ‘Signal Conversion’ or ‘Data Type Conversion’) to ensure compatibility.
How do I save Simulink data to Excel every interation instead of saving the data after the simulation is done
4 次查看(过去 30 天)
显示 更早的评论
I need to simulate In Simulink and log data in excel for a very long time. For example, 100 days. Normally, data is stored into the workspace using to-workspace blocks in Simulink and then send to excel after the simulation is done. However, due to the long simulation in real life many things could happen that could stop the simulation, like a short-circuit. This could result in the loss of my data. Therefore, I cannot permit that the data would not be collected due to unforseen cicumstances. Normally, data is logged after the simulation is done. However, I want the data to store every iteration in excel during the simulation. How can that be done?
0 个评论
回答(1 个)
Sameer
2024-3-27
Hi Jeroen
From my understanding, you need to save data to an Excel file at every iteration during a long Simulink simulation, to prevent data loss from unexpected interruptions. Normally, data is exported to Excel post-simulation, but given the critical nature and duration of your simulations, you're looking for a method that allows for continuous data saving throughout the simulation process.
Saving data to Excel at every iteration during a Simulink simulation presents a unique challenge, primarily due to the overhead associated with writing to an Excel file. However, for critical simulations where data loss cannot be tolerated, such as the scenario you described. Here is a method using a MATLAB ‘Function block’ to write data to Excel at every simulation step:
MATLAB Function Block for Writing Data
You can use a MATLAB ‘Function block’ to execute custom MATLAB code during the simulation. This block can be configured to write data to an Excel file at each simulation step. However, be cautious with performance, as writing to Excel is relatively slow and can significantly slow down your simulation.
Write Custom Code for Logging:
In the MATLAB ‘Function block’, you need to write code that appends data to your Excel file.
Here’s an example code snippet:
function writeDataToExcel(data, iteration)
% Define the Excel file path
excelFilePath = 'C:\path\to\your\file.xlsx';
% Define the range to write data in Excel
% Assuming you write one row of data per iteration
range = ['A' num2str(iteration+1)]; % Excel rows start from 1, and assuming A column
% Write data to Excel file
writematrix(data, excelFilePath, 'Sheet', 1, 'Range', range);
end
In this example, data is the data you want to log, and iteration could be a simulation time or a custom iteration counter. Adjust the path, sheet, and range according to your needs.
Connect Your Data to the MATLAB Function Block:
When you run the simulation, the MATLAB ‘Function block’ will execute your custom code at each simulation step, appending data to the specified Excel file.
Alternative Approach:
For long-duration simulations with critical data logging requirements, consider logging to a ‘.mat’ file or a plain text file (e.g., CSV) during the simulation. These formats are much more efficient for MATLAB to write to. After the simulation, you can then import the data into Excel if needed. This approach balances the need for real-time data preservation with the performance constraints of writing to Excel.
For additional information, please refer to the link below:
I hope this helps!
Sameer
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Import from MATLAB 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!