从simulink导​出数据到excel命​令窗口行输入代码错误

1 次查看(过去 30 天)
zhexuan
zhexuan 2024-3-6
在命令窗口行输入代码希望导出数据到excel表格,但是报错The file name 'C:\Users\Jason\Desktop\fangbo.xlsx' is invalid because it does not have the extension ".mdl" or".slx".
代码如下:
Out1=sim('C:\Users\Jason\Desktop\fangbo.xlsx',[0,181]);
filename_time='C:\Users\Jason\Desktop\fangbo.xlsx';
sheet=‘sheet1’;
position=‘A1’;
%xlswrite('C:\Users\Jason\Desktop\fangbo.xlsx',t_time,sheet,A);
xlswrite('C:\Users\Jason\Desktop\fangbo.xlsx',Out1,sheet,B);

回答(1 个)

UDAYA PEDDIRAJU
UDAYA PEDDIRAJU 2024-3-14
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.

类别

Help CenterFile Exchange 中查找有关 Data Import from MATLAB 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!