Change variables in for loop
9 views (last 30 days)
so I want to change variables in a for loop to use them in inpolygon.
I have the variables x_e1, x_e2, x_e3, y_e1, y_e3, y_e3 (and x,y) in my workspace (all of them are k long).
Unfortunately, my code below doesn't work.
Any help or advice is appreciated!
Joseph Cheng on 2 Jun 2021
Edited: Joseph Cheng on 2 Jun 2021
That is because you're evaluating a string in that inpoly and not the variable x_e#... ideally you wouldn't want to do the evaluation with these kind of variables. best practice is not to have these kinds of variables especially if they are all the same length. you could create a single x and y variable for example
x_eNUM = [x_e1;x_e2;x_e3];
y_eNUM = [y_e1;y_e2;y_e3];
inwhich you can then index without having to "change" variable names like:
which then as you can see in the x_eNUM example you evaluate the previously x_eNUM by accessing the first row to represent x_e1. also added in what i think you were going for where you check each k index in the point (x,y). previously above you were checking all of x and y against the x_e# and y_e# for length(x) times over and over.
if you are set on using x_e1,2,3 variables you can look at the funciton eval() which will evaluate a string but take a look at what i think you're attempting with the inpolygon(x(k), and y(k)....) adjustment i made