- What's the "model"?
- What's the "inports_cell"?
- etc
I want connect line from in1 to terminator
    4 views (last 30 days)
  
       Show older comments
    
- I disconnect the existing line.
- Create Terminator
- Connect from input to terminator
But, Occur error add_line (Error: Invalid simulink Object name).
How to solve this Error.
mdl = "test_model";
% system load
load_system(mdl);
% IO list loads
inports = find_system(mdl, 'SearchDepth', 1, 'BlockType', 'Inport');
outports = find_system(mdl, 'SearchDepth', 1, 'BlockType', 'Outport');
%   find_system Prop (model name, find type, find type select, value, value type)
inports_suffix = inports_cell(mdl, inports);
outports_suffix = inports_cell(mdl, outports);
[inport_num,] = show_prompt(inports_suffix);
terminate_inport(mdl, inport_num, inports_suffix, inports);
%   IO ports name subsystem/portsname -> portsname change func.
function inports_name_cell = inports_cell(mdl, inports)
    inports_name_cell = cell(1, numel(inports));
    for i = 1:length(inports)
        inports_name_cell{i} = erase(inports{i}, [mdl, '/']);
    end
end
function [show_prompt_idx,show_prompt_tf] = show_prompt(inports_suffix) % inport block name prompt
    inports_name = strrep(inports_suffix,'/1','');
    [show_prompt_idx,show_prompt_tf] = listdlg("SelectionMode", "single", "ListString", inports_name); % 입력값을 텍스트로 반환
end
function terminate_inport(mdl, inport_num, inports_suffix, inports)
    term_block_name = ['Terminator_', num2str(inport_num)]; % Terminator 넘버링
    term_block_path = mdl + '/' + term_block_name; % Terminator 경로와 이름 합치기
    if(getSimulinkBlockHandle(term_block_path) == -1) % Terminator 가 없다면
        position = get_param([inports{inport_num}], 'Position'); % inport블록의 위치 반환
        left = position(1); % 1은 x좌표 
        top = position(2); % 2는 y좌표
        right = position(3);
        bottom = position(4);
        add_block('simulink/Sinks/Terminator', term_block_path, 'Position', [left-80, top,left-70, top+14]); % terminator블록 생성
        lines = get_param(inports{inport_num}, 'LineHandles'); % inport에 연결된 line정보 반환
        terminators_suffix = [strrep(term_block_path,[mdl,'/'],''),'/1']; % Terminator의 경로를 제거하고 이름만 남긴 뒤, 포트 번호 입력
        delete_line(lines.Outport);
        add_line(mdl, inports_suffix{inport_num}, terminators_suffix); % inport 와 Terminator 연결
    else % Terminator 가 있다면
        disp("Terminator already exist.");
    end
end
2 Comments
  Angelo Yeo
    
 on 8 Jan 2024
				In order to find the precise cause and solution for the issue you are currently experiencing, I believe that the following additional information would be necessary. If you could provide the following information, it would enable contributors, including myself, to assist you more effectively:
1) Detailed description of the issue
2) Example files and data files that can reproduce the issue
3) Exact steps to reproduce the same problem using the files you provided
4) All error messages that appear when the issue occurs
Accepted Answer
  Angelo Yeo
    
 on 8 Jan 2024
        
      Edited: Angelo Yeo
    
 on 8 Jan 2024
  
      I have temporarily fixed your script. You can use outport and inport handles as mentioned in add_line. Please fix the script as you need.
mdl = "test_model";
% system load
load_system(mdl);
% IO list loads
inports = find_system(mdl, 'SearchDepth', 1, 'BlockType', 'Inport');
outports = find_system(mdl, 'SearchDepth', 1, 'BlockType', 'Outport');
%   find_system Prop (model name, find type, find type select, value, value type)
inports_suffix = inports_cell(mdl, inports);
outports_suffix = inports_cell(mdl, outports);
[inport_num,] = show_prompt(inports_suffix);
terminate_inport(mdl, inport_num, inports);
%   IO ports name subsystem/portsname -> portsname change func.
function inports_name_cell = inports_cell(mdl, inports)
    inports_name_cell = cell(1, numel(inports));
    for i = 1:length(inports)
        inports_name_cell{i} = erase(inports{i}, [mdl, '/']);
    end
end
function [show_prompt_idx,show_prompt_tf] = show_prompt(inports_suffix) % inport block name prompt
    inports_name = strrep(inports_suffix,'/1','');
    [show_prompt_idx,show_prompt_tf] = listdlg("SelectionMode", "single", "ListString", inports_name); % 입력값을 텍스트로 반환
end
function terminate_inport(mdl, inport_num, inports)
    term_block_name = ['Terminator_', num2str(inport_num)]; % Terminator 넘버링
    term_block_path = mdl + '/' + term_block_name; % Terminator 경로와 이름 합치기
    if(getSimulinkBlockHandle(term_block_path) == -1) % Terminator 가 없다면
        position = get_param([inports{inport_num}], 'Position'); % inport블록의 위치 반환
        left = position(1); % 1은 x좌표    
        top = position(2); % 2는 y좌표
        right = position(3);
        bottom = position(4);
        add_block('simulink/Sinks/Terminator', term_block_path, 'Position', [left-80, top,left-70, top+14]); % terminator블록 생성
        lines = get_param(inports{inport_num}, 'LineHandles'); % inport에 연결된 line정보 반환
        delete_line(lines.Outport);
        %%%%%%%%%%%%%%%%%%%%%%%%%%%% TEMPORARILY FIXED %%%%%%%%%%%%%%%%%%%%%%%%%%%%
        paramOut = get_param([inports{inport_num}], 'PortHandles');
        paramIn = get_param(term_block_path, 'PortHandles');
        add_line(mdl, paramOut.Outport, paramIn.Inport); % inport 와 Terminator 연결
        %%%%%%%%%%%%%%%%%%%%%%%% TEMPORARILY FIXED DONE %%%%%%%%%%%%%%%%%%%%%%%%%%%
    else % Terminator 가 있다면
        disp("Terminator already exist.");
    end
end
More Answers (0)
See Also
Categories
				Find more on Programmatic Model Editing in Help Center and File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
