Guy on Simulink

金宝app基于Simulink和模型的设计

Building Models with MATLAB Code

Occasionally I get questions about how to build, modify, and add blocks, to Simulink models using MATLAB commands. In this post, I will to give a basic overview of the common模型建设命令

Contents

从一个新系统开始

Because you need to refer to the system so often when doing model construction from M-code, I immediately save that off in a variable calledSYS.。这new_systemcommand created the empty model in memory, and you have to callopen_system在屏幕上显示它。

SYS.='testmodel'; new_system(sys)%创建模型open_system(sys)% Open the model

添加块和行

When I add blocks to the canvas, I specify the position to provide proper layout. The position parameter provides the top left (x,y) and lower right (x+w,y+h) corners of the block. The x and y values are relative to the origin (0,0) in the upper left corner of the canvas; x increases to the right, and y increases down. To keep my layout organized, I use a standard blocks size of 30 by 30, and offsets of 60.

x = 30;y = 30;w = 30;H = 30;offset = 60;

我喜欢略有不同比例的港口,因此我将它们定义为其他块的高度。add_blockspecifies the source block and the destination path, which defines the block name. Block names must be unique for a given system soadd_blockprovides aMakeNameUnique选项。(这里没有使用)

pos = [x y + h / 4 x + w y + h * .75];add_block('built-in/Inport',[sys'/ in1'],'位置',pos);

I'll add an integrator block, offset to the right of the inport.

pos = [(x +偏移)y(x +偏移)+ w y + h];add_block('built-in/Integrator',[sys'/Int1'],'位置',pos)

To connect the blocks, call add_line and provide the system name, source port and destination port. The ports are designated by the'blockname / portnum'格式。Default line routing is a direct line connection from the source to destination. I prefer to use theautorouting选项。

add_line(sys,'In1 / 1','Int1/1','autorouting','on')

添加多个块和行时,我将它们分组add_block / add_line.pairs to keep myself organized.

pos = [(x +偏移* 2)y(x +偏移* 2)+ w y + h];add_block('built-in/Integrator',[sys'/Int2'],'位置',pos) add_line(sys,'Int1/1','Int2/1','autorouting','on') pos = [(x+offset*2) y+offset (x+offset*2)+w (y+offset)+h]; add_block('built-in/Scope',[sys'/ scope1'],'位置',pos) add_line(sys,'Int1/1','范围1/1','autorouting','on')

Deleting Blocks and Lines

删除块时,我打电话delete_line.delete_block.。This is the reverse of what I did before. The commands are grouped indelete_line./delete_blockpairs. For this example, I'll delete the integratorINT2.,并添加出口。

delete_line.(sys,'Int1/1','Int2/1') delete_block([sys'/Int2'])POS = [(x +偏移* 2)y + h / 4(x +偏移* 2)+ w y + h * .75];add_block('内置/出口',[sys'/Out1'],'位置',pos) add_line(sys,'Int1/1','out1 / 1')

Replacing Blocks

Sometimes you don't really want to delete a block, you are just going to replace it.replace_block为您提供替换符合特定标准的所有块的功能。我仔细阅读了阅读文档以更好地了解这一功能。

replace_block(sys,'Name','In1','内置/罪','noprompt'); set_param([sys'/ in1'],'位置',[x y x+w y+h],'Name','正弦波');

[t,x,y] = sim(sys);绘图(t,y),ylim([ - 。5 2.5]),网格on

Now it's your turn

你使用模型建设命令吗?为什么余弦不在零下交叉?给一个在这里评论with your thoughts.




Published with MATLAB® 7.9

|

Comments

要发表评论,请点击这里要登录您的MathWorks帐户或创建新的。