How do I display simulink simulation data as a table

51 次查看(过去 30 天)
I would like to generate a simple table with the data from a Simulink simulation. Basically just a table with column labels in the top row, time in the first column and the data from logged values in each column. How do I generate this table?
I just want my model to put out something I can read from in Matlab easily to generate other tables. I have been using the export function in the simulink data inspector to get the data to excel, but I would like to move to dealing with the tables in Matlab as I have to run a bunch of simulations simultaneously and compile the data from multiple simulations into tables.
I am struggling finding ways to reference the data from simulink output in codes to write scripts to generate the tables that I want and at this point can't seem to reference the output data.

采纳的回答

Ayush Modi
Ayush Modi 2024-4-12
Hi Dieter,
I understand you want to get the output of simulink model in the MATLAB workspace, so that you can create a table from that data.
Please refer to the following MathWorks documentation for information on how to save or log the simulation data to the workspace:
Once you have the data in the workspace, here is how you can create a table with the specified requirement:
% Assuming your data is logged to a variable named 'logsout'
% and you want to create a table with time and these signals
% tout is the time output from the simulation data
% Initialize a table with the time column
T = table(tout, 'VariableNames', {'Time'});
% Loop through each element in the dataset to add it to the table
for i = 1:logsout.numElements
% Get the signal name
signalName = logsout.getElement(i).Name;
% Get the signal data
signalData = logsout.getElement(i).Values.Data;
% Add the data as a new column in the table
T.(signalName) = signalData;
end
% Now, T is a table with time and all your logged signals
Hope this helps!
  2 个评论
Dieter
Dieter 2024-4-16
Thank you so much for your help. I think I am getting closer. I have it somewhat working but it stops for some reason on the 8th column and doesn't continue to the final column (Column 186). I had to make some mods to call up the right locations but I think I have done essentially what you have said.
It seems there is a mismatch in the number of rows and the height of the table but I can do it manually if I just put the number in as 8 instead of having the loop calculate it. Any idea why it would stop here?
Error using . (line 507)
To assign to or create a variable in a table, the number of rows must match the
height of the table.
Error in TableGenerationScript (line 11)
T.(signalName)=signalData;
Here's the code:
%Generate a table with a time column and the variable name set to time.
Time=out.tout
Data = table(Time, 'VariableNames', {'Time'});
%Loop through each dataset element to add it to the table
for i=1:out.logsout.numElements
%Get signal name
signalName=out.logsout.getElement(i).Name;
%Get signal data
signalData=out.logsout.getElement(i).Values.Data;
%Add the data as a new column in the table
Data.(signalName)=signalData;
end
Dieter
Dieter 2024-4-17
I resolved the second issue. Thanks for the help. Turned out I had different vector formats being put out from signal converters and once I set those all to 1-D array this method worked perfectly.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Sources 的更多信息

产品


版本

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by