Inside of a for loop. How to call another script lets say (IK.m) as Ik sub every time the loop goes to a new point?

4 views (last 30 days)
%% Inside a for loop. How can I call another script lets say (IK.m) as Ik sub every time the loop goes to a new point?
% I've already calculated the points of the line and created the IK.m program
%IK.m is attached and the calculation of the liner motion is pasted below.
%% Input Data
% Xstart = input(' Please Enter X start point: ');% input is not necessary, the point can be inserted within the code
% Ystart = input(' Please Enter Y start point: ');
% Zstart = input(' Please Enter Z start point: ');
% Xend = input(' Please Enter X end point: ');
% Yend = input(' Please Enter Y end point: ');
% Zend = input(' Please Enter Z end point: ');
%
% Resolution = input(' Please number of resolution: ');
%% Direct Data
Xstart = 100;
Ystart = 0;
Zstart = 10;
Xend = 200;
Yend = 0;
Zend = 10;
Resolution = 100;
%% Equations
DeltaX = (Xend-Xstart)/Resolution;
DeltaY = (Yend-Ystart)/Resolution;
DeltaZ = (Zend-Zstart)/Resolution;
N = Resolution;
P = 1:N;
X = Xstart + (DeltaX * P);
Y = Ystart + (DeltaY * P);
Z = Zstart + (DeltaZ * P);
%% Display
fprintf('\n Output: DeltaX = %f and DeltaY = %f \n',DeltaX,DeltaY);
Output: DeltaX = 1.000000 and DeltaY = 0.000000
fprintf('\n Position of X & Y: \n \n');
Position of X & Y:
disp('X data:')
X data:
fprintf(' %f \n',X);
101.000000 102.000000 103.000000 104.000000 105.000000 106.000000 107.000000 108.000000 109.000000 110.000000 111.000000 112.000000 113.000000 114.000000 115.000000 116.000000 117.000000 118.000000 119.000000 120.000000 121.000000 122.000000 123.000000 124.000000 125.000000 126.000000 127.000000 128.000000 129.000000 130.000000 131.000000 132.000000 133.000000 134.000000 135.000000 136.000000 137.000000 138.000000 139.000000 140.000000 141.000000 142.000000 143.000000 144.000000 145.000000 146.000000 147.000000 148.000000 149.000000 150.000000 151.000000 152.000000 153.000000 154.000000 155.000000 156.000000 157.000000 158.000000 159.000000 160.000000 161.000000 162.000000 163.000000 164.000000 165.000000 166.000000 167.000000 168.000000 169.000000 170.000000 171.000000 172.000000 173.000000 174.000000 175.000000 176.000000 177.000000 178.000000 179.000000 180.000000 181.000000 182.000000 183.000000 184.000000 185.000000 186.000000 187.000000 188.000000 189.000000 190.000000 191.000000 192.000000 193.000000 194.000000 195.000000 196.000000 197.000000 198.000000 199.000000 200.000000
fprintf('\n');
disp('Y data:')
Y data:
fprintf(' %f \n',Y);
0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
disp('Z data:')
Z data:
fprintf(' %f \n',Z);
10.000000 10.000000 10.000000 10.000000 10.000000 10.000000 10.000000 10.000000 10.000000 10.000000 10.000000 10.000000 10.000000 10.000000 10.000000 10.000000 10.000000 10.000000 10.000000 10.000000 10.000000 10.000000 10.000000 10.000000 10.000000 10.000000 10.000000 10.000000 10.000000 10.000000 10.000000 10.000000 10.000000 10.000000 10.000000 10.000000 10.000000 10.000000 10.000000 10.000000 10.000000 10.000000 10.000000 10.000000 10.000000 10.000000 10.000000 10.000000 10.000000 10.000000 10.000000 10.000000 10.000000 10.000000 10.000000 10.000000 10.000000 10.000000 10.000000 10.000000 10.000000 10.000000 10.000000 10.000000 10.000000 10.000000 10.000000 10.000000 10.000000 10.000000 10.000000 10.000000 10.000000 10.000000 10.000000 10.000000 10.000000 10.000000 10.000000 10.000000 10.000000 10.000000 10.000000 10.000000 10.000000 10.000000 10.000000 10.000000 10.000000 10.000000 10.000000 10.000000 10.000000 10.000000 10.000000 10.000000 10.000000 10.000000 10.000000 10.000000
%% Plot
figure
axis square
plot3(X,Y,Z,'bp-','lineWidth',1,'MarkerSize',3,'MarkerEdge','g','MarkerFace','y')
grid on; hold on;
plot3(Xstart,Ystart,Zstart,'g.', Xend,Yend,Zend ,'r.','MarkerSize',16)
xlabel('X Axis'); ylabel('Y Axis');zlabel('Z Axis');
title('Linear Motion')
  2 Comments
Konard Adams
Konard Adams on 12 Oct 2021
Firstly, thank you for replying.
I will like to use loop but if there is another way, I won't mind trying such.
I meant segment as in the each point of the line , lets say 1:100.

Sign in to comment.

Accepted Answer

Rik
Rik on 13 Oct 2021
I don't see how exactly you would end up with 6 values for each point, but if you convert your script to a function, that will make a implementing a loop trivial.
%my guess for the first line of IK.m:
function Eightsols=IK(epx,epy,epz)
Don't forget to write documentation for this function. It should explain what input it expects, what it does, and what output it will give. Ideally you would also include an example. Make sure you only need to read that whenever you use your function again in three years.

More Answers (0)

Categories

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

Community Treasure Hunt

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

Start Hunting!