# how to make a function - keep getting errors

3 vues (au cours des 30 derniers jours)
N le 17 Fév 2014
Commenté : N le 18 Fév 2014
I need to make a function of some calculation steps that I made. the steps I have are=
f=input('frame number');
origin=(LE(:,f)+ME(:,f))/2;
Y=(FH(:,f)-origin)/norm(FH(:,f)-origin);
ztemp=(ME(:,f)-LE(:,f))/norm(ME(:,f)-LE(:,f));
x=cross(Y,ztemp);
X=x/norm(x);
z=cross(X,Y);
Z=z/norm(z);
R=[X Y Z]
end
now I need to make this in a function, what I did was :
f=input('frame number');
[X,Y,Z]=myfunction(f)
R=[X Y Z]
and then a file named myfunction
function [X,Y,Z]=myfunction(f)
origin=(LE(:,f)+ME(:,f))/2;
Y=(FH(:,f)-origin)/norm(FH(:,f)-origin);
ztemp=(ME(:,f)-LE(:,f))/norm(ME(:,f)-LE(:,f));
x=cross(Y,ztemp);
X=x/norm(x);
z=cross(X,Y);
Z=z/norm(z);
end
it refuses to work however and I get the errors= - Output argument "X" (and maybe others) not assigned during call to and -Error: Illegal use of reserved keyword "end".
what do I need to do ?
##### 3 commentairesAfficher 1 commentaire plus ancienMasquer 1 commentaire plus ancien
Thomas le 17 Fév 2014
where are LE and ME?
N le 17 Fév 2014
LE and ME are loaded beforehand. The calculation works if I just run it with using the first code, so I get a matrix R with X,Y and Z. The problem I have is putting it in a function format, then it doesn't work any more for me.

Connectez-vous pour commenter.

### Réponse acceptée

N le 17 Fév 2014
the numbers FH, LE and ME were loaded in another question, so matlab knows what these points are.They are just datapoints. The code is all saved in the same datapath. I known the first code works, I have tried it, but now I need to transform it in a function, which is were I'm having the problem.I tried the second and third, so the second as the general code and the third as the function, but it doesn't work.
##### 2 commentairesAfficher AucuneMasquer Aucune
Wayne King le 17 Fév 2014
The problem is when you transform it into a function and try to call the function from the command line, the function will not know what those variables are. It is not sufficient that they exist in the MATLAB workspace.
N le 18 Fév 2014
thankx this actually solved my problem, I did forget to make the variables clear before hand. once I did this the function worked !

Connectez-vous pour commenter.

### Plus de réponses (3)

William le 17 Fév 2014
MATLAB is case sensitive, it looks like you have a lower case x and an upper case 'X' in your code. if they mean the same thing they have to be the same case
post your code as such
if true
Myfunction = X etc
end
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

Wayne King le 17 Fév 2014
How is your function supposed to know what LE() and ME() are for starters? Or FH()?
You do not provide those arrays as input arguments to the function.
How are you attempting to the use the function, did you save it in a folder on the MATLAB path as myfunction.m?
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

Image Analyst le 17 Fév 2014
Are these all in one m-file, or in 3 separate m-files? If the first one is a script, you can't have an end at the end, it doesn't make sense. If they're all in one file, you need "function" keyword to declare each function, and you don't need "end"s but if you choose to use one then I think you need to use it for all of the functions in the m-file. You can't have a script followed by a function in the same file.
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

### Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by