首页 文章

在simulink中使用matlab功能块创建的信号

提问于
浏览
0

我想在simulink中从matlab函数块生成任意线性信号 . 我必须使用这个块因为那时,我想控制何时通过Stateflow中的序列生成信号 . 我尝试将函数作为具有值字段的结构和另一个时间作为以下代码:

`function y = sig (u) 

if u == 1 
t = ([0 10 20 30 40 50 60]); 
T = [(20 20 40 40 60 60 0]); 


S.time = [t ']; 
 S.signals (1) values ​​= [T '].; 
S.signals (1) = 1 dimensions.; 

else 
N = ([0 0 0 0 0 0 0]); 
S.signals (1) values ​​= [N '].; 
end 
y = S.signals (1). values 
end `

想法是u == 1生成信号,u == 0生成零输出 .

我还尝试使用以下代码将输出作为两列(一次和另一个函数值)的数组:

function y = sig (u) 

if u == 1 
S = ([0, 0]); 
cant = input ('Number of points'); 
for n = Drange (1: cant) 
S (n, 1) = input ('time'); 
S (n, 2) = input ('temperature');  
end 
y = [S] 
else 
y = [0] 
end 
end

在这两种情况下,我都无法生成信号 .

在第一种情况下,我得到的错误如下:

这种结构没有字段'信号';当读取或使用结构时,无法添加新字段

要么

端口宽度或尺寸出错 . 'tempstrcutsf2 / MATLAB函数/ u'的输出端口1是具有1个元素的一维向量 .

要么

未定义的函数或变量'y' . 对局部变量的第一次赋值确定其类 .

在第二种情况下:

代码生成不支持Try和catch,

解析MATLAB函数'MATLAB函数'(#23)时发生错误

端口宽度或尺寸出错 . 'tempstrcutsf2 / MATLAB函数/ u'的输出端口1是具有1个元素的一维向量 .

我将非常感谢任何贡献 .

PD:对不起我的英文xD

1 回答

  • 0

    你的代码中有很多错误,你必须在matlab中阅读更多关于arraysstructs的内容

    • 这里 S = ([0, 0]); 你声明一个只有两个元素的数组,所以大小是静态的

    • mtlab中没有名为 Drange 的函数,除非它是你的

    请参阅此示例,其中包含strcuts以及如何为您的函数创建它们

    function y = sig(u)
        if u == 1 
            %%getting fields size
            cant = input ('Number of points\r\n'); 
    
            %%create a structure of two fields 
            S = struct('time',zeros(1,cant),'temperature',zeros(1,cant));
    
            for n =1:cant 
               S.time(n) = input (strcat([strcat(['time' num2str(n)]) '= '])); 
                S.temperature(n) = input (strcat([strcat(['temperature'...
                    num2str(n)]) '= ']));  
            end 
                %%assign output as cell of struct 
                y = {S} ;
            else 
                y = 0 ;
        end 
    end
    

    得到结果形式只是使用

    s = y{1};
    s.time;
    s.temperature;
    

    将结果转换为2d数组

    2dArray = [y{1}.time;y{1}.temperature];
    

    使用数组

    function y = sig(u)
        if u == 1 
            cant = input ('Number of points\r\n');
            S = [];
            for n =1:cant
                S(1,n) = input (strcat([strcat(['time' num2str(n)]) '= ']));
                S(2,n) = input (strcat([strcat(['temperature'...
                    num2str(n)]) '= ']));
            end
            y = S;
        else 
             y = 0 ;
        end 
    end
    

相关问题