Hi zhexuan,
The problem appears to arise from attempting to use the "sim" function directly with an Excel file, which is not its intended use. The "sim" function is designed to run Simulink models, not to handle Excel files. The correct approach is to first run the Simulink model, save the output data to variables, and then use "xlswrite" or "writematrix" (recommended, as "xlswrite" might be unsupported in future MATLAB versions: https://www.mathworks.com/help/matlab/ref/xlswrite.html) to write the data to Excel.
1) Run Your Simulation: First, execute your model simulation and collect the output. You can do this with a command like:
out = sim('modelName'); %Replace 'modelName' with the actual name of your Simulink model.
2) Access Your Logged Data: The simulation output is stored in a "Simulink.SimulationOutput" object. To access the data, you can use:
out.recordout; %This command returns a Simulink.SimulationData.Dataset object containing your logged data.
3) Extract Specific Data: To get data for a specific signal, you would use a command similar to:
sqWaveData = get(out.recordout,3).Values.Data; %Here, 3 should be replaced with the index of your desired signal within the dataset.
4) Export Data to Excel: Finally, to write your data to an Excel file, you can use the "writematrix" function like so:
matlab
writematrix(sqWaveData, 'filename.xlsx'); %Just replace 'filename.xlsx' with your preferred file name and path.
you can refer to the documentation for more detais and other methods: using record block: https://www.mathworks.com/help/simulink/ug/log-data-to-the-workspace-and-a-file-using-the-record-block.html, or you can log the data first to the workspace then to the excel: https://www.mathworks.com/help/simulink/slref/scope.html.