2016-06-28 109 views
-1

我的代碼給出了一個錯誤,這是指數超過矩陣dimensions.It完美運行nsample = 10,但是當我使用nsamples =其他值它會給出錯誤。在我的代碼錯誤是索引超出矩陣尺寸

誤差從線108啓動時dataright(1:20)= 輸出(1:20),這意味着,但它在運行時我用dataright(1:10)=輸出(1:10)。

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 < 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 
+0

你的代碼是什麼?你在代碼中調用7 **函數**?你能解釋一下你的這段美麗的代碼嗎? –

+2

你在'dataright(1:20)= output(1:20)'行中硬編碼值'20';''dataright'的大小來自'nsamples'。 – Vahid

+0

這是一個S函數。我想人們會需要看看它是如何在Simulink模型中使用的。在問題中使用正確的標籤也會有所幫助。 – am304

回答

0

這是Level 2 S功能代碼生成Simulink模塊。它將數據記錄器與電腦連接起來。這意味着它是一個面向對象的代碼。我解決了更改緩衝區大小pbuffer = 500的問題。因此數據輸出的矩陣大小增加了。