MATLAB: Add_block command using with different destination

simulink

Hi,
I am trying to add simulink block to my model with add_block command. But I have a lot of subsytems and my destination name always change.
"place" is the string variable which is give destination
'Test/1ms/Diag' or it is change according to subsytem 'Test/1ms/Diag1' ….
Matlab gives error when I use this command
temp=add_block('simulink/Sinks/Scope',place ,'MakeNameUnique','on', 'Signal',get(srchandle,'SignalName'));
Invalid destination block specification
How can I use add_block with different destinations

Best Answer

Hi Hakan,
You should be able to use add_block with different destinations as long as your 'place' variable is:
  • a string
  • a valid model location
  • unique (irrelevant if you set 'MakeNameUnique' to 'on')
You can try setting your 'place' variable as a row vector to increase the flexibility of your program and to make it easier to use changing destinations. See the code below for an example of this.
for a = 1:4
% place variable as a changing row vector

place = [sys '/System1/C' num2str(a)];
pos = [x,y+offset*a,x+w,y+h+offset*a];
add_block('built-in/Constant',place,'position',pos);
end
The For Loop changes the 'place' variable every loop making it easy to use different destinations.
The full code is below if you would like to run it and see the results.
function newModel(sys)
if nargin == 0
sys = 'new_model';
end
open_system(new_system(sys));
x = 30;
y = 30;
w = 30;
h = 30;
offset = 60;
% place variable as a set string
place = 'new_model/System1';
pos = [x,y,x+w,y+h];
add_block('built-in/Subsystem',place,'position',pos);
for a = 1:4
% place variable as a changing row vector
place = [sys '/System1/C' num2str(a)];
pos = [x,y+offset*a,x+w,y+h+offset*a];
add_block('built-in/Constant',place,'position',pos);
end
Nick