I need help with my Cartesian points to Polar code?

3 views (last 30 days)
This is what I have so far. I'm supposed to create a function that determines the polar coordinates of a point from Cartesian coordinates. It should output the angle (in degrees) and its radial distance to the point. It seems to be working correctly for this. However, I need to evaluate several points at once and I'm not sure how I can do more than one at a time. Any help is appreciated!
function [th rad] = CartToPolar(x,y)
prompt={'enter x: ','Enter y: '};
dlg_title='CartToPolar';
answer=inputdlg(prompt,dlg_title);
x = str2num(answer{1});
y = str2num(answer{2});
r=sqrt(x^2 +y^2)
theta=atan2(y/x)
end

Answers (1)

Guillaume
Guillaume on 15 Apr 2015
Edited: Guillaume on 15 Apr 2015
Your function already takes x and y as input so there is no need for it to prompt the user. It is in fact mostly your prompt that stops your function from working with vector / matrices.
So, get rid of all the interactive bit and only keep the last two lines. The other problem preventing your function from working with matrices is that you should use memberwise operators (the dotted version of .^ and ./) instead of matrix operations (^ and /). With scalar inputs the difference between .^2 and ^2 does not matter but it matter greatly with matrices.
It will then work.

Categories

Find more on Polar Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!