Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: syntax for polar complex numbers
Date: Tue, 3 Nov 2009 18:47:02 +0000 (UTC)
Organization: Xoran Technologies
Lines: 71
Message-ID: <hcptr6$7vo$1@fred.mathworks.com>
References: <hcpmeu$h1o$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-03-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1257274022 8184 172.30.248.38 (3 Nov 2009 18:47:02 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Tue, 3 Nov 2009 18:47:02 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1440443
Xref: news.mathworks.com comp.soft-sys.matlab:582129


Another solution would be to use classdef to write your own Phasor data type.
I've created a prototype for you at the bottom of this post, which should be put in a file called Phasor.m 

Using the function Phasor() you can create a data type which is in all ways like MATLAB's usual complex variables, but which displays its contents in phasor form, e.g.

>> V=Phasor(1.01,.002)  %From your earlier example
 
[Mag Angle]:
 
    1.0100    0.0020

But the important thing is that you can also do arithmetic on it with complex numbers in whatever form you want, as in the following examples:

>> V/1+3i  %phasor/cartesian

ans =

   1.0100 + 3.0020i

>> V/V %phasor/phasor

ans =

     1

If you want the results displayed or saved in phasor form, you can use the Phasor() function once again:

>> result=Phasor(V/1+3i),
 
[Mag Angle]:
 
    3.1674    1.2463




%%%%The code below must go in a file Phasor.m

classdef Phasor < double
   
    methods
       
        function obj=Phasor(varargin)
            
            switch nargin
                
                case 2
                  [Mag,Angle]=deal(varargin{:});
                  [r,c]=pol2cart(Mag,Angle);
                  data=r+c*i;
                case 1
                  data=varargin{1};
            end            
            obj=obj@double(data);
            
        end
        
        function display(obj)
            
            [Angle,Mag]=cart2pol(real(obj), imag(obj));
            disp ' '
            disp('[Mag Angle]:')
            disp ' '
            disp([Mag(:),Angle(:)]);
            
   
        end
        
    end
    
end