【问题标题】:Signal created with matlab function block in simulink在 simulink 中使用 matlab 功能块创建的信号
【发布时间】:2014-07-28 02:49:16
【问题描述】:

我想从 simulink 中的 matlab 功能块生成任意线性信号。我必须使用这个块,因为那时我想通过 Stateflow 中的序列来控制何时生成信号。我尝试将 out of function 作为具有 value 字段的结构,另一次作为以下代码:

`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 Function / u”的输出端口 1 是具有 1 个元素的一维向量。

未定义的函数或变量“y”。对局部变量的第一次赋值决定了它的类。

在第二种情况下:

代码生成不支持 Try 和 catch,

在解析 MATLAB 函数 'MATLAB Function' (# 23) 期间发生错误

端口宽度或尺寸错误。 “tempstrcutsf2/MATLAB Function / u”的输出端口 1 是具有 1 个元素的一维向量。

我将非常感谢任何贡献。

PD:对不起我的英语 xD

【问题讨论】:

  • 您的代码看起来格式不正确,至少包含几个语法错误。请检查它。
  • 为什么不在启用的子系统中使用 Signal Builder 模块或 From Workspace 模块?

标签: arrays matlab structure simulink


【解决方案1】:

您的代码中有很多错误 您必须在 matlab 中阅读更多关于 arraysstructs 的信息

  1. 这里S = ([0, 0]); 声明了一个只有两个元素的数组,因此大小将是静态的
  2. 在 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;

将结果转换为二维数组

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

【讨论】:

  • 首先感谢您这么快回答我的问题。
  • 首先感谢您这么快回答我的问题。但我还是不明白一些事情。我的输出是“y”。但是你输入了“s=y{1}”。我的问题是:放置 y={S} 以生成我想要的信号帽还不够吗?你测试代码吗?因为我无法让它工作,我认为我的端口和数据管理器配置有问题。
  • 我真的没有明白你的意思,所以如果你想直接使用数组,请查看修改后的答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多