Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Quick matlab question

  1. Jun 2, 2008 #1
    1. The problem statement, all variables and given/known data
    Just wondering if someone could please steer me in the right dirrection for the correct syntax in matlab. I am trying to create a variable from an array of numbers and combine it with the letter k, to give me k1=[] k2=[] k3=[] etc. where [] is a 3x3 matracie

    2. Relevant equations

    3. The attempt at a solution

    This isn't the problem I am working on but the solution to this would be a great help.

    a=[1 3];
    ['k' int2str(a(2))]=[0 1 2;1 2 3;0 1 2]
    ??? Error: An array for multiple LHS assignment cannot contain character string.

    Trying to get this

    k3 =

    0 1 2
    1 2 3
    0 1 2

    1. The problem statement, all variables and given/known data

    2. Relevant equations

    3. The attempt at a solution
  2. jcsd
  3. Jun 2, 2008 #2


    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Use sprintf and eval.

    - Warren
  4. Jun 3, 2008 #3
    Thanks for the reply, I did get the function to work, but I did read on the net that this was a long winded way of going through the process. does matlab have any operations to make this simpler?
  5. Jun 9, 2008 #4
    Just one more question regarding matlab, I am having trouble working with angles. I think sin(alpha) needs to be -1 {or I might just be haveing a realy bad day with my maths}. Either way, can someone please explain what I need to do to get pi() instead of 0. I wrote a program the works off inputted co-odinates and it is not operating like it should for horrizontal member.
    consider moveing from point (0,0) to (-1,0)

    >> dx=-1-0

    dx =


    >> dy=0- 0

    dy =


    >> alpha=atan(dy/dx)

    alpha =


    >> sin(alpha)

    ans =


    For the rest of my program to work, I need to be able to evaluate sin properly otherwise the output is useless.

    This is probably an easy question and the solution might be quite obvious but with my limited experience I haven't managed to think of anything yet.

    Thank You
    Regards Elbarto
  6. Jun 9, 2008 #5

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    Use atan2 instead of atan.
  7. Jun 9, 2008 #6
    Thanks allot for the reply. Works a treat, just what I was looking for.

    Regards Elbarto
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook