Clear Filters
Clear Filters


5 views (last 30 days)
zhexuan on 6 Mar 2024
Answered: UDAYA PEDDIRAJU on 14 Mar 2024
在命令窗口行输入代码希望导出数据到excel表格,但是报错The file name 'C:\Users\Jason\Desktop\fangbo.xlsx' is invalid because it does not have the extension ".mdl" or".slx".

Answers (1)

UDAYA PEDDIRAJU on 14 Mar 2024
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: 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:
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:, or you can log the data first to the workspace then to the excel:


Find more on Data Import from MATLAB in Help Center and File Exchange


Community Treasure Hunt

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

Start Hunting!