How to extract data from a function or functions. Also how to export this data to excel.
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
0 votes
I have written two sets of codes. I placed them in two separate folders as program 1 and program 2.
Now with the code calling the inverse and looping. How do I extract the data? There is nothing in workspace.
Program 1 demonstrates the actual movement of the linear motion and program 2 is just the stationary output.
Is there a way to extract these data to excel?
Both programs are attached.
Accepted Answer
Bjorn Gustavsson
on 14 Oct 2021
Your functions have their own name-space where the variables that are defined exists (in addition to the input variables that you feed the functions with). When the functions complete these variables cease to exist, they are no more. To get the results of interest out you have to define (and assign) some output-variables in your function-files. This typically looks something like this:
function [r_out,flag] = position_along_line(l,r0,e_line)
Here a variables r_out and _flag are defined as output from the function and have to be defined in the function. This way your function calculates something and returns the result. You'll have to adjust your "programmes" similarly. One way to get all the results from a programme is to not have it defined as a function but use it as a script - then all of the calculations will be done in the base workspace and exist after the script has finished - the main drawback is that it is easy to develop scripts that rely on other variables to exist without noticing which gives the scrip some hidden dependencies.
Also avoid the "clc, clear all" trope inside functions it is usless, in scripts it is anoying.
HTH
7 Comments
Konard Adams
on 14 Oct 2021
Edited: Konard Adams
on 14 Oct 2021
You are appreciated. Thank you!
I am still trying to figure it out.
Bjorn Gustavsson
on 14 Oct 2021
For you step 1 is to simply write down all parameters in each of your "main programs" you want to save, on "paper". Then you have to make those parameters output-variables of the functions like I illustrated above. Since no one here can know what variables you are interested in saving we cannot give detailed instructions for you to follow, therefore you will have to make do with information about the principle that you then can adapt to your current problem and other problems in the future.
Since you seem rather new to both matlab and programming, I suggest that you browse through the on-ramp information - with particular focus on the programming-parts: on-ramp.
HTH
Konard Adams
on 14 Oct 2021
Edited: Konard Adams
on 15 Oct 2021
Thank you.
Let me try and explain better what I am looking for.
Here is the calculation for the points on the line:
%Input 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);
%% 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')

Now:
I want to sub or call the inverse script to loop through the equation above and gain a 100 *6 matrix.
Therefore after running the Inverse kinematics script, eight solutions are found. I would like to loop one or all of these solutions into the equation above to generate the 100 x 6 matrix which i can then export as an excel file.
%% Inverse Kinematics
% Link lenghts & Offsets in Millimeters(mm)
a2=380; a3=0; d3=0; d4=351; L5=89;
% Input Coordinate points
p1x = 202.90; %End effector points
p1y = 55.85;
p1z = -449.09;
p2x = 232.91;
p2y = 49.08;
p2z = -465.79;
p3x = 199.39;
p3y = 31.37;
p3z = -445.48;
p4x = 196.96;
p4y = 55.17;
p4z = -459.50;
epx = 206.24; % Tool tip points
epy = 39.06;
epz = -472.97;
% M points computation
nx = (p2x-p1x)/sqrt((p2x-p1x)^2+(p2y-p1y)^2+(p2z-p1z)^2);
ny = (p2y-p1y)/sqrt((p2x-p1x)^2+(p2y-p1y)^2+(p2z-p1z)^2);
nz = (p2z-p1z)/sqrt((p2x-p1x)^2+(p2y-p1y)^2+(p2z-p1z)^2);
sx = (p3x-p1x)/sqrt((p3x-p1x)^2+(p3y-p1y)^2+(p3z-p1z)^2);
sy = (p3y-p1y)/sqrt((p3x-p1x)^2+(p3y-p1y)^2+(p3z-p1z)^2);
sz = (p3z-p1z)/sqrt((p3x-p1x)^2+(p3y-p1y)^2+(p3z-p1z)^2);
ax = (p4x-p1x)/sqrt((p4x-p1x)^2+(p4y-p1y)^2+(p4z-p1z)^2);
ay = (p4y-p1y)/sqrt((p4x-p1x)^2+(p4y-p1y)^2+(p4z-p1z)^2);
az = (p4z-p1z)/sqrt((p4x-p1x)^2+(p4y-p1y)^2+(p4z-p1z)^2);
px = epx-ax*L5; % end points calulations
py = epy-ay*L5;
pz = epz-az*L5;
% Showing the values of the computed Goal points.
disp('nx = '),disp(nx); disp('ny = '),disp(ny); disp('nz = '),disp(nz);
nx =
0.8573
ny =
-0.1934
nz =
-0.4771
disp('sx = '),disp(sx); disp('sy = '),disp(sy); disp('sz = '),disp(sz);
sx =
-0.1404
sy =
-0.9795
sz =
0.1444
disp('ax = '),disp(ax); disp('ay = '),disp(ay); disp('az = '),disp(az);
ax =
-0.4948
ay =
-0.0566
az =
-0.8672
disp('px = '),disp(px); disp('py = '),disp(py); disp('pz = '),disp(pz);
px =
250.2776
py =
44.1013
pz =
-395.7931
% trigonometric substitution using element (2,4)
p1 = sqrt(px^2+py^2); % Rho value in the manuscript for theta 1
p3 = sqrt(d4^2+a3^2); % Rho value in the manuscript for theta 3
disp('p1 = '),disp(p1); disp('p3 = '),disp(p3);
p1 =
254.1334
p3 =
351
% Finding joint angle 1 _Teacher formula
theta1_1 = atan2(py,px)-atan2(d3/p1,sqrt(1-(d3/p1)^2)); % +/-...[1 -1]
theta1_2 = atan2(py,px)-atan2(d3/p1,-sqrt(1-(d3/p1)^2));
% Finding joint angle 3 _Teacher formula
K = (px^2+py^2+pz^2-a3^2-d4^2-a2^2-d3^2)/(2*a2);
disp('K= '),disp(K)
K=
-61.0067
% Teacher formula
theta3_1 = atan2(a3,d4)-atan2(K/p3,sqrt(1-(K/p3)^2)); % +/-...[1 -1]
theta3_2 = atan2(a3,d4)-atan2(K/p3,-sqrt(1-(K/p3)^2));
% Finding joint angle 2 _Teacher formula
% Theta2_1
s23_1 = ((-a2*cos(theta3_1)-a3)*pz+(a2*sin(theta3_1)-d4)*(px*cos(theta1_1)+py*sin(theta1_1))); %Y
c23_1 = ((a2*sin(theta3_1)-d4)*pz+(a2*cos(theta3_1)+a3)*(px*cos(theta1_1)+py*sin(theta1_1))); %X
theta23_1 = atan2(s23_1,c23_1);
theta2_1 = (theta23_1 - theta3_1);
% Theta2_2
s23_2 = ((-a2*cos(theta3_2)-a3)*pz+(a2*sin(theta3_2)-d4)*(px*cos(theta1_1)+py*sin(theta1_1)));%Y
c23_2 = ((a2*sin(theta3_2)-d4)*pz+(a2*cos(theta3_2)+a3)*(px*cos(theta1_1)+py*sin(theta1_1))); %X
theta23_2 = atan2(s23_2,c23_2);
theta2_2 = (theta23_2 - theta3_2);
% Theta2_3
s23_3 = ((-a2*cos(theta3_1)-a3)*pz+(a2*sin(theta3_1)-d4)*(px*cos(theta1_2)+py*sin(theta1_2)));%Y
c23_3 = ((a2*sin(theta3_1)-d4)*pz+(a2*cos(theta3_1)+a3)*(px*cos(theta1_2)+py*sin(theta1_2))); %X
theta23_3 = atan2(s23_3,c23_3);
theta2_3 = (theta23_3 - theta3_1);
% Theta2_4
s23_4 = ((-a2*cos(theta3_2)-a3)*pz+(a2*sin(theta3_2)-d4)*(px*cos(theta1_2)+py*sin(theta1_2)));%Y
c23_4 = ((a2*sin(theta3_2)-d4)*pz+(a2*cos(theta3_2)+a3)*(px*cos(theta1_2)+py*sin(theta1_2))); %X
theta23_4 = atan2(s23_4,c23_4);
theta2_4 = (theta23_4 - theta3_2);
% Finding joint angle 4 _Teacher formula
% Theta4_1
s4_1 = cos(theta1_1)*ay-sin(theta1_1)*ax;
c4_1 = -cos(theta1_1)*cos(theta23_1)*ax-sin(theta1_1)*cos(theta23_1)*ay+sin(theta23_1)*az;
theta4_1 = atan2(s4_1,c4_1);
% Theta4_2
s4_2 = cos(theta1_1)*ay-sin(theta1_1)*ax; %cos(theta1_2)*ay-sin(theta1_2)*ax;
c4_2 = -cos(theta1_1)*cos(theta23_2)*ax-sin(theta1_1)*cos(theta23_2)*ay+sin(theta23_2)*az;%...
...-cos(theta1_2)*cos(theta23_2)*ax-sin(theta1_2)*cos(theta23_2)*ay+sin(theta23_2)*az;
theta4_2 = atan2(s4_2,c4_2);
% Theta4_3
s4_3 = cos(theta1_2)*ay-sin(theta1_2)*ax; %cos(theta1_1)*ay-sin(theta1_1)*ax;
c4_3 = -cos(theta1_2)*cos(theta23_3)*ax-sin(theta1_2)*cos(theta23_3)*ay+sin(theta23_3)*az;%...
...c4_3 = -cos(theta1_1)*cos(theta23_3)*ax-sin(theta1_1)*cos(theta23_3)*ay+sin(theta23_3)*az;
theta4_3 = atan2(s4_3,c4_3);
% Theta4_4
s4_4 = cos(theta1_2)*ay-sin(theta1_2)*ax;
c4_4 = -cos(theta1_2)*cos(theta23_4)*ax-sin(theta1_2)*cos(theta23_4)*ay+sin(theta23_4)*az;
theta4_4 = atan2(s4_4,c4_4);
% Finding joint angle 5 _Teacher **corrected August 28 2021**
% Theta5_1
s5_1 = -(sin(theta1_1)*sin(theta4_1)+cos(theta1_1)*cos(theta23_1)*cos(theta4_1))*ax +...
(cos(theta1_1)*sin(theta4_1)-sin(theta1_1)*cos(theta23_1)*cos(theta4_1))*ay +...
(sin(theta23_1)*cos(theta4_1))*az;
c5_1 = -(cos(theta1_1)*sin(theta23_1))*ax -(sin(theta1_1)*sin(theta23_1))*ay -(cos(theta23_1))*az;
theta5_1 = atan2(s5_1,c5_1);
% Theta5_2
s5_2 = -(sin(theta1_1)*sin(theta4_2)+cos(theta1_1)*cos(theta23_2)*cos(theta4_2))*ax +...
(cos(theta1_1)*sin(theta4_2)-sin(theta1_1)*cos(theta23_2)*cos(theta4_2))*ay +...
(sin(theta23_2)*cos(theta4_2))*az;
c5_2 = -(cos(theta1_1)*sin(theta23_2))*ax -(sin(theta1_1)*sin(theta23_2))*ay -(cos(theta23_2))*az;
theta5_2 = atan2(s5_2,c5_2);
% Theta5_3
s5_3 = -(sin(theta1_2)*sin(theta4_3)+cos(theta1_2)*cos(theta23_3)*cos(theta4_3))*ax +...
(cos(theta1_2)*sin(theta4_3)-sin(theta1_2)*cos(theta23_3)*cos(theta4_3))*ay +...
(sin(theta23_3)*cos(theta4_3))*az;
c5_3 = -(cos(theta1_2)*sin(theta23_3))*ax -(sin(theta1_2)*sin(theta23_3))*ay -(cos(theta23_3))*az;
theta5_3 = atan2(s5_3,c5_3);
% Theta5_4
s5_4 = -(sin(theta1_2)*sin(theta4_4)+cos(theta1_2)*cos(theta23_4)*cos(theta4_4))*ax +...
(cos(theta1_2)*sin(theta4_4)-sin(theta1_2)*cos(theta23_4)*cos(theta4_4))*ay +...
(sin(theta23_4)*cos(theta4_4))*az;
c5_4 = -(cos(theta1_2)*sin(theta23_4))*ax -(sin(theta1_2)*sin(theta23_4))*ay -(cos(theta23_4))*az;
theta5_4 = atan2(s5_4,c5_4);
% % Finding joint angle 6 _Teacher formula
% Theta6_1
s6_1 = -(cos(theta1_1)*cos(theta23_1)*sin(theta4_1)-sin(theta1_1)*cos(theta4_1))*nx...
-(sin(theta1_1)*cos(theta23_1)*sin(theta4_1)+cos(theta1_1)*cos(theta4_1))*ny...
+(sin(theta23_1)*sin(theta4_1))*nz;
c6_1 = (cos(theta1_1)*cos(theta23_1)*cos(theta4_1)*cos(theta5_1)-cos(theta1_1)*sin(theta23_1)...
*sin(theta5_1)+sin(theta1_1)*sin(theta4_1)*cos(theta5_1))*nx...
+(sin(theta1_1)*cos(theta23_1)*cos(theta4_1)*cos(theta5_1)-sin(theta1_1)*sin(theta23_1)...
*sin(theta5_1)-cos(theta1_1)*sin(theta4_1)*cos(theta5_1))*ny...
-(sin(theta23_1)*cos(theta4_1)*cos(theta5_1)+cos(theta23_1)*sin(theta5_1))*nz;
theta6_1 = atan2(s6_1,c6_1);
% Theta6_2
s6_2 = -(cos(theta1_1)*cos(theta23_2)*sin(theta4_2)-sin(theta1_1)*cos(theta4_2))*nx...
-(sin(theta1_1)*cos(theta23_2)*sin(theta4_2)+cos(theta1_1)*cos(theta4_2))*ny...
+(sin(theta23_2)*sin(theta4_2))*nz;
c6_2 = (cos(theta1_1)*cos(theta23_2)*cos(theta4_2)*cos(theta5_2)-cos(theta1_1)*sin(theta23_2)...
*sin(theta5_2)+sin(theta1_1)*sin(theta4_2)*cos(theta5_2))*nx...
+(sin(theta1_1)*cos(theta23_2)*cos(theta4_2)*cos(theta5_2)-sin(theta1_1)*sin(theta23_2)...
*sin(theta5_2)-cos(theta1_1)*sin(theta4_2)*cos(theta5_2))*ny...
-(sin(theta23_2)*cos(theta4_2)*cos(theta5_2)+cos(theta23_2)*sin(theta5_2))*nz;
theta6_2 = atan2(s6_2,c6_2);
% Theta6_3
s6_3 = -(cos(theta1_2)*cos(theta23_3)*sin(theta4_3)-sin(theta1_2)*cos(theta4_3))*nx...
-(sin(theta1_2)*cos(theta23_3)*sin(theta4_3)+cos(theta1_2)*cos(theta4_3))*ny...
+(sin(theta23_3)*sin(theta4_3))*nz;
c6_3 = (cos(theta1_2)*cos(theta23_3)*cos(theta4_3)*cos(theta5_3)-cos(theta1_2)*sin(theta23_3)...
*sin(theta5_3)+sin(theta1_2)*sin(theta4_3)*cos(theta5_3))*nx...
+(sin(theta1_2)*cos(theta23_3)*cos(theta4_3)*cos(theta5_3)-sin(theta1_2)*sin(theta23_3)...
*sin(theta5_3)-cos(theta1_2)*sin(theta4_3)*cos(theta5_3))*ny...
-(sin(theta23_3)*cos(theta4_3)*cos(theta5_3)+cos(theta23_3)*sin(theta5_3))*nz;
theta6_3 = atan2(s6_3,c6_3);
% Theta6_4
s6_4 = -(cos(theta1_2)*cos(theta23_4)*sin(theta4_4)-sin(theta1_2)*cos(theta4_4))*nx...
-(sin(theta1_2)*cos(theta23_4)*sin(theta4_4)+cos(theta1_2)*cos(theta4_4))*ny...
+(sin(theta23_4)*sin(theta4_4))*nz;
c6_4 = (cos(theta1_2)*cos(theta23_4)*cos(theta4_4)*cos(theta5_4)-cos(theta1_2)*sin(theta23_4)...
*sin(theta5_4)+sin(theta1_2)*sin(theta4_4)*cos(theta5_4))*nx...
+(sin(theta1_2)*cos(theta23_4)*cos(theta4_4)*cos(theta5_4)-sin(theta1_2)*sin(theta23_4)...
*sin(theta5_4)-cos(theta1_2)*sin(theta4_4)*cos(theta5_4))*ny...
-(sin(theta23_4)*cos(theta4_4)*cos(theta5_4)+cos(theta23_4)*sin(theta5_4))*nz;
theta6_4 = atan2(s6_4,c6_4);
% Converting Theta 1 from radian to degrees
theta1_1 = rad2deg(theta1_1);
theta1_2 = rad2deg(theta1_2);
disp('theta1_1 ='),disp(theta1_1)
theta1_1 =
9.9935
disp('theta1_2 ='),disp(theta1_2)
theta1_2 =
-170.0065
% Converting Theta 3 from radian to degrees
theta3_1 = rad2deg(theta3_1);
theta3_2 = rad2deg(theta3_2);
disp('theta3_1 ='),disp(theta3_1)
theta3_1 =
10.0093
disp('theta3_2 ='),disp(theta3_2)
theta3_2 =
169.9907
% Converting Theta 2 from radian to degrees
theta2_1 = rad2deg(theta2_1);
disp('theta2_1 ='),disp(theta2_1)
theta2_1 =
9.9987
theta2_2 = rad2deg(theta2_2);
disp('theta2_2 ='),disp(theta2_2)
theta2_2 =
-255.4066
theta2_3 = rad2deg(theta2_3);
disp('theta2_3 ='),disp(theta2_3)
theta2_3 =
75.4066
theta2_4 = rad2deg(theta2_4);
disp('theta2_4 ='),disp(theta2_4)
theta2_4 =
-189.9987
% Converting Theta 4 from radian to degrees
theta4_1 = rad2deg(theta4_1);
disp('theta4_1 ='),disp(theta4_1)
theta4_1 =
10.0102
theta4_2 = rad2deg(theta4_2);
disp('theta4_2 ='),disp(theta4_2)
theta4_2 =
1.9056
theta4_3 = rad2deg(theta4_3);
disp('theta4_3 ='),disp(theta4_3)
theta4_3 =
-178.0944
theta4_4 = rad2deg(theta4_4);
disp('theta4_4 ='),disp(theta4_4)
theta4_4 =
-169.9898
% Converting Theta 5 from radian to degrees
theta5_1 = rad2deg(theta5_1);
disp('theta5_1 ='),disp(theta5_1)
theta5_1 =
9.9657
theta5_2 = rad2deg(theta5_2);
disp('theta5_2 ='),disp(theta5_2)
theta5_2 =
115.2286
theta5_3 = rad2deg(theta5_3);
disp('theta5_3 ='),disp(theta5_3)
theta5_3 =
115.2286
theta5_4 = rad2deg(theta5_4);
disp('theta5_4 ='),disp(theta5_4)
theta5_4 =
9.9657
% Converting Theta 6 from radian to degrees
theta6_1 = rad2deg(theta6_1);
disp('theta6_1 ='),disp(theta6_1)
theta6_1 =
9.9789
theta6_2 = rad2deg(theta6_2);
disp('theta6_2 ='),disp(theta6_2)
theta6_2 =
20.6535
theta6_3 = rad2deg(theta6_3);
disp('theta6_3 ='),disp(theta6_3)
theta6_3 =
20.6535
theta6_4 = rad2deg(theta6_4);
disp('theta6_4 ='),disp(theta6_4)
theta6_4 =
9.9789
%% Inverse kinematics solutions test1
Sol1 = [ theta1_1,theta2_1,theta3_1,theta4_1,theta5_1,theta6_1];
Sol2 = [theta1_1,theta2_2,theta3_2,theta4_2,theta5_2,theta6_2];
Sol3 = [theta1_2,theta2_3,theta3_1,theta4_3,theta5_3,theta6_3];
Sol4 = [theta1_2,theta2_4,theta3_2,theta4_4,theta5_4,theta6_4];
Sol5 = [ theta1_1,theta2_1,theta3_1,theta4_1+pi,-theta5_1,theta6_1+pi];
Sol6 = [theta1_1,theta2_2,theta3_2,theta4_2+pi,-theta5_2,theta6_2+pi];
Sol7 = [theta1_2,theta2_3,theta3_1,theta4_3+pi,-theta5_3,theta6_3+pi];
Sol8 = [theta1_2,theta2_4,theta3_2,theta4_4+pi,-theta5_4,theta6_4+pi];
Eightsols = [Sol1;Sol2;Sol3;Sol4;Sol5;Sol6;Sol7;Sol8];
disp('The 8 solutions are:'),disp(Eightsols)
The 8 solutions are:
9.9935 9.9987 10.0093 10.0102 9.9657 9.9789
9.9935 -255.4066 169.9907 1.9056 115.2286 20.6535
-170.0065 75.4066 10.0093 -178.0944 115.2286 20.6535
-170.0065 -189.9987 169.9907 -169.9898 9.9657 9.9789
9.9935 9.9987 10.0093 13.1518 -9.9657 13.1204
9.9935 -255.4066 169.9907 5.0472 -115.2286 23.7951
-170.0065 75.4066 10.0093 -174.9528 -115.2286 23.7951
-170.0065 -189.9987 169.9907 -166.8482 -9.9657 13.1204
Bjorn Gustavsson
on 15 Oct 2021
So instead of printing these 8 solutions to the command-window make Eightsols an output argument of this function. Then handle those eight solutions one-by-one as input to whatever function you might need to calculate the results you want - and save those in whatever format you want. Perhaps this should be done in some kind of script where you define the input-parameters to the first function, then call the first function, and handle its outputs and call the second function to produce the results you want to save, and finally save them in your preferred formats (for that have a look at the help and documentation to xlswrite).
Konard Adams
on 16 Oct 2021
Edited: Konard Adams
on 18 Oct 2021
Thank you! I got it. You are appreciated.
Bjorn Gustavsson
on 18 Oct 2021
My pleasure.
(I saw your most recent question, but thought: You'll become a much better programmer by figuring that out yourself - it's not nice sort-term but on any longer time-scale than 3 days it is to be preferred. Now you can swim!)
Konard Adams
on 18 Oct 2021
Cheers!
More Answers (0)
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Products
See Also
on 14 Oct 2021
on 18 Oct 2021
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)