Specify Paths That Contain Multiline Names and Special Characters
To take action programmatically in Simulink®, you must specify the target object you want to take the action on. The target object can be a model, a component— such as a subsystem or library— or a model element, such as a block or signal line. One way to specify a target object is using a path.
A path is a string or character array that specifies the location of a target object relative to the top level of the model. This example shows how to specify paths that contain multiline names and special characters. For more information about paths, see Get Handles and Paths.
Specify Path That Contains Multiline Names
This example shows how to specify a path that contains multiline names. In the example, you add this Sine Wave block to a model.
1. Create a model named myModel
.
new_system("myModel");
2. Open the model.
open_system("myModel");
3. Specify the path of the library block you want to add to the model. The Sine Wave block is located in the Simulink library, in the Sources sublibrary.
pathLib = "simulink/Sources/Sine Wave";
4. Specify the path of the new block you want to create in the model. Use the newline character to specify the line break in the block name.
pathNew = "myModel/Input Signal"+newline+"Cycle A";
5. Add the new block to the model.
add_block(pathLib,pathNew);
The add_block
function adds a new block to the model. The name the software gives the block is the name at the end of the path you specify in the second input argument of the function. When the programmatic operation you run affects model element names or labels, such as the block name, use the newline character to specify the line breaks in multiline names.
When the programmatic operation you run does not affect names or labels, and when there are no spaces next to line breaks in the path you want to specify, you can use a blank space to indicate a line break. For example, get the amplitude of the sine wave output by the block you created in the previous step.
1. Specify the block path, using blank spaces to indicate line breaks.
path = "myModel/Input Signal Cycle A";
2. Get the amplitude.
a = get_param(path,"Amplitude")
a = '1'
Specify Path That Contains Special Characters
This example shows how to specify a path that contains special characters.
Suppose you want to specify this path: myModel/Sample at 130 °C/myBlock
.
You can copy and paste the symbol into the command from another source, such as the web.
Alternatively, you can use Unicode® characters to specify the path. MATLAB® stores all characters as Unicode characters using the UTF-16 encoding, where every character is represented by a numeric code value. For more information, see Unicode and ASCII Values. To specify the path using Unicode characters, complete these steps.
1. Find the code values of the special characters in the path. The code value of the degree symbol is 176
.
This table shows the numeric code values of the letters in the Greek alphabet. To see the full table, open the example. Then, open the SpecifyPaths.mlx
file. Run the script. Pause your pointer on the table and scroll.
i = [913:929,931:937,945:969]'; Char = char(i); Value = num2str(i); table(Char,Value)
ans=49×2 table
Char Value
____ _____
Α 913
Β 914
Γ 915
Δ 916
Ε 917
Ζ 918
Η 919
Θ 920
Ι 921
Κ 922
Λ 923
Μ 924
Ν 925
Ξ 926
Ο 927
Π 928
⋮
This table shows the code values of other commonly used special characters.
i = [33:47,60:64,92:96,124,126,162:188]'; Char = char(i); Value = num2str(i); table(Char,Value)
ans=54×2 table
Char Value
____ _____
! 33
" 34
# 35
$ 36
% 37
& 38
' 39
( 40
) 41
* 42
+ 43
, 44
- 45
. 46
/ 47
< 60
⋮
2. Convert the code values to character arrays using the char function.
myChar = char(176);
3. Add the special character to the rest of the path using string concatenation.
path = "myModel/Sample at 130 "+myChar+"C/myBlock"
path = "myModel/Sample at 130 °C/myBlock"