フィルターのクリア

dictionary() question: "Error: Output argument not assigned a value in the execution with 'x' function" - works with some inputs and not others even though code is identical.

10 ビュー (過去 30 日間)
Hi there,
I'm coding a unit converter for uni that applies the following user defined function to a user interface. This code isn't finished yet but I'm really scratching my head as why some conversions work and others don't.. even though the code for myunitconv is identical for all conversions and the function works fine when used in isolation.
Would appreciate any insights! Thanks!
This is the error I get:
Output argument "numout" (and possibly others) not assigned a value in the execution with "myunitconv" function.
Error in UnitConverter_FINAL (line 39)
numout = myunitconv(selection_input, ref(ConvSelection)); % calls unit converter function to apply conversion...
Function:
function [numout, unit] = myunitconv(numin,convtype)
if strcmp(convtype, 'cels2fahr') == 1;
numout = (numin*(9/5)+32);
unit = 'F';
elseif strcmp(convtype, 'fahr2cels') == 1;
numout = (numin-32)*(5/9);
unit = 'C';
elseif strcmp(convtype, 'cm2in') == 1;
numout = numin/2.54;
unit = 'in';
elseif strcmp(convtype, 'in2cm') == 1;
numout = numin*2.54;
unit = 'cm';
elseif strcmp(convtype, 'met2ft') == 1;
numout = numin*3.2808;
unit = 'ft';
elseif strcmp(convtype, 'ft2met') == 1;
numout = numin/3.2808;
unit = 'm';
elseif strcmp(convtype, 'km2mi') == 1;
numout = numin/1.6093;
unit = 'mi';
elseif strcmp(convtype, 'mi2km') == 1;
numout = numin*1.6093;
unit = 'km';
elseif strcmp(convtype, 'gr2oz') == 1;
numout = numin/28.3495;
unit = 'oz' ;
elseif strcmp(convtype, 'oz2gr') == 1;
numout = numin*28.3495;
unit = 'gr';
elseif strcmp(convtype, 'kg2lb') == 1;
numout = numin*2.2046;
unit = 'lb';
elseif strcmp(convtype, 'lb2kg') == 1;
numout = numin/2.2046;
unit = 'kg';
elseif strcmp(convtype, 't2LT') == 1;
numout = numin/1.0161;
unit = 'LT';
elseif strcmp(convtype, 'LT2t') == 1;
numout = numin*1.0161;
unit = 't';
else
disp('Error!')
end
Program:
% references numbers in user menu to conversions
ref = dictionary(... % creates dictionary called ref
[1 2 3 4 5 6 7 8 9 10 11 12 13 14],... % keys
["cels2far" "fahr2cel" "cm2in" "in2cm" "met2ft" "ft2met" "km2mi" "mi2km" "gm2oz" "oz2gm" "kg2lb" "lb2kg" "LT2t" "t2LT"]) % values
ref_prevUnit = dictionary(...
[1 2 3 4 5 6 7 8 9 10 11 12 13 14],...
["C" "F" "cm" "in" "m" "ft" "km" "mi" "gm" "oz" "kg" "lb" "LT" "t"]);
ref_convUnit = dictionary(...
[1 2 3 4 5 6 7 8 9 10 11 12 13 14],...
["F" "C" "in" "cm" "ft" "m" "mi" "km" "oz" "gm" "lb" "kg" "t" "LT"])
disp('Welcome to my unit converter') % user interface begins
disp('Please use the following numbers to indicate which conversion you would like to use:')
disp('-----------------------------------------------')
disp('1 converts Celsius to Fahrenheit') % no
disp('2 converts Fahrenheit to Celsius') % no
disp('3 converts Centimetres to Inches') % yes
disp('4 converts Inches to Centimetres') % yes
disp('5 converts Metres to Feet') % yes
disp('6 converts Feet to Metres') % yes
disp('7 converts Kilometres to Miles') % yes
disp('8 converts Miles to Kilometres') % yes
disp('9 converts Grams to Ounces') % no
disp('10 converts Ounces to Grams') % no
disp('11 converts Kilograms to Pounds') % yes
disp('12 converts Pounds to Kilograms') % yes
disp('13 converts Tonnes (Metric) to Tons (Imperial)') % yes
disp('14 converts Tons (Imperial) to Tonnes (Metric)') % yes
disp('-------------------------------------------------')
ConvSelection = input('What conversion would you like to do? '); % select conversion from menu
while true % begin while loop - if input is incorrect the error for conv selection will be displayed and restarted
if ismember(ConvSelection, 1:14) == 1 % if the conversion selection is between 1-14
if isKey(ref, ConvSelection) == 1 % and if the conversion selection is a key within the dictionary ref
selection_input = input('Enter the number you would like to convert: '); % prompts user to input number
numout = myunitconv(selection_input, ref(ConvSelection)); % calls unit converter function to apply conversion...
% (selection_input is numin, ref(ConvSelection) references
% the conversion (i.e when ConvSelection = 1, 'cels2fahr'
% is applied
break; % breaks while loop following unit conversion
else % if
disp('Incorrect Input! Please try again.')
end
else % if ConvSelection is not a number between 1-14, reprompt user
ConvSelection = input('Incorrect Input! Please enter a number from 1 - 14: ');
end % end if statament
end % end while loop
disp([num2str(selection_input), ref_prevUnit(ConvSelection), 'converted is', num2str(numout), ref_convUnit(ConvSelection)]);

採用された回答

Steven Lord
Steven Lord 2024 年 4 月 25 日
What value is assigned to the output argument variable numout if the value in the input argument variable convtype doesn't match any of the values you're checking for with your strcmp calls? If the answer is "nothing", that would cause the error you received.
So for example, what should this function call return?
[numout, unit] = myunitconv(1, 'thisConversionTypeDoesNotExist')
By the way, you should probably use a switch / case construct or perhaps define conversion functions and call the appropriate function using feval. I'd also use the error function rather than just displaying the word Error.
  4 件のコメント
Steven Lord
Steven Lord 2024 年 4 月 29 日
To my knowledge the situation you have described shouldn't be possible though,
Why can't a user of your function call it with whatever they want for the second input? What prevents them from doing so?
You need to decide how much defensive code you need to write to guard against Bobby Tables.
Fletcher
Fletcher 2024 年 4 月 29 日
I understand now - my assumption is that the function is a separate file and the user would never access it, but that doesn't make sense as the user would need the function accessible for matlab to read and could edit it.
Thanks for your help. Great comic too.

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

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2024 年 4 月 29 日
ref = dictionary(... % creates dictionary called ref
[1 2 3 4 5 6 7 8 9 10 11 12 13 14],... % keys
["cels2far" "fahr2cel" "cm2in" "in2cm" "met2ft" "ft2met" "km2mi" "mi2km" "gm2oz" "oz2gm" "kg2lb" "lb2kg" "LT2t" "t2LT"]); % values
Notice the first of those is cels2far
if strcmp(convtype, 'cels2fahr') == 1;
Notice that is cels2fahr which is not the same as cels2far
  1 件のコメント
Fletcher
Fletcher 2024 年 4 月 29 日
編集済み: Fletcher 2024 年 4 月 29 日
Noted - silly errors that I should have seen.
Thanks for your help.

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

カテゴリ

Help Center および File ExchangeData Type Identification についてさらに検索

製品


リリース

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by