MATLAB中load_system用法

目录

语法

说明

示例

将模型加载到内存中


        load_system的功能是将 Simulink 模型加载到内存中。

语法

handle = load_system(sys)

说明

        handle = load_system(sys) 将模型 sys 加载到内存中,而无需在 Simulink® 编辑器中打开模型。将模型加载到内存后,可以使用 Simulink API 命令对其进行处理。使用 save_system 保存对模型的更改。

示例

将模型加载到内存中

        将模型 vdp 加载到内存中并返回模型句柄。

h = load_system("vdp")
h =

  172.0004

输入参数

sys — 要搜索的系统

        要搜索的系统,指定为完整的系统路径名称、系统路径名称元胞数组、句柄或句柄向量。

输出参数

handle — 加载的模型的句柄

        加载的模型的句柄。

classdef Initilization methods(Static) % Following properties of 'maskInitContext' are avalaible to use: % - BlockHandle % - MaskObject % - MaskWorkspace: Use get/set APIs to work with mask workspace. function MaskInitialization(maskInitContext) % 从 Mask 参数获取状态空间矩阵 A = eval(get_param(gcb, 'A_matrix')); B = eval(get_param(gcb, 'B_matrix')); C = eval(get_param(gcb, 'C_matrix')); D = eval(get_param(gcb, 'D_matrix')); % 验证矩阵 if isempty(A) || isempty(B) || isempty(C) || isempty(D) error('状态空间矩阵 A, B, C, D 为空,请检查 InitFcn 是否正确执行'); end % 调试:打印子系统路径 disp(['当前子系统路径: ', gcb]); % 确保模型已加载 model_name = bdroot(gcb); if ~bdIsLoaded(model_name) load_system(model_name); disp(['模型 ', model_name, ' 已加载']); end % 查找子系统内的所有块 block_list = find_system(gcb, 'SearchDepth', 1, 'LookUnderMasks', 'all', 'FollowLinks', 'on'); disp('子系统内的所有块:'); disp(block_list); % 查找 MATLAB Function 块 matlab_function_blocks = find_system(gcb, 'SearchDepth', 1, 'LookUnderMasks', 'all', 'FollowLinks', 'on', 'BlockType', 'MATLABFunction'); if isempty(matlab_function_blocks) % 如果 find_system 失败,尝试直接指定路径 expected_path = [gcb '/Fractional_order_operator']; if bdIsLoaded(model_name) && exist_block(expected_path) if strcmp(get_param(expected_path, 'BlockType'), 'MATLABFunction') matlab_function_blocks = {expected_path}; disp(['通过直接路径找到 MATLAB Function 块: ', expected_path]); else error('预期路径 %s 对应的块不是 MATLAB Function 块,实际类型为 %s', expected_path, get_param(expected_path, 'BlockType')); end else error('在子系统 %s 中未找到 MATLAB Function 块,预期路径 %s 无效,请检查子系统结构', gcb, expected_path); end end % 获取 MATLAB Function 块的路径 matlab_function_block = matlab_function_blocks{1}; disp(['找到 MATLAB Function 块: ', matlab_function_block]); % 验证块类型 if ~strcmp(get_param(matlab_function_block, 'BlockType'), 'MATLABFunction') error('路径 %s 对应的块不是 MATLAB Function 块,实际类型为 %s', matlab_function_block, get_param(matlab_function_block, 'BlockType')); end % 将状态空间矩阵传递给 MATLAB Function 块 set_param(matlab_function_block, 'A', mat2str(A)); set_param(matlab_function_block, 'B', mat2str(B)); set_param(matlab_function_block, 'C', mat2str(C)); set_param(matlab_function_block, 'D', mat2str(D)); % 调试信息 disp('Initialization 完成,状态空间矩阵已传递给 MATLAB Function 块:'); disp('A:'); disp(A); disp('B:'); disp(B); disp('C:'); disp(C); disp('D:'); disp(D); % 辅助函数:检查块是否存在 function exists = exist_block(block_path) try get_param(block_path, 'Name'); exists = true; catch exists = false; end end end % Use the code browser on the left to add the callbacks. end end
最新发布
04-03
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值