Info

この質問は閉じられています。 編集または回答するには再度開いてください。

How to call a function - with explanations

1 回表示 (過去 30 日間)
Jay
Jay 2018 年 9 月 17 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
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):
function [deg,rad] = convdegminssecs(deg,mins,secs)
% CONVDEGMINSSECS: converts degrees, mins, secs to decimal degrees and
% output answer in decimal degrees and radians
% INPUTS: deg,mins,secs
% OUTPUTS: deg,rad
%
% EXAMPLE of USAGE:
% convdegminssecs(45,54,55)
%
% ans = 45.9153
%
deg = deg + mins/60 + secs/3600;
rad = deg * pi/180;
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.
Thank you in advance.

回答 (1 件)

Walter Roberson
Walter Roberson 2018 年 9 月 17 日
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 件のコメント
Jay
Jay 2018 年 9 月 17 日
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
Walter Roberson 2018 年 9 月 17 日
The line
function [deg,rad] = convdegminssecs(deg,mins,secs)
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