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.
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)]);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)]); 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)]); dictionary, user-defined function, matlab MATLAB Answers — New Questions