I am facing invalid simulink object name error while executing text to char conversion

39 次查看(过去 30 天)
I want to perform a read operation from a text file line by line and create a constant block based out of each line name.
The created constant block to be connected to a already created display block in my simulink model, Below is the code for the same, But I am facing an error called as "invalid simulink object name"
% Open the text file
fileID = fopen('Test.txt', 'r');
model_name = 'Script_Test';
bl = find_system('Script_Test', 'Type', 'block');
position = [100 100 200 200];
% Initialize an empty cell array to store the lines
lines = {};
% Read the file line by line
while ~feof(fileID)
line = fgets(fileID); % Read one line
if ischar(line) % Check if line is a character array
lines = convertStringsToChars(line);
m = extractBefore(lines, '_');
constantBlockName = append(m);
constantBlockPosition = position;
% add_block('simulink/Sources/Constant', ['Script_Test/', constantBlockName],'position', constantBlockPosition, 'Value', num2str(0));
add_block('simulink/Sources/Constant',['Script_Test/', constantBlockName], 'position', constantBlockPosition, 'Value', num2str(0));
end
for d = 1:length(bl)
blockname = extractAfter(char(bl(d)),'Script_Test/Display_');
Constantname = extractAfter(constantBlockName,'Constant_');
if contains (blockname, Constantname) == 1
add_line('Script_Test', constantBlockName, char(bl(d)));
end
end
% Read the next line
line = fgets(fileID);
end

回答(1 个)

Ramtej
Ramtej 2024-4-10
Hi,
The error you are encountering is due to the invalid outport and inport in the function "add_line".
Replace the code inside your if statement with the code below:
% Here blockNames are the exact names that you see below your blocks in
% Simulink model
% Your outport and inport should be in the format: The block name, a slash, and the port number
outport=constantBlockName + "/1"; % Since the Constant block only has one output port, the only port number you specify is 1.
inport=displayBlockName + "/1"; % Since the Display block only has one input port, the only port number you specify is 1.
add_line('Script_Test', outport, inport);
Refer to the below documention on the detailed instructions on how to use "add_line" function:
  1 个评论
vignesh sainath
vignesh sainath 2024-4-12
Hi Ramtej
I have updated my script with your updated code snippet, But I am unable to see the error resolving can you check please.
fileID = fopen('TXT_Test.txt', 'r');
model_name = 'Test_SLX';
bl = find_system('Test_SLX', 'Type', 'block');
position = [100 100 200 200];
% Initialize an empty cell array to store the lines
lines = {};
% Read the file line by line
while ~feof(fileID)
line = fgets(fileID); % Read one line
if ischar(line) % Check if line is a character array
lines = convertStringsToChars(line);
m = extractBefore(lines, '_');
constantBlockName = append(m);
constantBlockPosition = position;
% add_block('simulink/Sources/Constant', ['Test_SLX/', constantBlockName],'position', constantBlockPosition, 'Value', num2str(0));
add_block('simulink/Sources/Constant',['Test_SLX/', constantBlockName], 'position', constantBlockPosition, 'Value', num2str(0));
end
for d = 1:length(bl)
blockname = extractAfter(char(bl(d)),'Test_SLX/Display_');
Constantname = append(constantBlockName);
if contains (blockname, Constantname) == 1
outport=constantBlockName + "/1"; % Since the Constant block only has one output port, the only port number you specify is 1.
inport= blockname + "/1"; % Since the Display block only has one input port, the only port number you specify is 1.
add_line('Test_SLX', constantBlockName, blockname);
end
end
% Read the next line
line = fgets(fileID);
end

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Programmatic Model Editing 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by