Incorporating Structure Fields into a Function as Inputs

1 view (last 30 days)
I'm using MATLAB (Mapping Toolbox) to create a large number of lines between different countries. Since there are so many lines, I'm trying to do this using object-oriented programming. This is the method I've written:
function transline = createlines(transline,Name,base.CapTr.val(a,b),base.EtrOut.val(:,a,b,1),base.EtrOut.val(:,b,a,1),base.EtrIn.val(:,a,b,1),base.EtrIn.val(:,b,a,1),... coords(c,2),coords(c,1),coords(d,2),coords(d,1))
where base is a struct and I intend to substitute different values for 'a' and 'b', e.g., base.CapTr.val(3,4) and create new objects.
The problem is I can't include base.CapTr.val(a,b) and the subsequent entries as inputs to the function because MATLAB declares the '.' to be an unexpected operator.
So, can you please tell me how I can add varying values of base.CapTr.val(a,b), etc. as inputs to the function?
I don't know if I've explained my problem properly, but I hope it's clear.
Thank you.

Accepted Answer

Cedric
Cedric on 12 May 2013
Edited: Cedric on 12 May 2013
I am not sure that I understand what you are trying to do .. so you have a class e.g. Transline, which contains a method createlines
methods
function transline = Transline( .. some input args .. )
transline = .. some initialization ..
end
function transline = createlines(transline, .. additional input args .. )
.. body of the method ..
end
end
Now when createlines is called as e.g.
transline = transline.createlines(S, 8, [3,4]) ;
the object is "stored into" (or accessible through) the transline that is a local variable and an input argument to the function createlines, S is stored into the second argument, etc, such as if creatlines were declared as
function transline = createlines(transline, arg2, arg3, arg4)
you would have S from the call available through arg2, 8 from the call available through arg3 and [3,4] from the call available through arg4.
Then you could do whatever you want with arg2.f1 and arg2.f2 within the method. In particular, if these two fields contain arrays, you could index them using values of properties, or extra args passed during the call. You could also pass directly relevant values at the call. What you cannot do is to write the function declaration using indexed structures (arrays, structs, etc) in place of "flat" argument names.
More explicitly, if the class defines properties
properties
coord, countryID, lines
end
with coord a struct with fields lon and lat, you can do either
function transline = createlines(transline, lon, lat)
transline.lines = ... something based on lon and lat ...
end
with a call like
id = transline.countryID ;
coord = transline.coord ;
transline = transline.createlines(coord.lon(id), coord.lat(id)) ;
or
function transline = createlines(transline, coord, countryID)
transline.lines = ... something based on coord.lon(countryID) and
coord.lat(countryID) ...
end
with a call like
transline = transline.createlines(transline.coord, transline.countryID) ;
but not
function transline = ...
createlines(transline, transline.coord.lat(transline.countryID), ..etc)
...
end
because "transline.coord.lat(transline.countryID)" doesn't provide MATLAB with a valid name for a local variable (which is what input arguments are ultimately).
Hope it helps (at least to refine your question if needed)!
  4 Comments
Usman
Usman on 23 May 2013
Hi Cedric,
Thanks a lot for your help!!!
Best regards,
Usman

Sign in to comment.

More Answers (0)

Categories

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