- Before trying to fix the problem see: Debug a MATLAB Program, then
- add a breakpoint to line 34, "bindata = data( ..."
- run the script, execution halts at line 34
- inspect the values of variables on line 34, data turns out to be empty, why is data empty?
- look for data in preceeding lines, data is assigned a value at line 24
- inspect (use the tooltip, hover over variables) the values of the variables on RHS in line 34 (shall be 24), ind2 is empty
- look for ind2 in preceeding lines, it's assigned in line 9
- in the command window run
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.
0 个评论
采纳的回答
per isakson
2020-8-1
编辑:per isakson
2021-5-20
How do I debug my matlab code?
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 Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!