How do I debug my matlab code?

4 次查看(过去 30 天)
Hi All,
I need some help debugging my code. It's something minor (I hope), but I can't seem to figure it out.
AbData = rand(756909,16); %%dummy data
tim_trl = 0:3.9634883453625200E-05:30-3.9634883453625200E-05; %%dummy time
t1 = 20; %start time, seconds
t2 = 30; %end time, seconds
ind1 = find(tim_trl>=t1, 1);
ind2 = find(tim_trl>=t2, 1); %10s time frame
time1 = tim_trl(ind1:ind2);
sampRate = 24414; %sampling freq (Hz), samples per sec
muaWindow = 0.001; %1ms window
binWidth = round(muaWindow*sampRate); %24 samples per 1ms
threshold = 0.018;
for channel = 1:16
data = AbData(ind1:ind2,channel);
counterMUA = 1; % needed for mua indexing
windowSt = 1; % set start point of window
for bin = 1:10000
%set end point of window
windowEnd = binWidth + windowSt - 1;
% access data in one bin
bindata = data(windowSt:windowEnd, channel); %debug here
%%calculate mua above threshold
mua(counterMUA, channel) = sum(bindata >= threshold);
counterMUA = counterMUA+1;
%slides window
windowSt = windowSt + binWidth;
end
end
This is the line of code that needs debugging:
bindata = data(windowSt:windowEnd, channel); %debug here
I know the index in position 1 exceeds array bounds, but I'm not sure how to overcome it.
Thanks in advance.

采纳的回答

per isakson
per isakson 2020-8-1
编辑:per isakson 2021-5-20
How do I debug my matlab code?
  1. Before trying to fix the problem see: Debug a MATLAB Program, then
  2. add a breakpoint to line 34, "bindata = data( ..."
  3. run the script, execution halts at line 34
  4. inspect the values of variables on line 34, data turns out to be empty, why is data empty?
  5. look for data in preceeding lines, data is assigned a value at line 24
  6. inspect (use the tooltip, hover over variables) the values of the variables on RHS in line 34 (shall be 24), ind2 is empty
  7. look for ind2 in preceeding lines, it's assigned in line 9
  8. in the command window run
K>> any(tim_trl>=t2)
ans =
logical
0
K>>
No value of tim_trl is larger or equal to the value of t2
That's a copy&paste error (I think). The line should read
ind2 = find(tim_trl<=t2, 1); %10s time frame
That didn't solve the problem. No, it should read
ind2 = find(tim_trl<=t2, 1, 'last' ); %10s time frame
And there are more problems.
Index in position 2 exceeds array bounds (must not exceed 1).
Error in cssm (line 34)
bindata = data(windowSt:windowEnd, channel); %debug here
At this point data is a column vector and channel has the value 2. Logical mistake, line 24 creates a column vector, channel is accounted for in line 24. Thus
bindata = data(windowSt:windowEnd); %debug here
Now the script runs without errors. Inspect the result. In the command window run
imagesc(mua)

更多回答(0 个)

类别

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

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by