0

I'm working with the Matlab's level 2 Functions within a Simulink block diagram to send serial data to an Arduino Board, which has to move 11 servos of a robot. I'm trying to improve the speed of the Simulation by vectorizing the assignment of the input and output ports to the corresponding data but I haven't have success. Here is the code that I'd like to vectorize:

function Outputs(block)
global S
global last_sent;
servo=cell(1,11);
str='Mover(';
for i=1:11 %Pretending to replace it by vector operation
    servo{i}=num2str(fix(block.InputPort(i).Data));
    str=[str servo{i} ','];
end
str=[str(1:end-1) ')'];


fprintf(S,'%s\n',str);
flushoutput(S);

pause(10e-3)
fprintf(S,'%s\n','Realim()');

servo_pos=eval(fgetl(S));
for i=1:11 %Pretending to replace it by vector operation

    block.OutputPort(i).Data=servo_pos(i);
end


flushinput(S)
last_sent=[ '[' str(7:end-1) ']'];

So, basically, I want to eliminate those for loops and replace them by vectorized operations. I've tried to use the : operator to get all the the input/output ports but I always get this error:

K>> block.InputPort(:)
No method 'InputPort' with matching signature found for class
'Simulink.MSFcnRunTimeBlock'.

In short, is there a way to vectorize the for loops?

Thanks in advance! Charlie

Charlie
  • 252
  • 5
  • 16
  • Vectorisation is not necesarily faster than `for` loops in Matlab these days. See this answer for example: http://stackoverflow.com/a/23880086/2917957. Something that is probably having a significant impact on performance are the string operations. Use the profiler to determine which lines of code are the bottlenecks, before optimising! – David May 27 '14 at 22:13
  • How can I use the profiler for this type of S-Function? – Charlie May 28 '14 at 03:29

0 Answers0