📄 sys_set.m
字号:
function sys_set(varargin)%*******************************************************************%* sys_set *%*******************************************************************% Callback function for the SetParam Masked subsystem block% This callback is installed by invoking it with the command% sys_set('init_block')% at the MATLAB prompt, with the appropriate model file open and selected.% There is an additional callback associated with the sys_set figure's% OK button, 'sys_set OK_pressed'. This callback sets the Gain blocks% according to the current values of mass, damping, and spring constant.action = varargin{1} ;switch action, case 'init_block', init_fcn ; % Call init_block, located in this M-file case 'create_fig', if(findobj('UserData',gcb)) % Don't open two for same block else create_fig ; end case 'close_fig', % Close if open when model is closed h_fig = findobj('UserData',gcb) ; if(h_fig) % Is the figure for current block open? close(h_fig) ; % If so, close it. end case 'rename_block', % Change the name in the figure UserData. h_fig = findobj('UserData',gcb) ; if(h_fig) % Is the figure open? set(h_fig,'UserData',gcb) ; % If so, change the name. end case 'OK_pressed', % OK button in sys_set figure ok_pressed ;end%*******************************************************************%* init_fcn *%*******************************************************************function init_fcn()% Configure the subsystem block callbackssys = gcs ;block = [sys,'/SetParams'] ;set_param(block,'OpenFcn', 'sys_set create_fig',... 'ModelCloseFcn','sys_set close_fig', ... 'DeleteFcn', 'sys_set close_fig', ... 'NameChangeFcn','sys_set rename_block') ;set_param(block,'k','1') ; % Load initial values in the dialogset_param(block,'m','1') ; % box fields. set_param(block,'c','1') ;%*******************************************************************%* create_fig *%*******************************************************************function create_fig()% We'll check UserData for the Subsystem block. This UserData% will contain the most recent values of mass, damp, spring.% Since UserData is not saved with the model, this will be% blank initially. We could save the default data in a .mat% file, but we'll just let everything default to 1mds = get_param(gcb,'UserData') ;mass = str2num(get_param(gcb,'m')) ;damp = str2num(get_param(gcb,'c')) ;spring = str2num(get_param(gcb,'k')) ;mass_str = sprintf('%f',mass) ; % Put values in stringsdamp_str = sprintf('%f',damp) ;spring_str = sprintf('%f',spring) ;% Create the user interface figure and ui controlsh_fig = figure('Position',[555 406 250 169],'Tag','sys_set',... 'MenuBar','none','NumberTitle','off', ... 'Resize','off', ... 'Name','Model Parameters') ;h_mass_label = uicontrol('Parent',h_fig, ...'Units','points', ...'Position',[18 99.75 23.25 10], ...'String','Mass', ...'Style','text', ...'Tag','mass_label');h_damp_label = uicontrol('Parent',h_fig, ...'Units','points', ...'Position',[18 76.25 76.5 10], ...'String','Damping coefficient', ...'Style','text', ...'Tag','damp_label');h_spring_label = uicontrol('Parent',h_fig, ...'Units','points', ...'Position',[18 50.25 60 10], ...'String','Spring constant', ...'Style','text', ...'Tag','spring_label');h_mass_text = uicontrol('Parent',h_fig, ...'Units','points', ...'BackgroundColor',[1 1 1], ...'Position',[97.5 99 64.5 14.25], ...'String',mass_str, ...'Style','edit', ...'Tag','mass_string');h_damp_text = uicontrol('Parent',h_fig, ...'Units','points', ...'BackgroundColor',[1 1 1], ...'Position',[98.25 75 64.5 15.75], ...'String',damp_str, ...'Style','edit', ...'Tag','damp_string');h_spring_text = uicontrol('Parent',h_fig, ...'Units','points', ...'BackgroundColor',[1 1 1], ...'Position',[98.25 49.5 65.25 12.75], ...'String',spring_str, ...'Style','edit', ...'Tag','spring_string');h_OK_button = uicontrol('Parent',h_fig, ...'Units','points', ...'Callback','sys_set OK_pressed', ...'Position',[73.5 12.75 50.25 19.5], ...'String','OK', ...'Tag','OK_button', ...'UserData',gcs ); % Save current system in OK buttonset(h_fig,'UserData',gcb) ; % userdata Save name of current block % in the figure's UserData. This is % used to detect that a sys_set fig % is already open for the current block, % so that only one instance of the figure % is open at a time.%*******************************************************************%* ok_pressed *%*******************************************************************function ok_pressed()% When the OK button is pressed get the current parameter values% and update the gain valuesh_fig = gcbf ;h_mass = findobj(h_fig,'Tag','mass_string') ;h_OK_button = findobj(h_fig,'Tag','OK_button') ;mass_string = get(h_mass,'String') ;mass = str2num(mass_string);h_damp = findobj(h_fig,'Tag','damp_string') ;damp_string = get(h_damp,'String') ;damp = str2num(damp_string);h_spring = findobj(h_fig,'Tag','spring_string') ;spring_string = get(h_spring,'String') ;spring = str2num(spring_string);% We could put close(h_fig) here to close the figure when OK pressedcm_str = sprintf('%f',damp/mass) ; % Set the gain blockskm_str = sprintf('%f',spring/mass) ;% Note that the set_param statements that follow are looking for% 2 particular named gain blocks. If the names of those blocks% change, these two set_param statements must also change.cur_sys = get(h_OK_button,'UserData') ;cur_block = get(h_fig,'UserData') ;set_param([get_param(cur_sys,'Name'),'/c//m'],'Gain',cm_str) ;set_param([get_param(cur_sys,'Name'),'/k//m'],'Gain',km_str) ;% Store values for mass, damping, spring in masked block% dialog box fields that are not directly accessible to the userset_param(cur_block,'m',sprintf('%f',mass)) ;set_param(cur_block,'c',sprintf('%f',damp)) ;set_param(cur_block,'k',sprintf('%f',spring)) ;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -