# Error when using Matlab Function block in Simechanics (nested functions and parsing of Matlab function both error)

I create a simple simulink model using Matlab Function block to calculate angles of 4 dof robotic arm using Robotic toolbox Matlab while input is x, y and z values. I used forward kinematics to calculate angles. Model and error is shown in below images:

My code is given below (which is put in matlab funcrion block):

function [theta1,theta2,theta3,theta4]=invkin(px,py,pz)
% code to find inverse kinematics solution for 4 dof rootic arm
preach = [px py pz]; % reach point of end-effector
theta1 = 0;  theta2 = 0; theta3 = 0; theta4 = 0;
% create links using D-H parameters
L(1) = Link([ 0 0 0 pi/2 0], 'standard');
L(2) = Link([ 0 .15005 .4318 0 0], 'standard');
L(3) = Link([0 .0203 0 -pi/2 0], 'standard');
L(4) = Link([0 .4318 0 pi/2 0], 'standard');
L(1).m = 4.43;
L(2).m = 10.2;
L(3).m = 4.8;
L(4).m = 1.18;
%define center of gravity
L(1).r = [ 0 0 -0.08];
L(2).r = [ -0.216 0 0.026];
L(3).r = [ 0 0 0.216];
L(4).r = [ 0 0.02 0];
%define link inertial as a 6-element vector
%interpreted in the order of [Ixx Iyy Izz Ixy Iyz Ixz]
L(1).I = [ 0.195 0.195 0.026 0 0 0];
L(2).I = [ 0.588 1.886 1.470 0 0 0];
L(3).I = [ 0.324 0.324 0.017 0 0 0];
L(4).I = [ 3.83e-3 2.5e-3 3.83e-3 0 0 0];
% set limits for joints
%build the robot model
qready = [0 -pi/4 pi/4 0]; % initial position of robot
T1= transl(preach); % convert of reach point of 4x4 homogenous matrix
[qreach,err,exitflag] =  rob.ikcon(T1, qready); % find inverse kinematics with error
%rob.fkine(qreach);
theta1 = qreach(1,1);  theta2 = qreach(1,2); theta3 = qreach(1,3); theta4 = qreach(1,4);
end

How to solve these errors.

Thanks.

• Constant inputs to a Matlab function to displays... why are you using Simulink at all? Why not just do it all in a Matlab script?
– Chuck
Commented Mar 20, 2017 at 19:07

After looking closer at your code, I'll reiterate my comment - why are you doing this in Simulink? I'll make your code briefer for clarity:

function [theta]=invkin(px,py,pz)
theta = 0;
L = <const>;
preach = <const>;
rob = f(L);
T1= f(preach);
theta = qreach;
end;

So,

1. You aren't using the inputs at all.
2. You're constantly resetting theta to zero at the start of the script, so if you did happen to make the inputs time-varying, I don't think the time-varying output would be what you want because you're not progressing through time. Your script would run a complete simulation at every call.

Imagine plotting this from a Matlab script - plot(time,theta) - again, there's no time dependence here. You just have one value, theta, that describes the joint angles required to achieve the desired end effector position. Your plot would be a horizontal line.

I would imagine this might be contributing to the trouble you're having implementing the fix you were given.

• Sorry @Chuck, by mistake I made preach = [px py pz] as comment while pasting code on here. Still the error is same. Your first question is why theta = 0. Because if I don't use theta = 0, matlab give error like Error in port widths or dimensions. Output port 1 of 'matlabfunik/MATLAB Function/pz' is a one dimensional vector with 1 elements .And answer to your second question why I'm using simmechanics because inverse kinematics is first part after getting angle I want to do inverse dynamics of my robot. So, output from this I will used input in inverse dynamics. Commented Mar 20, 2017 at 22:07
• @NaseebGill - You're getting the error in port widths because your code isn't structured to do what you want it to do. The only reason I can think of that you're running this in Simulink is because you're trying to convert your [px py pz] to a trajectory (time varying arc) and want to simulate how the robot moves. In order for that to work, though, you need to get the joint angle solution and apply it to your robot. You may be intending to add some dynamic simulation, but you don't have it yet, so you're left running a static simulation in a dynamic environment.
– Chuck
Commented Mar 21, 2017 at 11:54

In Simulink the Matlab embedded function block supports only a subset of the available commands in Matlab. If you want to use external functions, you have to declare these using coder.extrinsic().

If the problem is with nested functions and not with declaring the external function, you could try to de-nest the function, by simply deleting the 'value' function from the file it is defined in and create a new, separate file for the function on the same path. Please keep in mind that this is a workaround, not really a fix.

• Thanks @50k4 for your reply but when used coder.extrinsic('value') in matlab function block, it shows same error. How to remove this. value function is shown below: function out = value(v, opt) if opt.sym out = sym(v); else out = v; end end Commented Mar 19, 2017 at 16:47