Query regarding reshape and my code

Heya , i'm currently processing datasets that i've been provided. However when i try to run the matlab file , i get this following error.I'm not very familar with matlab if i'm being honest , any help would be appreciated.
Error in bassi_bed2b (line 21)
data2d=reshape(data(1,:,:),200,45);
close all
clear all
%ARRAYs 1-3 concatinated
load m:\125709_ABS_data.mat
fz=18; % fontsize
s1=size(ABS_Data);
r=0.005*(20:220);
nz=40; %number of frames running averaged over about 7s
for jj=1:floor(s1(1)/nz);
data=mean(ABS_Data(1+nz*(jj-1):jj*nz,:,:));
data2d=reshape(data(1,:,:),200,45);
%normalise data by bed echo max
mz=(max(data2d)); oz=ones(200,1); d2z=oz*mz;
data2d=data2d./d2z;
data_diff=diff(data2d);
mz=(max(data_diff)); oz=ones(199,1); d2z=oz*mz;
data_diff=data_diff./d2z;
for kk=1:45, hz=find(data_diff(120:150,kk)>0.1);
bed(jj,kk)=hz(1);
end
figure(1), orient tall
subplot(3,1,1)
pcolor(0.033*(1:45),0.1+(120:160)/200,data2d(120:160,:)), shading interp, hold on
%plot(119+bed(jj,:),'k','linewidth',3)
%title(num2str(jj))
x=1:45; xx=1:.1:45;
yy=interp1(x,bed(jj,:),xx,'cubic');
plot(0.033*xx,0.1+(120+yy)/200,'w','linewidth',2)
ylabel('Range (m)','fontsize',fz), xlabel('Distance (m)','fontsize',fz)
hold off
% remove slope
pp=polyfit(xx,yy,2);
ff=polyval(pp,xx);
yy=yy-ff;
bed2(jj,:)=yy-mean(yy);
riph=sqrt(2)*std(bed2(jj,:));
ripht(jj)=2*0.005*riph; %convert to m
s2=size(bed2);
q=0; cross=0;
for n=1:s2(2)-1
if sign(bed2(jj,n))+sign(bed2(jj,n+1))==0 %Detect for change in sign at zero crossover
q=q+1;
cross(q) = n;
end
end
%filter out very small ripple less thn 5 cm
hz=find(diff(cross)>15);
cross2=cross(hz);
riplen(jj)=2*0.0033*mean(diff(cross2));
pause(0.1)
hold off;
end %jj
axis([0 1.5 0.7 0.9])
text(1.4,0.87,'a','fontsize',fz-2,'color',[1 1 1])
set(gca,'fontsize',fz)
hold on
subplot(3,1,2)
surf(0.0033*[1:s2(2)],40*0.165*[1:s2(1)],bed2/200), hold on
colormap(copper)
shading flat
view(-40,60)
axis([0 1.5 0 1000 -0.15 0.15])
ylabel('Time (s)','fontsize',fz), xlabel('Distance (m)','fontsize',fz), zlabel('Hieght (m)','fontsize',fz)
text(1.7,300,0.145,'b','fontsize',fz-2,'color',[0 0 0])
set(gca,'fontsize',fz)
subplot(3,1,3)
ss=length(ripht)
semilogy(40*0.165*[1:ss],ripht,'xk'), hold on
mripht=mean(ripht);
plot(40*0.165*[1 ss],[mripht mripht],'k','linewidth',2)
plot(40*0.165*[1:ss],riplen,'ok'), hold on
mriplen=mean(riplen);
plot(40*0.165*[1 ss],[mriplen mriplen],'k','linewidth',2)
axis([0 1000 0.01 1])
xlabel('Time (s)','fontsize',fz), ylabel( '\eta (m) and \lambda (m)','fontsize',fz)
text(950,0.7,'c','fontsize',fz-2,'color',[0 0 0])
set(gca,'fontsize',fz)

1 Comment

Jan
Jan on 23 Jan 2018
Edited: Jan on 23 Jan 2018
Post the complete error message. Note that it is inconvenient to search the failing line.
It is recommended to post the relevant code only.

Sign in to comment.

Answers (1)

Although you did not post the part of the error message, which mentions, what the error is, I guess that
data2d = reshape(data(1,:,:), 200, 45)
fails, because the 2nd and 3rd dimension of data do not have 200*45 elements. You can check this with the debugger. Type this in the command window:
dbstop if error
Now run your code again until it stops at the error. Examine the dimensions:
size(data)
What do you see? Where does the difference between the real size and you expectations come from?

6 Comments

Heya, cheers for the quick reply.
K>> size(data)
ans =
1 200 60
end
That's the result i got when i ran the code
Fine. Now you know that data(1, :, :) has the dimensions [200 x 60] and in reshape(..., 200, 45) you try to reshape it to a [200 x 45] matrix. But this cannot work, because the number of elements change.
It is your turn to explain, what should happen instead. Either change it to:
reshape(data(1,:,:), 200, 60)
Or do you want a linear interpolation to get 45 values out of 60? Or maybe you mean:
reshape(data(1,:,1:45), 200, 45)
The readers cannot guess it and the code does not contain useful comments to clarify this.
I've fixed the error by changing the values for line 21 , however i now get another error
Attempted to access hz(1); index out of bounds because numel(hz)=0.
codeError in bassi_bed2b (line 32)
bed(jj,kk)=hz(1);
Jan
Jan on 23 Jan 2018
Edited: Jan on 23 Jan 2018
And again the debugger will help you to solve your problem. "dbstop if error, ..."
I guess this code
for kk=1:45
hz = find(data_diff(120:150,kk)>0.1);
bed(jj, kk) = hz(1);
end
does not find any value for some inputs. Perhaps you want:
for kk=1:45
hz = find(data_diff(120:150,kk)>0.1, 1);
if ~isempty(hz)
bed(jj, kk) = hz;
end
end
Care for a proper pre-allocation of bed before the loop. What should be the default value, if hz is empty?
The code contains a lot of weird parts:
q=0; cross=0;
for n=1:s2(2)-1
if sign(bed2(jj,n)) + sign(bed2(jj,n+1))==0
q=q+1;
cross(q) = n;
end
end
  1. The function "cross" is shadowed by a variable.
  2. corss is not pre-allocated, such that it grows iteratively. You should see a warning in the editor.
  3. The length of the resulting vector cross depends on the data. Better use a pre-allocation, or vectorized code:
Change = (sign(bed2(jj, 1:s2(2)-1)) + sign(bed2(jj, 2:s2(2))) == 0)
Remember, that the sign function replies 0 if the argument is 0, but in [0;0] there is no change of the sign. Maybe this is better:
Change = xor(bed2(jj, 1:s2(2)-1) < 0, bed2(jj, 2:s2(2)) < 0);
Do not use load without catching the outputs, because this pollutes the workspace. During reading the code, you cannot know if a variable has been imported before. Then using clear all to reduce the danger of collisions with formerly existing variables, is even more a waste of time. Remember that it removes all functions from the memory and reloading them from the lame disk needs a lot of time. It is much better insert the code in a function, omit clear all and catch the output by FileData = load(...).
Do you want to use this code for productive work? I guess that it contains more bugs. Finding them is hard due to the lean comments. If the code runs without an error, I still would not trust the results.
This is really a case of:
  • person writes code that does a passable job of solving their problem, using their data, not bothering to document it, comment it, putting any check for invalid inputs, and using hardcoded sizes for everything.
  • other person tries to use the code on their data which is slightly different from the original data (different number of samples, maybe) and then finds the code doesn't work because it's not documented, not commented, and doesn't check that the hardcoded sizes actually match the input size.
If the code you write is going to be reused (as anything worth writing would be) then spend some time commenting it, documenting it and make sure that your code validate whatever assumptions it makes such as size of inputs or make sure that it works with variable size of inputs.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Asked:

on 23 Jan 2018

Commented:

on 23 Jan 2018

Community Treasure Hunt

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

Start Hunting!