0

I wrote some code that works just fine to evaluate theta on its own with some test input. However, I would like to take this code and turn it into a function that I can call within another matlab file. I keep getting the error message, "Function definitions are not permitted in this context."

I want to be able to define four vectors in another matlab file and call SP1 to evaluate theta for those inputs. I'm not sure where I'm going wrong, though. Please help!

Thanks so much.

    clc
    clear all 

    function theta = SP1(p,q1,w1,r)

    % INPUT: 
    %function theta = SP1(p,q1,w1,r)
    % p = [5; -7; 12]; 
    % q1 = [17.3037; -3.1128; 2.48175];
    % w1 = [1/sqrt(8); sqrt(3/8); 1/sqrt(2)];
    % r = [1; 2; -3]; 

   % Define vectors u and v as well as u' and v'. 

   u = p - r;
   v = q1 - r; 
   w1_t = transpose(w1);
   u_prime = u - w1 * w1_t * u;
   v_prime = v - w1 * w1_t * v; 

   % Calculate theta if conditions are met for a solution to exist. 

    if (abs(norm(u_prime)-norm(v_prime))<0.01) & (abs((w1_t * u)-(w1_t * v))<0.01)
       X = w1_t*cross(u_prime,v_prime);
       Y = dot(u_prime,v_prime);
       theta = atan2(X,Y)                      
   else if (norm(u_prime) == 0 | norm(v_prime) == 0)    
       disp('Infinite Number of Solutions')
       else
    disp('Conditions not satisfied to find a solution')       
end
end
  • If you have time, have a look to the Matlab website: http://www.mathworks.se/help/matlab/matlab_prog/create-functions-in-files.html – tashuhka Mar 11 '14 at 19:16

2 Answers2

1

I think you can just remove the top two lines,

clc  
clear all

and save the rest of the code starting with function as SP1.m file.

Then you should be able to call this function as SP1 from other m files.

tashuhka
  • 5,028
  • 4
  • 45
  • 64
Song
  • 11
  • 2
  • Thank you! When I do that, Matlab still asks for my input. I tried to call SP1.m in another file by typing load('SP1.m') at the top, and I am now getting the error message:Error using load Number of columns on line 29 of ASCII file C:\Users\Documents\SP1.m must be the same as previous lines. Error in PA2 (line 4) load('SP1.m') – user3196474 Mar 11 '14 at 19:11
  • To test, you can put the SP1.m in the PATH, call the SP1 function from a matlab console window (example: "SP1(1,2,3,4)" ) – Song Mar 11 '14 at 19:20
  • Since the inputs for SP1 are all 3x1 vectors, will that work if I simply use 1,2,3,4? – user3196474 Mar 11 '14 at 19:24
1

I think you're confused about how functions work. The first line of a function definition defines how many inputs and outputs MATLAB expects:

function theta = SP1(p,q1,w1,r)

This means that calling a function SP1 will require you to give four inputs, and will return one output. It doesn't mean that:

  • Your inputs need to be named p, q1 and so on
  • Your output will be called theta automatically
  • The function will automatically take in the input variables p, q1, etc if they exist in the workspace.

It also doesn't do any checking on the inputs; so if you require that inputs be of a certain type, size, etc. you need to write your own error checking at the start of the file. You might intend that those inputs be 3x1 vectors, but there's nothing in the function to tell MATLAB that. So, SP1(1,2,3,4) will work, to some extent - it will take those inputs and try to run them through the function, and if they don't cause an error it will give you an output. The output might be wrong, but the computer doesn't know that.

Once you have a function you can call it multiple ways from the command line or from within other functions or scripts. As previously mentioned you don't have to stick to the naming of variables within the function, as long as input variables exist when the function is called MATLAB will accept them:

theta = SP1(p8,q27,w35,not_r);
myoutput = SP1(any,variable,I,like);

I don't necessarily have to give an output (but then the first output will be routed to ans)

SP1(this,will,also,work);

If I have some variables stored in a *.mat file (the case you seem to be asking about), I can do it like this:

load('mydata.mat'); %this file contains stored variables p, q1, w1 and r
theta = SP1(p,q1,w1,r);
nkjt
  • 7,825
  • 9
  • 22
  • 28