How to get the datatype from the signal line to excel after compiling the model?

17 次查看(过去 30 天)
Hello,How to extract the datatype present on the signal line for input and outpur to an excel after compiling the model ctrl+D. along with the input and output names?
In the attached screenshot, it shows both input and output has double but in reality I have several inputs with different dataypes.
Any ideas on how to do this?
Thank you!

回答(2 个)

Umar
Umar 2024-10-15

Hi @jenny ,

After reviewing the documentations provided at the links below

https://www.mathworks.com/help/matlab/ref/writetable.html

https://www.mathworks.com/help/matlab/ref/table.html

https://www.mathworks.com/help/matlab/import_export/ways-to-import-spreadsheets.html

Here is how you can extract the data types of signals in a Simulink model and write them, along with their names, to an Excel file after compiling the model (Ctrl+D), by following these detailed steps:

Step 1: Access Signal Information

Open Your Simulink Model: Load your model in Simulink. Signal Logging: Ensure that you have signal logging enabled for the input and output signals you are interested in. Right-click on the signal line and select Log Selected Signals. Model Configuration: Go to Simulation> Model Configuration Parameters> Data Import/Export and ensure that the Log signals option is checked.

Step 2: Compile the Model

Compile the Model: Press Ctrl+D to compile your model. This will create a structure containing information about all the signals.

Step 3: Access Signal Data Types

Use MATLAB Command Window: After compilation, you can access the logged signals using:

   logsout = get_param('your_model_name', 'SimulationOutput');

Extract Signal Information: Loop through the logged signals to extract their names and data types:

   signalInfo = logsout.get('YourSignalName'); % Replace with actual signal    
   name
   signalType = class(signalInfo.Values.Data);

Step 4: Create a Table for Excel Export

Prepare Data for Excel: Initialize arrays to hold names and data types:

   inputNames = {'Input1', 'Input2'}; % Add your actual input names here
   outputNames = {'Output1', 'Output2'}; % Add your actual output names here
   dataTypes = {'double', 'int32'}; % Replace with actual data types extracted

Create a Table: Combine the names and data types into a table:

   T = table(inputNames', dataTypes', 'VariableNames', {'SignalName', 
   'DataType'});

Step 5: Write to Excel

Export Table to Excel: Use the writetable function to write your table to an Excel file:

   filename = 'signal_data.xlsx';
   writetable(T, filename, 'Sheet', 1, 'Range', 'A1');

Make sure that you have MATLAB's Excel integration enabled (typically available on Windows) to use writetable directly with .xlsx files. If you want to preserve formatting or adjust cell sizes in Excel, consider using COM automation (via actxserver) for more control over the appearance of your spreadsheet. Here is how you might put it all together in MATLAB:

% Assuming your model is already loaded and compiled
% Accessing simulation output
logsout = get_param('your_model_name', 'SimulationOutput');
% Initialize arrays for names and types
signalNames = {'Input1', 'Input2', 'Output1', 'Output2'};
dataTypes = cell(length(signalNames), 1);
% Extract data types
for i = 1:length(signalNames)
  sigInfo = logsout.get(signalNames{i});
  dataTypes{i} = class(sigInfo.Values.Data); % Get the data type
end
% Create table for exporting
T = table(signalNames', dataTypes', 'VariableNames', {'SignalName', 
'DataType'});
% Write to Excel file
filename = 'signal_data.xlsx';
writetable(T, filename, 'Sheet', 1, 'Range', 'A1');

This process will ensure that you have a comprehensive overview of all signal names and their corresponding data types written neatly into an Excel file for further analysis or documentation purposes.

Hope this helps.

Please let me know if you have any further questions.

  2 个评论
Jenny
Jenny 2024-10-29,21:29
Hello Umar,
Thank you so much for taking time in answering my question!
So, below code, should I manually write the input and output names? The signal logging will not take the inport and outport names automatically? If it does not, is there any way to get the signal names instead if manually writing?
% Initialize arrays for names and types
signalNames = {'Input1', 'Input2', 'Output1', 'Output2'};
dataTypes = cell(length(signalNames), 1);
Thank you!
Umar
Umar 2024-10-30,0:08

Hi @Jenny/Yojitha ,

When you enable signal logging for your Simulink model, it does capture the names of the logged signals. However, in my initial code example, it requires you to define these names manually in the signalNames array. I know this can indeed be cumbersome if there are many signals or if they change frequently. So, instead of hardcoding signal names, you can programmatically retrieve all logged signal names directly from the logsout object after compiling your model. Here is how you can do this:

   % Accessing simulation output
   logsout = get_param('your_model_name', 'SimulationOutput');
   % Get all signal names dynamically
   signalNames = logsout.getElementNames();
   % This retrieves all logged signal names
   dataTypes = cell(length(signalNames), 1);
   % Extract data types for each signal
   for i = 1:length(signalNames)
       sigInfo = logsout.get(signalNames{i});
       dataTypes{i} = class(sigInfo.Values.Data); 
       % Get the data type
   end
   % Create table for exporting
   T = table(signalNames', dataTypes', 'VariableNames', 
   {'SignalName','DataType'});
   % Write to Excel file
   filename = 'signal_data.xlsx';
   writetable(T, filename, 'Sheet', 1, 'Range', 'A1');

If your model structure changes (e.g., adding or removing signals), this method ensures that your code remains functional without needing updates to the hardcoded names.

Hope this helps.

If you still have any further questions or need additional assistance, feel free to ask!

请先登录,再进行评论。


Paul
Paul 2024-10-30,17:09
Hi jenny,
Signal names on the outputs of a block can be found via:
names = get_param(block,'OutputSignalNames');
The port (in and out) data types can be found via:
feval(sys,[],[],[],"compile");
dt = get_param(block,"CompiledPortDataTypes")
feval(sys,[],[],[],"term");

类别

Help CenterFile Exchange 中查找有关 Save Run-Time Data from Simulation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by