Info

Cette question est clôturée. Rouvrir pour modifier ou répondre.

# How to call a function - with explanations

1 vue (au cours des 30 derniers jours)
Jay le 17 Sep 2018
Clôturé : MATLAB Answer Bot le 20 Août 2021
I have viewed the MATLAB documentation and I am still confused.
I am attempting to use the
convdegminssecs.m
The code (to save time and since it is short):
% CONVDEGMINSSECS: converts degrees, mins, secs to decimal degrees and
% INPUTS: deg,mins,secs
%
% EXAMPLE of USAGE:
% convdegminssecs(45,54,55)
%
% ans = 45.9153
%
deg = deg + mins/60 + secs/3600;
end
If I have D.MS value of 38.4810 and want it converted to Degrees Decimals, when specifying the function in the main script, how and where do I stipulate the following:
1. - The value to be used / converted in the function being invoked (DMS value)
2. - The functions name
3. - The variable for the output (DD value)
Please state what each component of the command does what for ease of comprehension so I know how to call functions in the future.

### Réponses (1)

Walter Roberson le 17 Sep 2018
Interpreting 38.4810 as meaning 38 degrees, 48 minutes, 10 seconds, then:
D_MS = 38.4810; %original in hybrid notation
D = fix(D_MS); %degrees
MS = round((abs(D_MS) - abs(D)) * 10000); %temporary variable to hold minutes and seconds in hybrid notation
M = floor(MS/100); %minutes
S = mod(MS, 100); %seconds
DD = convdegminssecs(D, M, S); %DD is output
This code does not assume that D_MS is a positive value: if that could be assumed then the code would be a little shorter.
##### 2 commentairesAfficher AucuneMasquer Aucune
Jay le 17 Sep 2018
Hi Walter,
I was looking more for the way to execute the function rather than writing the code.
I.e. in the main script file:
function x = y
and using the hyperlink I attached with the explanation as to what the function x = y command is doing.
I hope what I am asking is still not too ambiguous.
Walter Roberson le 17 Sep 2018
The line
declares that the function accepts up to three input parameters (no more.) When the function is called, the first positional parameter will be made available to the function under the variable name deg, and the second positional parameter will be made available to the function under the variable name mins, and the third positional parameter will be made available to the function under the variable name secs . If the function is called with fewer than three parameters, then that is only an error if the function tries to use the missing parameter. There are ways in MATLAB to test whether a parameter has been provided.
The function header also declares that the function returns up to two output variables (no more.) Anything that the code assigns to the first variable, deg, will be returned to the first variable in any assignment that the function call is assigned in, and anything that the code assigns to the second variable, rad, will be returned to the second variable in any assignment that the function call is assigned in. It is not an error to call the function in a context that does not expect any variables to be returned, and it is not an error to call the function in a context that expects only one variable be returned, but in a context that expects three or more variables to be returned an error would be generated.
If the function is called in an expression that is not an assignment, then in most cases it will be only the value of the first output position that will be used in the expression. For example if you had
convdegminssecs(38,16,42).^2
then it would be whatever had been assigned to the first output position (deg) that was squared.

### Community Treasure Hunt

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

Start Hunting!

Translated by