本系列教程是根據孫忠潇編著的<<Simulink仿真及代碼生成技術入門到精通>>學習筆記。
目錄
Simulink仿真入門到精通(一) Simulink界面介紹
Simulink仿真入門到精通(二) Simulink子產品
Simulink仿真入門到精通(三) Simulink信号
Simulink仿真入門到精通(四) Simulink子系統
Simulink仿真入門到精通(五) Simulink模型的仿真
Simulink仿真入門到精通(六) Simulink模型儲存為圖檔
Simulink仿真入門到精通(七) Simulink的回調函數
Simulink仿真入門到精通(八) M語言對Simulink模型的自動化操作及配置
Simulink仿真入門到精通(九) Simulink的流控制
Simulink仿真入門到精通(十) S函數
Simulink仿真入門到精通(十一) 子產品的封裝
Simulink仿真入門到精通(十二) Publish釋出M檔案
Simulink仿真入門到精通(十三) Simulink建立自定義庫
Simulink仿真入門到精通(十四) Simulink自定義環境
Simulink仿真入門到精通(十五) Simulink在流程工業中的仿真應用
Simulink仿真入門到精通(十六) Simulink基于模型設計的工業應用概述
Simulink仿真入門到精通(十七) Simulink代碼生成技術詳解
Simulink仿真入門到精通(十八) TLC語言
Simulink仿真入門到精通(十九) 總結回顧
當使用者編寫了自定義的S函數或者使用Simulink标準庫中的子產品搭建子系統後,可以通過封裝為其設計顯示外觀,追加參數對話框。
封裝是建構一個以對話框為接口的互動界面的過程,它将複雜的子產品邏輯關系隐藏起來,封裝之後僅提供給使用者GUI界面填寫參數。使用者僅需操作對話框即可實作自定義的功能。
11.1 Mask Editor封裝子產品
Mask Editor封裝的對象有兩種:
- Simulink Library中的子產品構成的子系統,每個參數已經具有變量名和依附的控件,隻需将其連結到新封裝的GUI控件上即可;
- S函數編寫的子產品,需要為每個參數建立變量名和參數控件。
11.1.1 封裝子產品構成的子系統
y=a*x^2+b;
右擊Mask→Create Mask
包括4個頁面:
- Icon&Ports:編輯子系統的子產品外觀,如在子系統圖示中添加線條、文本和圖像等;
- Parameters&Dialog:添加或修改子產品參數,并為其設計控件類型;
- Initialization:編輯子子產品的初始化腳本;
- Documentation:添加子子產品的功能介紹及Help文檔路徑。
Icon&Ports
- disp('y=a*x^2+b');
将字元串顯示在子系統框圖的中央。
- text(x,y,'text'):x、y為坐标對,表示text相對于遠點所顯示的位置;
- text(x,y,'text','horizontalAlignment','halign','verticalAlignment','valign'):設定文本顯示的坐标和方位;
- text(x,y,'text','texmode','on'):開啟tex文本模式。
text(0,0,'y=a*x^2+b','horizontalAlignment','left','verticalAlignment','bottom');
通過Icon Units開啟歸一化,坐标範圍0~1。
在text和disp函數語句之前使用color函數,可以規定文本顯示時所使用的顔色,參數可為blue、green、red、cyan、magenta、yellow、black。
color('magenta');
text(0.3,0.3,'y=a*x^2+b','horizontalAlignment','left','verticalAlignment','bottom');
如果使用多種顔色,可以使用多個color分段調整文字的顔色。
- image:将圖檔展示到子系統或S函數子產品框圖上。
image(imread('Mask_demo01.jpg'));
将圖檔全伸展展示。
image(imread('Mask_demo01.jpg'),'top-left');
部分區域顯示。
在定義完Parameters&Dialog之後,可以通過plot函數繪制圖像到所封裝的方框圖上。
在Icon drawing commands中輸入以下代碼。
a=str2num(get_param(gcbh,'g_a'));
b=str2num(get_param(gcbh,'g_b'));
t=0:0.1:10;
plot(t,a*t.^2+b);
Icon units選擇AutoScale。
封裝子系統的子產品圖示時,可以使用Port_label函數設定輸入輸出端口的顯示名稱
- port_label('port_type',port_number,'label','texmode','on');
port_type是指輸入或輸出端口,用input、output表示,當子系統為使能子系統、觸發子系統或與流控制相連的Action子系統時,也可以指定Enable、trigger或Action端口。
port_number為port_type指定的端口類型存在多個端口時端口的序号,從1開始。
label為在指定端口顯示的文本字樣。
texmode表示tex模式開啟與否。
在Icon drawing commands中輸入以下代碼。
port_label('input',1,'In','texmode','on');
port_label('output',1,'Out','texmode','on');
Parameters&Dialog
- Parameter:包括edit、popup、radiobutton和checkbox等控件,其具有value屬性,使用者可輸入或選擇參數的值,并将該值以變量的形式傳遞到子產品内部使用,使用者可以定義控件參數發生變化時所觸發的回調函數。
- Display:作用是提示使用者資訊,比如文字或圖檔。
- Container:含Panel、tab、group box等,可以進行分組,控件tab和group box内部還可以再使用Mask Controls。其中tab控件與其他控件有所不同,不能直接添加到子產品對象中,需要與稱作tabcontainer的隐藏控件共同使用。這些控件僅僅在視覺上起到簡明、美觀的提示作用,沒有value屬性和回調函數。
- Action:包括超連結和按鈕兩種控件,可通過單擊動作觸發使用者自定義的回調函數,但是控件本身沒有變量名,也不必傳遞到封裝的模型或S函數内部的功能。
Dialog Box是目前對話框已經包含的控件資訊表格,表格預設顯示3個屬性:type、Prompt、Name,分别表示控件的類型、說明标簽和變量名。
屬性清單:
屬性名 | 作用說明 |
Name | 控件變量名 |
Value | 控件變量的值 |
Prompt | 控件作用提示文字 |
Type | 控件類型選擇 |
Evaluate | 控件中輸入的内容作為字元串,進行一次eval(str)操作 |
Tunable | 仿真過程中控件值是否可變 |
Read only | 隻讀,不可編輯 |
Hidden | 隐藏控件 |
Never save | 即使控件改變也不儲存 |
Enable | 僅在Read only時不使能,使用者不能編輯控件,顯示為灰色 |
Visible | 控制控件是否可見,僅當Hidden時使用 |
Callback | 編寫或調用控件的回調函數 |
Item location | New/Current row,設定控件放在目前行或者另起一行 |
Prompt location | Top/Left,設定說明文字在控件的上方或左方 |
輸入非法性檢查(輸入字母則報錯):
function check_num(param)
% This function check if the value of param is a number data type.
val = get_param(gcbh, param);
if isletter(val)
errordlg(['Input value of ', param(end) ,' must be a number data-type!']);
return;
end
Initialization
主要用于定義子產品的初始化指令,已經封裝過的參數名将列在左側。
此頁面可以填入M代碼。
子產品的Initialization commands在以下時刻執行:
- 當Icon draw commands或Initialization commands裡更改封裝參數時;
- 當Icon draw commands有内容時,翻轉或旋轉子產品;
- 輕按兩下打開子產品的參數對話框或單擊參數對話框上的Apply/OK/Cancel按鈕關閉對話框時;
- 當使用set_param函數更改子產品參數的值時,賦相同值也會觸發;
- 拷貝此子產品到同一模型中或其他模型中;
- 模型運作仿真時;
- 模型編譯生成代碼時;
- 模型工具欄中選擇Updata diagram時。
如果需要在上述時刻觸發某些動作,或者重新重新整理子產品的外觀,可以在這裡編寫M腳本或調用M函數。
Documentation
用來補充子產品的說明資訊及Help按鈕的功能。
Type的内容顯示到子產品參數對話框的左上角,Description的内容則緊随其後。
11.1.2 封裝S函數編寫的子產品
直接按11.1.1中方法Mask即可。
11.2 程式設計自動封裝子產品
11.2.1子產品的屬性
通過get方法可以擷取子產品屬性清單,如下所示
get(gcbh)
Path: 'mymodel'
Name: 'y=a*x^2+b'
Tag: ''
Description: ''
Type: 'block'
Parent: 'mymodel'
Handle: 5.0001
HiliteAncestors: 'none'
RequirementInfo: ''
FontName: 'auto'
FontSize: -1
FontWeight: 'auto'
FontAngle: 'auto'
Selected: 'on'
MaskType: 'Custom'
MaskDescription: 'This block supplies a demo of masking.'
MaskHelp: ''
MaskPromptString: 'a|b'
MaskStyleString: 'edit,edit'
MaskVariables: '[email protected];[email protected];'
MaskTunableValueString: 'on,on'
MaskCallbackString: 'check_num('g_a');|check_num('g_b');'
MaskEnableString: 'on,on'
MaskVisibilityString: 'on,on'
MaskToolTipString: 'on,on'
MaskVarAliasString: ''
MaskInitialization: ''
MaskSelfModifiable: 'off'
MaskDisplay: 'a=str2num(get_param(gcbh,'g_a'));↵b=str2num(get_param(gcbh,'g_b'));↵t=0:0.1:10;↵plot(t,a*t.^2+b);'
MaskBlockDVGIcon: ''
MaskIconFrame: 'on'
MaskIconOpaque: 'opaque'
MaskIconRotate: 'none'
MaskPortRotate: 'default'
MaskIconUnits: 'autoscale'
MaskValueString: '1|20'
MaskRunInitForIconRedraw: 'off'
MaskTabNameString: ''
Mask: 'on'
MaskCallbacks: {2×1 cell}
MaskEnables: {2×1 cell}
MaskNames: {2×1 cell}
MaskPropertyNameString: 'g_a|g_b'
MaskPrompts: {2×1 cell}
MaskStyles: {2×1 cell}
MaskTunableValues: {2×1 cell}
MaskValues: {2×1 cell}
MaskToolTipsDisplay: {2×1 cell}
MaskVisibilities: {2×1 cell}
MaskVarAliases: {2×1 cell}
MaskWSVariables: [1×2 struct]
MaskTabNames: {2×1 cell}
MaskObject: []
Ports: [1×9 double]
Position: [1×4 double]
Orientation: 'right'
PortRotationType: 'default'
ForegroundColor: 'black'
BackgroundColor: 'white'
DropShadow: 'off'
IOType: 'none'
NamePlacement: 'normal'
ShowName: 'on'
HideAutomaticName: 'on'
Priority: ''
Commented: 'off'
AttributesFormatString: ''
TargetArchitectureMapping: {}
InstantiateOnLoad: 'off'
PolySpaceEndComment: ''
PolySpaceStartComment: ''
AncestorBlock: ''
ReferenceBlock: ''
SourceLibraryInfo: ''
LibraryVersion: ''
UserDataPersistent: 'off'
UserData: []
CompiledIsActive: 'on'
RTWdata: []
HDLData: []
Diagnostics: ''
DialogParameters: [1×1 struct]
IntrinsicDialogParameters: [1×1 struct]
AlgorithmParameters: [1×1 struct]
SecondaryAlgorithmParameters: [1×1 struct]
CompiledSampleTime: [-1 0]
InputSignalNames: {''}
OutputSignalNames: {''}
ModelParamTableInfo: []
StatePerturbationForJacobian: '1.0e-05'
SCDEnableBlockLinearizationSpecification: 'off'
SCDBlockLinearizationSpecification: []
CopyFcn: ''
DeleteFcn: ''
UndoDeleteFcn: ''
LoadFcn: ''
ModelCloseFcn: ''
PreSaveFcn: ''
PostSaveFcn: ''
InitFcn: ''
StartFcn: ''
PauseFcn: ''
ContinueFcn: ''
StopFcn: ''
NameChangeFcn: ''
ClipboardFcn: ''
DestroyFcn: ''
PreCopyFcn: ''
OpenFcn: ''
CloseFcn: ''
PreDeleteFcn: ''
ParentCloseFcn: ''
MoveFcn: ''
BlockType: 'SubSystem'
BlockDescription: 'Select the settings for the subsystem block. To enable parameters for code generation, select 'Treat as atomic unit'.'
LinkStatus: 'none'
StaticLinkStatus: 'none'
PortConnectivity: [2×1 struct]
PortHandles: [1×1 struct]
LineHandles: [1×1 struct]
CompiledPortWidths: [0×0 struct]
CompiledPortDimensions: [0×0 struct]
CompiledPortDataTypes: [0×0 struct]
CompiledPortUnits: [0×0 struct]
CompiledPortComplexSignals: [0×0 struct]
CompiledPortFrameData: [0×0 struct]
DataTypeOverride_Compiled: 'UseLocalSettings'
MinMaxOverflowLogging_Compiled: 'UseLocalSettings'
Capabilities: [1×1 Capabilities]
IOSignalStrings: []
RuntimeObject: [0×1 double]
ExtModeUploadOption: 'none'
ExtModeLoggingSupported: 'off'
ExtModeLoggingTrig: 'off'
ShowPortLabels: 'FromPortIcon'
BlockChoice: ''
TemplateBlock: ''
MemberBlocks: ''
Permissions: 'ReadWrite'
ErrorFcn: ''
PermitHierarchicalResolution: 'All'
TreatAsAtomicUnit: 'off'
MinAlgLoopOccurrences: 'off'
PropExecContextOutsideSubsystem: 'off'
SystemSampleTime: '-1'
RTWSystemCode: 'Auto'
RTWFcnNameOpts: 'Auto'
RTWFcnName: ''
RTWFileNameOpts: 'Auto'
RTWFileName: ''
FunctionInterfaceSpec: 'void_void'
FunctionWithSeparateData: 'off'
RTWMemSecFuncInitTerm: 'Inherit from model'
RTWMemSecFuncExecute: 'Inherit from model'
RTWMemSecDataConstants: 'Inherit from model'
RTWMemSecDataInternal: 'Inherit from model'
RTWMemSecDataParameters: 'Inherit from model'
IsSubsystemVirtual: 'on'
Variant: 'off'
VariantControl: ''
OverrideUsingVariant: ''
GeneratePreprocessorConditionals: 'off'
AllowZeroVariantControls: 'off'
PropagateVariantConditions: 'off'
ActiveVariant: ''
ActiveVariantBlock: ''
TreatAsGroupedWhenPropagatingVariantConditions: 'on'
Location: [1×4 double]
Open: 'off'
PortBlocksUseCompactNotation: 'off'
ScrollbarOffset: [-106 -133]
SetDomainSpec: 'off'
DomainSpecType: 'Deduce'
CompiledDomainSpecType: 'Unset'
ModelBrowserVisibility: 'off'
ModelBrowserWidth: 200
ScreenColor: 'white'
CurrentBlock: 'Gain'
CurrentOutputPort: []
Blocks: {6×1 cell}
Lines: [5×1 struct]
DeleteChildFcn: ''
PaperOrientation: 'landscape'
PaperPosition: [1×4 double]
PaperPositionMode: 'auto'
PaperType: 'usletter'
PaperSize: [11 8.5000]
PaperUnits: 'inches'
TiledPaperMargins: [1×4 double]
TiledPageScale: 1
ShowPageBoundaries: 'off'
ZoomFactor: '100'
ReportName: ''
g_a: '1'
g_b: '20'
inspect
(gcbh)
與封裝有關的屬性清單:
屬性名 | 屬性的值 | 作用說明 | |
MaskType | 'Custom' | Mask Editor的Documentation頁面的參數 | |
MaskDescription | 'This block supplies a demo of masking.' | Mask Editor的Documentation頁面的參數 | |
MaskHelp | 'helpview('xxx.html');' | Mask Editor的Documentation頁面的參數 | |
MaskPromptString | 'a|b' | 參數的說明字元串,不同參數說明字元之間使用'|'分隔 | |
MaskStyleString | 'edit,edit' | 參數的控件類型字元串,兩兩之間使用','分隔 | |
MaskVariables | '[email protected];[email protected];' | 參數變量名及其編号的字元串,兩兩之間使用';'分隔 | |
MaskTunableValueString | 'on,on' | 表征參數是否在仿真時間内可調,使用','分隔 | |
MaskCallbackString | 'check_num('g_a');|check_num('g_b');' | 表示參數Callback調用的内容字元串,使用'|'分隔 | |
MaskEnableString | 'on,on' | 表示參數是否使能的字元串,使用','分隔 | |
MaskVisibilityString | 'on,on' | 表示參數是否可見的字元串,使用','分隔 | |
MaskToolTipString | 'on.on' | 表示當滑鼠停留在子產品外觀框圖範圍内是是否在提示框顯示參數值 | |
MaskInitialization | '' | Initialization Command的内容字元串 | |
MaskDisplay |
| Icon Draw Commands的内容字元串 | |
MaskValueString | '1|20' | 子產品封裝參數的目前值,用'|'分隔 | |
MaskCallbacks | Cell類型的MaskCallbackString | 可以使用元胞數組的下标進行通路或編輯 | |
MaskEnables | Cell類型的MaskEnableString | 可以使用元胞數組的下标進行通路或編輯 | |
MaskNames | | 可以使用元胞數組的下标進行通路或編輯 | |
MaskPropertyNameString | g_a|g_b | 字元形式,将參數變量名用'|'分隔并連接配接起來 | |
MaskPrompts | Cell類型的MaskPromptsString | 可以使用元胞數組的下标進行通路或編輯 | |
MaskStyles | Cell類型的MaskStylesString | 可以使用元胞數組的下标進行通路或編輯 | |
MaskTunableValues | Cell類型的MaskTunableValuesString | 可以使用元胞數組的下标進行通路或編輯 | |
MaskValues | Cell類型的MaskValuesString | 可以使用元胞數組的下标進行通路或編輯 | |
MaskToolTipsDisplay | Cell類型的MaskToolTipsString | 可以使用元胞數組的下标進行通路或編輯 | |
MaskVisibilities | Cell類型的MaskVisibilitiesString | 可以使用元胞數組的下标進行通路或編輯 | |
MaskTabNames | Cell型數組,每個元素為對應參數所在tab頁面的名字 | 可以使用元胞數組的下标進行通路或編輯 |
幫助文檔:https://www.mathworks.com/help/simulink/slref/mask-parameters.html
11.2.2 使用set_param和get_param封裝子產品
- ParameterValue=get_param(Object,ParameterName)
Object表示子產品或者模型的句柄或模型子產品路徑,ParameterName表示子產品或模型的屬性。
- set_param(Object,ParameterName,value)
parameterName和value可以多組成對出現。
例:
set_param('mymodel/y=a*x^2+b','Maskvisibilities',{'on','off'})
使用Excel編輯封裝:
function masktool(block, excelfile)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This function help to mask parameters into block selected.
% MaskNames should not be the same
% MaskPrompts, MaskNames, MaskStyles, popupcontents, MaskTabs, MaskCallbackStrings
% are written in Excel and this function can automatically transfer them
%
% block - the block need to be masked
% excelfile - parameters saved in the excelfile
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% index definition
promts_index = 1;
names_index = 2;
styles_index = 3;
popups_index = 4;
tabs_index = 5;
callbacks_index = 6;
% get data in excelfile
[num, str] = xlsread(excelfile);
% get mask prompts and saved as cell format
promts = str(2:end,promts_index);
% get mask names and saved as cell format
names = str(2:end,names_index);
% change names to MaskVariables format
len = length(names);
maskvar = '';
for ii = 1 : len
maskvar = [maskvar, names{ii}, '=@', num2str(ii), ';']; %OK
end
% get mask styles and saved as cell format
styles = str(2:end,styles_index);
% get mask tab names
tabname = str(2:end, tabs_index);
% get callbacks strings
callbackstr = str(2:end, callbacks_index);
%get block's handle
blockhandle = get_param(block,'Handle');
% get model properties
prop = get(blockhandle);
% mask promts into block
set_param(blockhandle, 'MaskPrompts', promts);
% mask names into block (MaskNames or MaskPropertyNameString is a readonly property)
%set_param(blockhandle, 'MaskNames', names);
set_param(blockhandle, 'MaskVariables', maskvar);
% mask styles intoblock
set_param(blockhandle, 'MaskStyles', styles);
% check popup to write strings
style = str(2:end, styles_index);
% get popup strings
popups = str(2:end, popups_index);
% popuplen = length(popups);
% for ii = 1 :popuplen
% str2pro = popups{ii};
% str2pro(findstr(popups{ii} ,char(10))) = ''; %char(10) = line change token, delete it
% popups{ii} = str2pro;
% end
% get MaskStyleString
stylestring = get_param(blockhandle, 'MaskStyleString');
% get empty start index from MaskStyleString and its length and its total number
emptystart = regexp(stylestring,'<empty>');
lenempty = length('<empty>');
if ~isempty(emptystart)
numtorep = length(emptystart);
% draw popup content from excelfile
content = cell(numtorep);
count = 1;
num = length(style);
for ii = 1:num
if strcmp(style{ii}, 'popup')||strcmp(style{ii}, 'radiobutton')
content{count} = str(ii + 1, popups_index);
% content keep format as xxx|xxx|xxx
count = count + 1;
end
end
end
% part stylestring to seperate parts
% create cell using () not {}
if ~isempty(emptystart)
strpart = cell(numtorep + 1);
strpart{1} = stylestring(1: emptystart(1) - 1);
for ii = 2 : numtorep
strpart{ii} = stylestring(emptystart(ii-1) + lenempty :emptystart(ii) - 1);
end
strpart{numtorep + 1} = stylestring(emptystart(end) + lenempty : end);
% insert content into strpart
maskstring =strpart{1};
for ii = 1: numtorep
maskstring = strcat(maskstring, content{ii});
maskstring = strcat(maskstring, strpart{ii + 1});
end
% cell changed to string
stylestring = char(maskstring);
% delete char(10) (which is not \n or \r)
stylestring(findstr(stylestring, char(10))) = ''; %delete char(10) change line token
% set MaskStyleString into block
set_param(blockhandle, 'MaskStyleString', stylestring);
end
% set tab names
set_param(blockhandle, 'MaskTabNames', tabname);
% format callback str to xx|xx|xx|xx style but not display in callback dialog
% cbnum = length(callbackstr);
% cbstr = '';
% for ii = 1:cbnum
% cbstr = [cbstr, callbackstr{ii},'|'];
% end
% cbstr
% set MaskCallbacks
set_param(blockhandle, 'MaskCallbacks', callbackstr);
% get model name
modelname = get_param(blockhandle, 'Name');
%modelpath = get_param(blockhandle, 'Path');
%fullname = fullfile(modelpath, modelname);
% write Parameters automatically
paramstr = get_param(blockhandle, 'MaskPropertyNameString');
paramstr(findstr(paramstr,'|')) = ',';
set_param(blockhandle, 'Parameters', paramstr);
% write S-Function name automatically
set_param(blockhandle, 'FunctionName', excelfile);
% print OK info
disp([modelname, ' Mask Over!']);
>> masktool(gcbh,'BlkName.xls');
S-Function Mask Over!
11.2.3 使用Simulink.Mask類封裝子產品
Simulink.Mask類給子產品建立一個封裝對象,再通過對象的子方法來操作其各個屬性。Simulink.Mask類提供的方法包括以下操作:
- 建立、拷貝、删除子產品的封裝;
- 建立、拷貝、删除子產品封裝的參數;
- 決定封裝的對象子產品;
- 擷取封裝時的工作空間變量。
>> maskObj=Simulink.Mask.get(gcbh)
maskObj =
Mask - 屬性:
Type: 'Custom'
Description: 'This block supplies a demo of masking.'
Help: ''
Initialization: ''
SelfModifiable: 'off'
Display: 'a=str2num(get_param(gcbh,'g_a'));↵b=str2num(get_param(gcbh,'g_b'));↵t=0:0.1:10;↵plot(t,a*t.^2+b);'
IconFrame: 'on'
IconOpaque: 'opaque'
RunInitForIconRedraw: 'off'
IconRotate: 'none'
PortRotate: 'default'
IconUnits: 'autoscale'
Parameters: [1×2 Simulink.MaskParameter]
ParameterConstraints: [0×0 Simulink.Mask.Constraints]
CrossParameterConstraints: [0×0 Simulink.Mask.CrossParameterConstraints]
BaseMask: [0×0 Simulink.Mask]
MaskObj成員清單:
屬性成員名 | 說明 |
Type | Mask Type |
Description | Mask Description |
Help | 打開help文檔的代碼 |
Initialization | 初始化指令 |
SelfModifiable | 是否使能子產品修改自身内容,如popup的下拉菜單内容在Callback中被改變 |
Display | 繪制子產品外觀的代碼 |
IconFrame | IconFrame是否可見 |
IconOpaque | 子產品圖示是否透明 |
RunInitForIconRedraw | 在執行子產品外觀繪制前是否必須執行初始化指令 |
IconRotate | 子產品外觀圖示是否跟随子產品一起旋轉 |
IconUnits | 子產品外觀圖示的機關設定,'pixel|autoscale|normalized' |
Parameters | 封裝參數集合,類型為Simulink.MaskParameters |
BaseMask | 未使用屬性 |
Simulink.Mask類子方法清單:
子方法名 | 功能說明 |
addParameter | 向子產品中增加一個參數,參數名與值成對輸入,當所封裝子產品與Simulink标準庫有關聯時,使用此函數會報錯 |
copy | 将一個子產品的封裝拷貝到另外一個子產品 |
create | 為未封裝的子產品建立一個封裝對象 |
delete | 為子產品解封裝并删除封裝對象變量 |
get | 擷取子產品的封裝并存為一個對象變量 |
getDialogControl | 查找封裝對象中的控件,參數為控件的變量名Name,傳回被查找對象變量及其父對象變量 |
isMaskWithDialog | 目前對象是否封裝了對話框 |
getOwner | 擷取目前封裝所依附的子產品 |
getParameter | 擷取某個參數的屬性,輸入參數為這個參數的Name |
getWorkspaceVariables | 擷取目前封裝所使用的所用參數并将參數名與值作為結構體變量傳回 |
numParameters | 傳回封裝中參數的個數 |
removeAllParameters | 删除封裝中所有的參數,一般不建議使用。當删除的Mask的Owner與Simulink标準庫中存在連結關系的話,禁止删除 |
set | 設定封裝某個屬性的值,用法類似set_param,屬性名與值可成對輸入 |
addDialogControl | 為父對象添加控件,父對象可以為子產品的封裝對象,也可為group和tab container等能夠包容子控件的控件對象 |
封裝Simulink子系統
對于未封裝的子系統:
maskObj=Simulink.Mask.create(gcbh);
maskObj.Type='Custom';
maskObj.Description='This demo displays how to mask a model.';
maskObj.Display='t=0:0.1:10;y=t.^2+1;plot(t,y)';
g_a=maskObj.addParameter('Type','edit','Name','g_a','Prompt','a','Value','0');
g_b=maskObj.addParameter('Type','edit','Name','g_b','Prompt','b','Value','0');
g_a.set('Value','1');
g_b.Value='20';
封裝S函數子產品
function masktool_neo(block, excelfile)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This function help to mask parameters and displays into block selected.
% MaskNames should not be the same
% MaskPrompts, MaskNames, MaskStyles, popupcontents, MaskTabs, MaskCallbackStrings
% are written in Excel and this function can automatically transfer them to
% the S-function block.Block masked by this tool can not be edited in Mask
% Editor.
%
% block - the block need to be masked
% excelfile - parameters saved in the excelfile
%
% button, image, text, hyperlink, group supported
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% index definition
promts_index = 1;
names_index = 2;
styles_index = 3;
popups_index = 4;
% tabs_index = 5;
callbacks_index = 6;
% get data in excelfile
[num, str] = xlsread(excelfile);
% get mask prompts and saved as cell format
promts = str(2:end,promts_index);
% get mask names and saved as cell format
names = str(2:end,names_index);
% change names to MaskVariables format
% get mask styles and saved as cell format
styles = str(2:end,styles_index);
% get mask tab names
% tabname = str(2:end, tabs_index);
% get TypeOptions
typeopts = str(2:end, popups_index);
% get callbacks strings
callbackstr = str(2:end, callbacks_index);
% test if gcbh is masked. If masked , delete the old mask and create a
% blank one.
maskobj = Simulink.Mask.get(block);
if ~isempty(maskobj)
maskobj.delete;
end
maskobj = Simulink.Mask.create(block);
% parameter list value
p_str = [];
% get the object of groupbox "Parameter"
% par_obj = maskobj.getDialogControl('ParameterGroupVar');
% add controls according to the styles.
len = length(names);
for ii = 1:len
if ismember(styles{ii}, {'text','group','tab'})
% addDialogControl without callback
prop = maskobj.addDialogControl(styles{ii},names{ii});
prop.Prompt = promts{ii};
elseif ismember(styles{ii}, {'pushbutton','hyperlink'})
% addDialogControl for Action controls, they have Callback
prop = maskobj.addDialogControl(styles{ii},names{ii});
prop.Prompt = promts{ii};
prop.Callback = callbackstr{ii};
elseif ismember(styles{ii}, 'image')
% addDialogControl without Prompt
prop = maskobj.addDialogControl(styles{ii},names{ii});
elseif ismember(styles{ii},{'edit','checkbox'}) % 'dial','spinbox','slider' only supported in 2014a and later
% addParameter
p_str = [p_str, names{ii}, ','];
p = maskobj.addParameter('Type', styles{ii}, 'Prompt', promts{ii},'Name', names{ii}, 'Callback', callbackstr{ii});
elseif ismember(styles{ii}, {'popup','radiobutton'})
% addParameter for parameters that have TypeOptions
p_str = [p_str, names{ii}, ','];
expression = '\|';
split_str = regexp(typeopts{ii},expression,'split');
maskobj.addParameter('Type', styles{ii},'TypeOptions', split_str, 'Prompt', promts{ii}, 'Name', names{ii}, 'Callback', callbackstr{ii});
end
end
% write S-Function name and parameter-list automatically
set_param(block, 'FunctionName', excelfile);
set_param(block, 'Parameters', p_str(1:end - 1));
disp('Current block is masked automatically.');
11.3 使用GUIDE封裝子產品
使用S-Function子產品結合Level M S函數檔案在Simulinnk中實作選擇Excel檔案,并将其中資料按照采樣時間順序輸出的功能。
在子產品的對話框中,将資料通過表格展示,并直接将Excel中資料的圖像繪制出來。
function [sys,x0,str,ts,simStateCompliance] = sfun_xlsread(t,x,u,flag,g_file_path)
switch flag,
case 0,
[sys,x0,str,ts,simStateCompliance]=mdlInitializeSizes(g_file_path);
case 1,
sys=mdlDerivatives(t,x,u);
case 2,
sys=mdlUpdate(t,x,u);
case 3,
sys=mdlOutputs(t,x,u);
case 4,
sys=mdlGetTimeOfNextVarHit(t,x,u);
case 9,
sys=mdlTerminate(t,x,u);
otherwise
DAStudio.error('Simulink:blocks:unhandledFlag', num2str(flag));
end
function [sys,x0,str,ts,simStateCompliance]=mdlInitializeSizes(g_file_path)
global data len cnt
[data, str] = xlsread(g_file_path);
len = length(data);
sizes = simsizes;
sizes.NumContStates = 0;
sizes.NumDiscStates = 0;
sizes.NumOutputs = 1;
sizes.NumInputs = 0;
sizes.DirFeedthrough = 0;
sizes.NumSampleTimes = 1;
sys = simsizes(sizes);
x0 = [];
str = [];
ts = [0,0];
cnt = 0;
simStateCompliance = 'UnknownSimState';
function sys=mdlDerivatives(t,x,u)
sys = [];
function sys=mdlUpdate(t,x,u)
sys = [];
function sys=mdlOutputs(t,x,u)
global data len cnt
cnt = cnt + 1;
if cnt <= len
sys = data(cnt);
else
sys = data(end);
end
function sys=mdlGetTimeOfNextVarHit(t,x,u)
sampleTime = 1;
sys = t + sampleTime;
function sys=mdlTerminate(t,x,u)
sys = [];
function varargout = sfun_xlsread_gui(varargin)
% SFUN_XLSREAD_GUI MATLAB code for sfun_xlsread_gui.fig
% SFUN_XLSREAD_GUI, by itself, creates a new SFUN_XLSREAD_GUI or raises the existing
% singleton*.
%
% H = SFUN_XLSREAD_GUI returns the handle to a new SFUN_XLSREAD_GUI or the handle to
% the existing singleton*.
%
% SFUN_XLSREAD_GUI('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in SFUN_XLSREAD_GUI.M with the given input arguments.
%
% SFUN_XLSREAD_GUI('Property','Value',...) creates a new SFUN_XLSREAD_GUI or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before sfun_xlsread_gui_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to sfun_xlsread_gui_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help sfun_xlsread_gui
% Last Modified by GUIDE v2.5 26-Jul-2014 19:44:47
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @sfun_xlsread_gui_OpeningFcn, ...
'gui_OutputFcn', @sfun_xlsread_gui_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before sfun_xlsread_gui is made visible.
function sfun_xlsread_gui_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to sfun_xlsread_gui (see VARARGIN)
% Choose default command line output for sfun_xlsread_gui
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes sfun_xlsread_gui wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = sfun_xlsread_gui_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
close(gcf);
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
close(gcf);
% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
disp('Help button is pressed. User can supply help file themselves.');
% --- Executes on button press in pushbutton4.
function pushbutton4_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
[filename, pathname] = uigetfile({'*.xls','*.xlsx'}, 'Select an Excel file');
if isequal(filename,0)
set_param(gcbh, 'g_file_path', '');
set(handles.edit1, 'string', '');
disp('User selected Cancel');
return;
else
file_path = fullfile(pathname, filename);
set_param(gcbh, 'g_file_path', file_path);
set(handles.edit1, 'string', file_path);
end
[data, str] = xlsread(file_path);
set(handles.uitable1, 'data', data);
axes(handles.axes1);
bar(data, 'g');
function edit1_Callback(hObject, eventdata, handles)
% hObject handle to edit1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit1 as text
% str2double(get(hObject,'String')) returns contents of edit1 as a double
% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end