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.