Filter löschen
Filter löschen

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

35 Ansichten (letzte 30 Tage)
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 Kommentare
vignesh sainath
vignesh sainath am 12 Apr. 2024
Hi Daniel
Please find the error below
>> LDF_Script
Error using LDF_Script
Invalid Simulink object name: 'CrashInput'.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Ramtej
Ramtej am 10 Apr. 2024
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 Kommentar
vignesh sainath
vignesh sainath am 12 Apr. 2024
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

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Programmatic Model Editing finden Sie in Help Center und File Exchange

Produkte


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by