Is there a way to copy or extract or get the initial values or default Values of input ports of a bus or simulink Model with input signal names?

8 次查看(过去 30 天)
Hello All,
I am trying to extract or copy the initial Values or default values along with the input signal names of bus or simulink Model into a text file using matlab script.what is the Best way to extract the signal names and their defaul values?
Many Thanks in Advance!
Best Regards
JM

回答(1 个)

Abhipsa
Abhipsa 2025-5-2
I understand that you want to automatically retrieve signal names from a Simulink model, get the initial or default values associated with the signals and write this information in a text file using MATLAB script.
This can be done using 2 approaches as shown below.
Approach 1
The below code snippet does the same for constant and sine block using “find_system” method in MATLAB and then iterating over the blocks:
% Define the model name
model = 'BusModel'; % Replace with your model name
load_system(model);
% Find all Bus Creator blocks
busCreators = find_system(model, 'BlockType', 'BusCreator');
% Open file to write output
fileID = fopen('signal_defaults.txt', 'w');
fprintf(fileID, 'Signal Name\tDefault Value\n');
for i = 1:length(busCreators)
blk = busCreators{i};
numInputs = str2double(get_param(blk, 'Inputs'));
lines = get_param(blk, 'LineHandles');
for j = 1:numInputs
line = lines.Inport(j);
if line ~= -1
srcBlock = get_param(line, 'SrcBlockHandle');
srcBlockName = get_param(srcBlock, 'Name');
blockType = get_param(srcBlock, 'BlockType');
defaultVal = 'N/A'; % Default
if strcmp(blockType, 'Constant')
defaultVal = get_param(srcBlock, 'Value');
elseif strcmp(blockType, 'Sin')
try
sineType = get_param(srcBlock, 'SineType'); % This exists for unmasked Sine blocks
if strcmp(sineType, 'Sample based')
% Get mask values
maskValues = get_param(srcBlock, 'MaskValues');
if length(maskValues) >= 4
amp = str2double(maskValues{1});
bias = str2double(maskValues{2});
samplesPerPeriod = str2double(maskValues{3});
offsetSamples = str2double(maskValues{4});
angle = 2 * pi * offsetSamples / samplesPerPeriod;
sineVal = amp * sin(angle) + bias;
defaultVal = num2str(sineVal);
end
elseif strcmp(sineType, 'Time based')
amp = str2double(get_param(srcBlock, 'Amplitude'));
bias = str2double(get_param(srcBlock, 'Bias'));
freq = str2double(get_param(srcBlock, 'Frequency'));
phase = str2double(get_param(srcBlock, 'Phase'));
t = 0; % Evaluate at t = 0 as we need the default value
sineVal = amp * sin(freq * t + phase) + bias;
defaultVal = num2str(sineVal);
end
catch
% Fallback: not a standard sine block
defaultVal = 'Error';
end
end
fprintf(fileID, '%s\t%s\n', srcBlockName, defaultVal);
end
end
end
fclose(fileID);
disp('Signal names and default values saved to signal_defaults.txt');
close_system(model, 0);
You can customize the above code according to the blocks that you are using.
I have created the below model:
The script’s output for the above model:
Approach 2
Another way of achieving this by logging the signals and utilising “logsout” property of the model in the MATLAB script.
I am attaching a sample code demonstrating the same below:
% Define the model name
model = 'BusModel'; % Replace with your model name
load_system(model);
% Open file to write output
fileID = fopen('signal_defaults.txt', 'w');
fprintf(fileID, 'Signal Name\tDefault Value\n');
out = sim(model); % simulate the model
logsout = out.logsout;
numSignals = logsout.numElements
for i=1:numSignals
srcSignalName = logsout{i}.Name; %this extracts the signal name, not the block name
defaultVal = logsout{i}.Values.Data(1); %here idx 1 is extracted as we want the default value
fprintf(fileID, '%s\t%s\n', srcSignalName, num2str(defaultVal));
end
fclose(fileID);
disp('Signal names and default values saved to signal_defaults.txt');
close_system(model, 0);
The below model is used for this approach :
Please note that the signals are logged here.
The output of this approach:
One advantage of approach 2 over the approach 1 is that there is no prior need of knowing the type of signal block used.
For more details you can refer to the below MATLAB documentations:
I hope this resolves your query.

类别

Help CenterFile Exchange 中查找有关 Prepare Model Inputs and Outputs 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by