MATLAB: Relatively simple problem with matrix and loops

for loopMATLABmatrix

i got a problem with a script and is described next:
this is the script that i'm preparing. Is focused to clip a topography in several parts (Topoclip1, Topoclip2… Topoclipi) based on polygons included in a shapefile.
The main problem is the loop, i dont know what's happen when i try to run the scripts the results is (??? Subscripted assignment dimension mismatch.)
%%Inputs
clear
addpath /Users/mac/Documents/MATLAB/intersection/stat1
stationsdir='/Users/mac/Documents/MATLAB/intersection/stat1';
a=load('topo.txt'); %load the raster (previously grided xyz table)
cd(stationsdir)
%cd(station)
station='stat';
file=sprintf('%s_clip.shp',station);
p=shaperead(file);
nim=numel(p);
% Extract rectangular profile corners for each profile
for i=1:nim
data=p(i,1); %for the current extention of data...
x=data.X; %extract x values from shapefile
x=x'; %transpose elements

x(end)=[];
y=data.Y; %extract y values from shapefile
y=y'; %transpose elements
y(end)=[];
xy=[x y]; %conform the resulting matrix
xy(end,:)=[]; %delete the redundating last row and fix a 4 vertices polygon
%to here all works ok... the problems is placed somewhere in the next lines
taga=num2str(i);
v = genvarname(['Prof' taga]);
eval([v '=xy']);
Xp=xy(:,1); %rectangular profile X coordinate
Yp=xy(:,2); %rectangular profile Y coordinate
Xt= a(:,1); %Lon1
Yt= a(:,2); %Lat1
h= a(:,3);
%in = inpolygon(Xt,Yt,Xp,Yp);
%plot(Xp,Yp,x(in),Yt(in),'r+')
[loni,lati] = polybool('intersection',Xt,Yt,Xp,Yp);
[lati loni];
A(:,1)=a(:,1);
A(:,2)=a(:,2);
A(:,3)=a(:,3);
B(:,1)=loni';
B(:,2)=lati';
out=A(ismember(A(:,1:2),B,'rows'),:);
taga=num2str(i);
v = genvarname(['Topoclip' taga]);
eval([v '=out']);
%scatter(Topoclip1(:,1), Topoclip1(:,2),20,Topoclip1(:,3),'filled')
end
the ides is to obtain an i number of Topoclip (ie: Topoclip1, Topoclip2, Topocli3….) eachone formed by the clipped topography.
Please give me a hand…. if you can…
thanx

Best Answer

If you run the following, you have the same problem. The error usually tells you which line of the code did the error happen. So check the assignment inside the loop to see how it happens. You can put a break point in the code and run the code line by line
a=rand(3);
a(:,1)=1:5
Related Question