Effacer les filtres
Effacer les filtres

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

37 vues (au cours des 30 derniers jours)
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 commentaires
Daniel
Daniel le 10 Avr 2024
Could you provide the full error, please?
vignesh sainath
vignesh sainath le 12 Avr 2024
Hi Daniel
Please find the error below
>> LDF_Script
Error using LDF_Script
Invalid Simulink object name: 'CrashInput'.

Connectez-vous pour commenter.

Réponses (1)

Ramtej
Ramtej le 10 Avr 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 commentaire
vignesh sainath
vignesh sainath le 12 Avr 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

Connectez-vous pour commenter.

Produits


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by