In my code error is Index Exceeds Matrix Dimension
<p>my code gives an error and that is Index exceeds matrix dimensions.It runs perfectly for nsample=10 but when I use nsamples=other value it gives error.</p>

<blockquote>
  <p>The error starts from line 108 that means when dataright(1:20) =
  output(1:20);but it runs when I used dataright(1:10) = output(1:10).</p>
</blockquote>

<pre><code>function icall(block)
setup(block);
function setup(block)
block.NumInputPorts = 0;
block.NumOutputPorts = 4;
block.OutputPort(1).SamplingMode = 'sample';
%% Setup functional port to default
block.SetPreCompPortInfoToDefaults;
%% Setup output port

block.SampleTimes = [0 1];

block.SimStateCompliance = 'DefaultSimState';

block.RegBlockMethod('Start', @Start);
block.RegBlockMethod('Outputs', @Outputs); % Required
block.RegBlockMethod('Update', @Update);
block.RegBlockMethod('Terminate', @Terminate); % Required
block.RegBlockMethod('SetInputPortSamplingMode', @SetInpPortFrameData);
block.RegBlockMethod('PostPropagationSetup', @DoPostPropSetup);
function DoPostPropSetup(block)
% Setup Dwork
block.NumDworks = 4;
block.Dwork(1).Name = 'Nothing';
block.Dwork(1).Dimensions = 1;
block.Dwork(1).DatatypeID = 0;
block.Dwork(1).Complexity = 'Real';
block.Dwork(1).UsedAsDiscState = false;
block.Dwork(2).Name = 'Nothing1';
block.Dwork(2).Dimensions = 1;
block.Dwork(2).DatatypeID = 0;
block.Dwork(2).Complexity = 'Real';
block.Dwork(2).UsedAsDiscState = false;
block.Dwork(3).Name = 'Nothing2';
block.Dwork(3).Dimensions = 1;
block.Dwork(3).DatatypeID = 0;
block.Dwork(3).Complexity = 'Real';
block.Dwork(3).UsedAsDiscState = false;
block.Dwork(4).Name = 'Nothing3';
block.Dwork(4).Dimensions = 1;
block.Dwork(4).DatatypeID = 0;
block.Dwork(4).Complexity = 'Real';
block.Dwork(4).UsedAsDiscState = false;

%endfunction

%endfunction

function Start(block)

fullpathToDll = 'Z:\Farha\2015_02_26_photometer_Datenlogger\PC-Software\CGMultChan.dll';
fullpathToHeader = 'Z:\Farha\2015_02_26_photometer_Datenlogger\PC-Software\CGMultChan.h';
fullpathToHeader;
fullpathToDll;
loadlibrary(fullpathToDll, fullpathToHeader);
libfunctions ('CGMultChan');
delete(instrfindall);
serialPort = 'COM1';
IP = calllib('CGMultChan', 'CGMultChan_Connect', '192.168.100.158'); %Connect to Data Loger
if (IP == 0)
end
out = instrfind('Port', 'COM1');
%Open Serial COM Port
s = serial(serialPort);
set(s, 'BaudRate', 57600, 'DataBits', 8, 'Parity', 'none', 'StopBits', 1, 'FlowControl', 'none', 'Terminator', 'CR');
set(s, 'Timeout', 20);
fopen(s);
TI_ms = calllib('CGMultChan', 'CGMultChan_SetIntTime', .02 * 100);
tic

block.Dwork(1).Data = 1;
block.Dwork(2).Data = 2;
block.Dwork(3).Data = 3;
block.Dwork(4).Data = 5;

function Outputs(block)

block.OutputPort(1).Data = block.Dwork(1).Data; %+ %block.InputPort(1).Data;
block.OutputPort(2).Data = block.Dwork(2).Data;
block.OutputPort(3).Data = block.Dwork(3).Data;
block.OutputPort(4).Data = block.Dwork(4).Data;

count = 0;
function Update(block)
nsamples = 20
count = 0
while (count &lt; nsamples)

    BufferSize = 16;
    pBuffer = libpointer('singlePtr', zeros(BufferSize, 1));
    data2 = calllib('CGMultChan', 'CGMultChan_MeasureAll', pBuffer); %Measurement
    output = pBuffer.Value

    count = count + 1;
    time(count) = toc; %Extract Elapsed Time
    dataright(count) = output(1)
    dataup(count) = output(2)
    databack(count) = output(3)
    datafront(count) = output(5)
end
dataright(1:20) = output(1:20);
dataup(1:20) = output(1:20);
databack(1:20) = output(1:20);
datafront(1:20) = output(1:20);
block.Dwork(1).Data = double(dataright(1));
block.Dwork(2).Data = double(dataup(2));
block.Dwork(3).Data = double(databack(3));
block.Dwork(4).Data = double(datafront(5));

%end Update
function SetInpPortFrameData(block, idx, fd)
block.InputPort(idx).SamplingMode = fd;
for i = 1:block.NumOutputPorts
    block.OutputPort(i).SamplingMode = fd;
end
%%
function Terminate(block)
serialPort = 'COM1';
s = serial(serialPort);

fclose(s);
clear pBuffer;
calllib('CGMultChan', 'CGMultChan_Disconnect'); %Disconnect from Data Logger
</code></pre>