Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

To resolve issues starting MATLAB on Mac OS X 10.10 (Yosemite) visit: http://www.mathworks.com/matlabcentral/answers/159016

Calculating the angle between two vectors

Asked by Marcelo Costa on 20 Nov 2012

Is my code correct? I need to calculate the angle between these vectors, I would check if the command is correct!

clear all
  Files=dir('*.txt');
  [z,n]=size(Files);
  for x=1:z
     arq=load(Files(x).name);
     [v,b]=size(arq);
  for m=1:v;
      pre_maleolo=atan2(norm(cross(arq(m,1:3)-arq(m,4:6),arq(m,7:9)-arq(m,4:6))),dot(arq(m,1:3)-arq(m,4:6),arq(m,7:9)-arq(m,4:6)));
      pre_joelho=atan2(norm(cross(arq(m,4:6)-arq(m,7:9),arq(m,10:12)-arq(m,7:9))),dot(arq(m,4:6)-arq(m,7:9),arq(m,10:12)-arq(m,7:9)));
      pre_quadril=atan2(norm(cross(arq(m,7:9)-arq(m,10:12),arq(m,13:15)-arq(m,10:12))),dot(arq(m,7:9)-arq(m,10:12),arq(m,13:15)-arq(m,10:12)));
      pre_tronco=atan2(norm(cross(arq(m,10:12)-arq(m,13:15),arq(m,16:18)-arq(m,13:15))),dot(arq(m,10:12)-arq(m,13:15),arq(m,16:18)-arq(m,13:15)));
      maleolo(m)=pre_maleolo*180/pi;
      joelho(m)=pre_joelho*180/pi;
      quadril(m)=pre_quadril*180/pi;
      tronco(m)=pre_tronco*180/pi;
       end
        savefile=['angulos','.',Files(x).name];
        save(fullfile('C:','TCC',savefile),'maleolo','joelho','quadril','tronco','-ASCII');
    end

0 Comments

Marcelo Costa

Products

No products are associated with this question.

1 Answer

Answer by Jan Simon on 20 Nov 2012

Usually after arq=load(Files(x).name) the variable arq is a struct. I think, you want to use a specific field of it. If you have a different situation, please explain this explicitly.

There is no chance that we can guess if parts like "arq(m,7:9)-arq(m,4:6)" are correct or not. There is neither a documentation nor descriptive names of variables.

You forgot what you think about the correctness of your code. Do you error messages or do the results differ from your expectations? Did you create test data with known output and compared it with the results?

3 Comments

Marcelo Costa on 20 Nov 2012

the error is that the graphical representation of the data is reverse of which is actually.

About the first part arq=load(Files(x).name) is working.

example of which is composed as a txt file.

metatarsl(x) metatarsl(y) metatarsl(z) malleolus(x) malleolus(y) malleolus(z) epicondyle(x) epicondyle(y) epicondyle(z) trochanter(x) trochanter(y) trochanter(z) iliac(x) iliac(y) iliac(z) acromion(x) acromion(y) acromion(z)

Jan Simon on 20 Nov 2012

@Marcelo: There is no graphical output in your code. And what exactly is "reverse" here? Does the example of the text file help to understand the problem? It is surprising that the text file does not contain any numbers.

I suggest to update the question substantially: Take into account, that we do not have the faintest idea about what you are doing. Omit all code, which is not related to the problem. Consider, that is is even for the author very hard to debug lines like this:

atan2(norm(cross(arq(m,10:12)-arq(m,13:15),arq(m,16:18)-arq(m,13:15))),dot(arq(m,10:12)-arq(m,13:15),arq(m,16:18)-arq(m,13:15)))

Without knowing what this line should compute, a debugging is impossible.

Marcelo Costa on 20 Nov 2012

This a example file. https://www.dropbox.com/s/rposu6mprscafha/suj1_chute1_pos.txt

The header of file is metatarsl(x) metatarsl(y) metatarsl(z) malleolus(x) malleolus(y) malleolus(z) epicondyle(x) epicondyle(y) epicondyle(z) trochanter(x) trochanter(y) trochanter(z) iliac(x) iliac(y) iliac(z) acromion(x) acromion(y) acromion(z)

In another code i draw the vector and the two vector: A(metatrsl(x,y,z),malleolus(x,y,z)) and B(epycondyle(x.y.z),malleolus(x,y,z)) are reverse. Vector A is a foot and vector B is shank, but the shank is in place of the foot, and the foot is in place of the shank. I need to calc the angle between this two vectors.

My question is really this part of the calculation is correct to calculate the angle of these two vectors.

atan2(norm(cross(arq(m,1:3)-arq(m,4:6),arq(m,7:9)-arq(m,4:6))),dot(arq(m,1:3)-arq(m,13:15),arq(m,7:9)-arq(m,4:6)))

Sorry for my confusion, it´s the first time i use this support. Thank you too for trying to understand the misunderstanding of my question

Jan Simon

Contact us