MATLAB脚本读取Excel中的mapping数据自动生成simulink模型

MATLAB脚本读取Excel中的mapping数据自动生成simulink模型


MATLAB脚本读取Excel中的mapping数据自动生成simulink模型

excel文件形如:

在这里插入图片描述

matlab脚本:

function MODEL_update(model_file)

model_file = 'model_001';
excel_file = 'ADAS_MAPPING.xlsx';

disp('--------------------------------------------------------------');
disp('MODEL_update: V1.00 !');
disp('TIPS:      MODEL_update(model_file)');
disp('--------------------------------------------------------------');

if exist('model_file', 'var')
    pause(0);
else  
    disp(['model file name is empty, exit!!!!!!']); 
    clear;
    return;
end

excel_ver = 0;
% 检查字符串长度是否至少为4  
if length(excel_file) >= 4  
    % 获取字符串的后5个字符  
    postfix_Chars = excel_file(end-3:end);  
      
    % 比较这4个字符是否等于'xlsx'  
    if strcmp(postfix_Chars, 'xlsx')  
        excel_ver = 2;
        % disp('新的Excel格式');  
    elseif strcmp(postfix_Chars, '.xls') 
        excel_ver = 1;
        % disp('老的Excel格式');  
    else  
        disp('不是Excel格式'); 
        return; 
    end  
else  
    disp('文件名长度不够');  
end

if excel_ver == 2;
    % M = readmatrix(excel_file); 
    % 此时,MATLAB将自动生成变量名,如Var1, Var2等
    T = readtable(excel_file, 'ReadVariableNames', false); 
elseif excel_ver == 1;
    M = readmatrix(excel_file);
end


new_system(model_file) % 创建模型
open_system(model_file) % 打开模型

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



%return;
%a=get(gcbh)
for i=1 : size(T,1)
    i_str = num2str(i);

    pos_in = [x y+offset*i+h/4 x+w y+offset*i+h*.75];
    in_str = append('/In', i_str);
    in_port_str = append(in_str(2:end), '/1');
    add_block( 'built-in/Inport' ,[model_file in_str], 'Position' ,pos_in);


    % input_all_raw=find_system(model_file,'BlockType','Inport');

    pos_sigcov = [x+offset*1 y+offset*i+h/4 x+offset*1+w y+offset*i+h*.75];
    sigcov_str = append('/SigCov', i_str);
    sigcov_port_str = append(sigcov_str(2:end), '/1');
    sigcov_block_str = append(model_file, sigcov_str);
    add_block( 'simulink/Signal Attributes/Signal Conversion' ,[model_file sigcov_str], 'Position' ,pos_sigcov);
    %set_param(sigcov_block_str,'GotoTag',T{i, 1}{1});

    add_line(model_file, in_port_str, sigcov_port_str, 'autorouting', 'on' )


    pos_goto = [x+offset*3 y+offset*i+h/4 x+offset*3+w*4 y+offset*i+h*.75];
    goto_str = append('/Goto', i_str);
    goto_port_str = append(goto_str(2:end), '/1');
    goto_block_str = append(model_file, goto_str);
    add_block( 'simulink/Signal Routing/Goto' ,[model_file goto_str], 'Position' ,pos_goto);
    set_param(goto_block_str,'GotoTag',T{i, 1}{1});
    
    add_line(model_file, sigcov_port_str, goto_port_str, 'autorouting', 'on' )

    pos_from = [x+offset*10 y+offset*i+h/4 x+offset*10+w*4 y+offset*i+h*.75];
    from_str = append('/From', i_str);
    from_port_str = append(from_str(2:end), '/1');
    from_block_str = append(model_file, from_str);
    add_block( 'simulink/Signal Routing/From' ,[model_file from_str], 'Position' ,pos_from);
    set_param(from_block_str,'GotoTag',T{i, 1}{1});

    pos_out = [x+offset*13 y+offset*i+h/4 x+offset*13+w y+offset*i+h*.75];
    out_str = append('/Out', i_str);
    out_port_str = append(out_str(2:end), '/1');
    add_block( 'built-in/Outport' ,[model_file out_str], 'Position' ,pos_out);
    
    add_line(model_file, from_port_str, out_port_str, 'autorouting', 'on' )
end

clear;
return;

生成的matlab模型:

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值