How to write a method to have input parameter a 1xN array?

4 views (last 30 days)
I want to write a method (function) in my class to return a matrix containing pairs of (xyz) coordinates as output. The input has to be a 1xN array (a vector) containing angles. For example: 0:pi/50:2*pi . Please find below the method inside the Helix class I am talking about, as well as the entire Helix class if you may find it useful to understand what is happening (if not, scroll down to the method discussed above):
classdef Helix
%Helix class
% Detailed explanation
properties(Access=protected)
trk; %just a CdfTrack property for this class' objects
end
methods
%constructor
function obj = Helix(t)
if nargin>0
obj(numel(t))= Helix; %preallocate an array[vector] of the right size for (Helix-type) objects
for i=1:numel(t)
obj(i).trk=t(i); %for every vector's element, set its trk property accordingly
end
end
end
%method to calculate the radius of the helix
function r = radius(obj)
r=zeros(numel(obj));
for i=1:numel(obj)
h=obj(i).trk.curvature();
r(i)=1/(2*h);
end
end
%method to calculate the center of the helix
function position = center (obj)
ll=numel(t);
position=zeros(2,ll); %an empty 2xnumel(t) matrix to be filled with (x,y) pairs
for j=1:numel(t)
wc = (r + obj(j).trk.d0()) * exp(i*(obj(j).trk.phi0()+ pi/2));%USELESS?
x = (r + obj(j).trk.d0()) * cos(obj(j).trk.phi0());
y = (-1)*(r+ obj(j).trk.d0()) * sin(obj(j).trk.phi0());
position(1,j)=x; position(2,j)=y;
end %here do we know about r from the previous function or do we have to call the radius() function INSIDE this second function?
end
%method to calculate the 3D coord. (x,y,z) from a vector of angles in radians
function threed = points(obj,ve) %does this function understands about obj?
n=size(ve,2); %how many intermediate steps we have in the input range
threed=zeros(n,3);%an empty nx3 matrix. we intend to transform it into: on rows pairs of (xyz) coordinates. n such pairs
for j=1:numel(obj)
% w= wc- i*r*exp(i*(obj(j).trk.phi0()+q*time));
for k=1:n %we fill the above matrix with the coordinates as explained
threed(k,1) = -(r(j) + obj(j).trk.d0())*sin(obj(j).trk.phi0()) + r(j) *sin(obj(j).trk.phi0()+ ve(1,k)); %this is x-coordinate.gradually go through (with that for loop) all the numbers in the (ve) vector to replicate the time evolution of coordinates
threed(k,2)= (r(j) +obj(j).trk.d0())*cos(obj(j).trk.phi0()) - r(j) *cos(obj(j).trk.phi0()+ ve(1,k)); %this is y-coordinate at a given instant dictated by ve(1,k)
threed(k,3) = obj(j).trk.z0() + abs(r)(j)*obj(1,k)*obj(j).trk.cotTheta();%this is z-coordinate at a given instant
end
end
end
end
end
When working in the console and calling: h=Helix(t) ,where t is an object of CdfTrack type(with 5 properties) ---> puts in h an object of type helix, with a hidden property called trk (not very relevant to the question). Then v = h.points([0:pi/100:6*pi]) ---> error: Error using Helix/points Too many input arguments.
I tried to include this range of values inside a variable first and then to call the function using that variable. Nothing works.
What do I do wrong? Thank you!
  1 Comment
dpb
dpb on 18 Sep 2017
The problem has to do with how the class methods are defined -- the function above would work passed a vector (altho it needs error checking to ensure it's a row or code to transform the input to the proper orientation for robustness that's not the actual problem).
The problem is the calling syntax isn't correct to call just a function; try just
v = points([0:pi/100:6*pi])
instead.
I've never used the Matlab class structure so don't know enough syntax to know just what is needed to make the function a method associated with a class, but there's where the issue is...

Sign in to comment.

Answers (1)

Cedric
Cedric on 18 Sep 2017
Edited: Cedric on 18 Sep 2017
Assuming that Helix is a handle class (subclass of handle), your method should be either static
methods( Static = true )
function threed = points( ve )
...
end
end
and called
threed = Helix.points( ve ) ;
or it has to accept the object as first argument:
function threed = points( obj, ve )
...
or
function threed = points( ~, ve )
...
if your don't need the object (yet).
Your error message comes from the fact that the method is automatically/internally called with two arguments: the object and ve.
  3 Comments
Cedric
Cedric on 18 Sep 2017
Thank you Guillaume, I hadn't looked at the equations!
Cedric
Cedric on 18 Sep 2017
Edited: Cedric on 18 Sep 2017
Iustin, after you edited your code in the questions, I propose that you update the indentation and comment out faulty lines as follows:
classdef Helix
%Helix class
% Detailed explanation
properties(Access=protected)
trk; %just a CdfTrack property for this class' objects
end
methods
%constructor
function obj = Helix(t)
if nargin>0
obj(numel(t))= Helix; %preallocate an array[vector] of the right size for (Helix-type) objects
for i=1:numel(t)
obj(i).trk=t(i); %for every vector's element, set its trk property accordingly
end
end
end
%method to calculate the radius of the helix
function r = radius(obj)
r=zeros(numel(obj));
for i=1:numel(obj)
h=obj(i).trk.curvature();
r(i)=1/(2*h);
end
end
%method to calculate the center of the helix
function position = center(obj)
ll=numel(t);
position=zeros(2,ll); %an empty 2xnumel(t) matrix to be filled with (x,y) pairs
for j=1:numel(t)
%%wc = (r + obj(j).trk.d0()) * exp(i*(obj(j).trk.phi0()+ pi/2));%USELESS?
%%x = (r + obj(j).trk.d0()) * cos(obj(j).trk.phi0());
%%y = (-1)*(r+ obj(j).trk.d0()) * sin(obj(j).trk.phi0());
%%position(1,j)=x; position(2,j)=y;
end %here do we know about r from the previous function or do we have to call the radius() function INSIDE this second function?
end
%method to calculate the 3D coord. (x,y,z) from a vector of angles in radians
function threed = points(obj,ve) %does this function understands about obj?
n=size(ve,2); %how many intermediate steps we have in the input range
threed=zeros(n,3);%an empty nx3 matrix. we intend to transform it into: on rows pairs of (xyz) coordinates. n such pairs
for j=1:numel(obj)
% w= wc- i*r*exp(i*(obj(j).trk.phi0()+q*time));
for k=1:n %we fill the above matrix with the coordinates as explained
%%threed(k,1) = -(r(j) + obj(j).trk.d0())*sin(obj(j).trk.phi0()) + r(j) *sin(obj(j).trk.phi0()+ ve(1,k)); %this is x-coordinate.gradually go through (with that for loop) all the numbers in the (ve) vector to replicate the time evolution of coordinates
%%threed(k,2)= (r(j) +obj(j).trk.d0())*cos(obj(j).trk.phi0()) - r(j) *cos(obj(j).trk.phi0()+ ve(1,k)); %this is y-coordinate at a given instant dictated by ve(1,k)
%%threed(k,3) = obj(j).trk.z0() + abs(r)(j)*obj(1,k)*obj(j).trk.cotTheta();%this is z-coordinate at a given instant
end
end
end
end
end
where I commented out 6 lines (starting with %%). After doing this, you can start playing with your Helix object(s):
>> h = Helix(1:4)
h =
1×4 Helix array with no properties.
>> h(1).points( [1,2] )
ans =
0 0 0
0 0 0
and see that it works except for the computations that had problems. In fact you get the value of threed defined during its initialization (0s). So the OOP mechanisms work, and you can focus on the rest.
One error was coming for your attempt to access the _j_th element of the output of ABS apparently (?)
abs(r)(j)
which you cannot do in MATLAB. You have to store the output in an intermediary variable and then index the variable.

Sign in to comment.

Categories

Find more on Graphics Object Programming 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!