how can i add unit in both input and output

7 ビュー (過去 30 日間)
Ga Ma
Ga Ma 2020 年 4 月 7 日
コメント済み: Ga Ma 2020 年 4 月 8 日
I have a function looks like this. Now, i want add unit in both input and output. For example, if i i run money("dollar", 3), i will get: price= 3 dollar. Or if i run money("dollar",2,3 ), i will get price = 48 dollar. i don't know what code can do this.. please help me, thank you very much.
  1 件のコメント
Geoff Hayes
Geoff Hayes 2020 年 4 月 8 日
Is it necessary to pass in the units of the input variable(s)? Does the function body depend upon it for anyting other than providing an output string (or two parameters?) that indicate a numeric value and the unit?

サインインしてコメントする。

採用された回答

Image Analyst
Image Analyst 2020 年 4 月 8 日
Not exactly sure what formula you're using but this might be close:
price = money('dollar')
price = money('dollar', 2)
price = money('dollar', 2, 3)
function price = money(varargin)
price = '';
% nargin
% varargin
if nargin < 2
return;
end
if nargin >= 2
units = varargin{1};
p = varargin{2};
p0 = varargin{2};
end
if nargin >= 3
exponent = varargin{3};
% Compute p = k * k^2 * k^3 * k^4 * etc.
for k = 2 : exponent
p = p * p0 ^ k;
end
end
price = sprintf('price = %.2f %s', p, units);
end
  1 件のコメント
Ga Ma
Ga Ma 2020 年 4 月 8 日
Yes, it works. Thank you very much!!! Have a good day!

サインインしてコメントする。

その他の回答 (0 件)

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by