Relatively simple problem with matrix and loops
1 view (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
Fangjun Jiang
on 14 Oct 2011
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
4 Comments
Walter Roberson
on 15 Oct 2011
Jules,
After your line
in = inpolygon(Xt,Yt,Xp,Yp);
then the points that are inside the polygon are Xt(in) and Yt(in)
If you need the indices of these for some purpose, then they are
find(in)
More Answers (0)
See Also
Categories
Find more on Vehicle Scenarios in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!