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
2 个评论
回答(1 个)
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:
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Programmatic Model Editing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!