Error when using level 2 matlab s-function

I'm using simulink with system generator
I'm trying to write a level 2 s-function having as output 3 matrix of uniform random values (0 or 1)
I tried doing like this
for i=1:50
block.OutputPort(1).Data(i)=randint;
block.OutputPort(2).Data(i)=randint;
block.OutputPort(3).Data(i)=randint;
end
and like this also
for i=1:50
block.OutputPort(1).Data=randint(1,i,[0,1]);
block.OutputPort(2).Data=randint(1,i,[0,1]);
block.OutputPort(3).Data=randint(1,i,[0,1]);
end
and I have the same error
"Invalid assignment in 'PRNG/Level-2 MATLAB S-Function': attempt to assign a vector of width 2 to a vector of width 1"
Can you help me please
Thnks

回答(1 个)

Please show the output for
which -all randint
randint was obsoleted from the Communications Systems Toolbox starting in R2012a, and was removed a few releases later. When called in the syntax you are using it returned a single random value. I suspect that you are getting some third-party randint

3 个评论

C:\Program Files\MATLAB\R2013a\toolbox\comm\commdeprecated\randint.m
when I used randint in a matlab function I got a true result
function qa = QProt(n)
qa = zeros(1,n);
for i=1:n
qa=randint(1,i,[0,1]);
end
end
Try running the loop in reverse, 50 down to 1. Remember that in acceleration mode, the first assignment to a variable determines the data type and size. You did not initialize to a vector of the final length so the first assignment to element 1 would set the size of the variable to be 1.
But my suggestion otherwise would be to use your second version except with just i=50 fixed instead of the for loop that assigns a random vector of length 1, overwrites that with a random vector of length 2, overwrites again with length 3 and so on.
Thanks for your reply
I partially resolved the problem bye using this code
function Outputs(block)
x=block.InputPort(1).Data;
for i=1:x
block.OutputPort(1).Data(i)=randint(1,1,[0,1]);
block.OutputPort(2).Data(i)=randint(1,1,[0,1]);
block.OutputPort(3).Data(i)=randint(1,1,[0,1]);
end
%end Outputs
But when running I see changing values before having a final matrix. it's like if the loop is executed in the display.

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Startup and Shutdown 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by