Error with index exceeding array bound

5 次查看(过去 30 天)
I am using the Data Acquisition toolbox and have run into this issue.
Warning: Error occurred while executing the listener callback for event ElementsAvailable defined for class
daq.Buffer:
Index in position 1 exceeds array bounds. Index must not exceed 100.
Error in LiveDataAcquisition_3_DEV_NT/scansAvailable_Callback (line 129)
app.fwsdata(app.fws_x,(app.SelectedChannels)) = app.calibratedData(ii,app.SelectedChannels);
The following code is where this error is occuring:
% for data that goes to the workspace and
for ii = 1:(app.RateEdit.Value/10)
% keep count of the sample#/time
app.fws_x = app.fws_x + 1;
% initialize data to be written into the workspace
app.fwsdata(app.fws_x,(app.SelectedChannels)) = app.calibratedData(ii,app.SelectedChannels);
end
Some background the fwsdata will be sent to the workspace at the end of the data collection and calibrated data is "calibrated data" ie has had a slope and intercept applied to it and has 1/10 of the sample rate stored in it at a time hence the for loop using a tenth of the sample rates value. The error that I am having is that if I raise the RateEdit.Value above 1000(1004 exactly) It will give me the error shown above. the app needs to be able to collect data at a sample rate of 2048Hz. I have tried to use a while loop to see if there was a problem with the for loops upper limit but this did not seem to fix the error. I have also tried using the round function becuase the value that I want to put in is 2048 and the value is going into the decimals but this also did not seem to fix the problem either. I am unsure why the index must not exceed 100. Thanks
  2 个评论
Torsten
Torsten 2024-5-1
编辑:Torsten 2024-5-1
Insert the lines
app.RateEdit.Value/10
size(app.calibratedData,1)
before entering the for-loop and tell us the output.
Connor
Connor 2024-5-2
I noticed that it does store in a 100/(number of channels) is there a way to change this?

请先登录,再进行评论。

回答(1 个)

Shubham
Shubham 2024-5-20
Hi Connor,
The error message you're encountering indicates that at some point in your code execution, the index app.fws_x is exceeding the pre-allocated size of the array app.fwsdata. This situation typically arises when the loop iterates more times than the array can accommodate based on its dimensions. Given the details you've provided, let's troubleshoot and propose a solution:
Understanding the Issue
  • Your loop is designed to iterate (app.RateEdit.Value/10) times. When app.RateEdit.Value is set to values higher than 1000 (e.g., 1004), dividing by 10 gives you more than 100 iterations.
  • Each iteration increments app.fws_x by 1 and attempts to store data in app.fwsdata at this index.
  • If app.fwsdata is not initialized or pre-allocated with enough space to accommodate the maximum expected index, you will encounter the "Index exceeds array bounds" error when app.fws_x surpasses the size of app.fwsdata.
Possible Solutions
  1. Ensure app.fwsdata is pre-allocated with enough rows to hold the maximum number of samples you expect. If you're sampling at 2048Hz and collecting data for a certain duration, pre-allocate app.fwsdata accordingly.
% Example of pre-allocation assuming a 1-second duration for simplicity
maxRate = 2048; % Maximum sample rate
durationInSeconds = 1; % Example duration
app.fwsdata = zeros(maxRate * durationInSeconds, length(app.SelectedChannels));
2. If app.fws_x is meant to index into app.fwsdata, ensure that it does not exceed the number of rows app.fwsdata. You might need to adjust how you calculate the number of iterations or how you increment app.fws_x.
3. Since you mentioned that app.fws_x is incremented within the loop and used as an index, ensure that its maximum value does not exceed the pre-allocated size of app.fwsdata. If app.RateEdit.Value can vary, your pre-allocation should consider the maximum possible value.
4. Add a check before the line that causes the error to print out the current value of app.fws_x and the size of app.fwsdata. This can help you identify the exact condition causing the overflow.
if app.fws_x > size(app.fwsdata, 1)
error('app.fws_x exceeds the number of rows in app.fwsdata.');
end
5. If the division of app.RateEdit.Value by 10 leads to a non-integer number of iterations which might not be your intent (since ii as an iterator should be an integer), consider using floor, ceil, or round to adjust the iteration count to an integer value. However, this seems not to be the core issue based on your description.
Note:
Ensure that app.fwsdata is pre-allocated with a size that can accommodate the highest expected index based on your sampling rate and the duration of data collection. Adjust your loop or indexing logic to prevent exceeding the allocated size. If your application requires handling variable lengths of data dynamically, consider checking and resizing the array within the loop, though be mindful that resizing arrays in a loop can significantly impact performance.

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品


版本

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by