String Input Loop not working

9 visualizzazioni (ultimi 30 giorni)
Gwen Plato
Gwen Plato il 8 Ott 2018
Modificato: Stephen23 il 8 Ott 2018
I'm trying to get a function to recognize two string inputs and number and, using a loop, sort out how to manipulate the number based on what string inputs were entered but no matter how I try I can't get it:
function [Length, u] = border(ug,ud,number)
ug = input('In (m, cm, in, ft):');
ud = input('Out (m, cm, in, ft):');
number = input('number:');
x=['m','cm','in','ft'];
for k=1:length(x)
if (ug(k)=='m')&&(ud(k)=='cm')
L(k)=number.*100;
u(k)='cm'
elseif (ug(k)=='cm')&&(ud(k)=='m')
L(k)=number./100;
u(k)='m'
end
Length=L(k);
u=u(k);
fprintf('%.2f%s\n',Length(k),u(k))
end

Risposte (2)

KSSV
KSSV il 8 Ott 2018
Don't use == to check strings...use strcmp or strcmpi. Read about those functions.
  4 Commenti
Gwen Plato
Gwen Plato il 8 Ott 2018
function [Length, u] = border(ug,ud,number)
ug = input('In (m, cm, in, ft):');
ud = input('Out (m, cm, in, ft):');
number = input('number:');
x={'m','cm','in','ft'};
r=strcmp(x,ug);
w=strcmp(x,ud);
for k=1:length(number)
if (r(1,1)==1)&&(w(1,2)==1)
L(k)=number.*100;
u(k)='cm'
elseif (r(1,2)==1)&&(w(1,1)==1)
L(k)=number./100;
u(k)='m'
end
Length=L(k);
u=u(k);
fprintf('%.2f%s\n',Length(k),u(k))
end
KSSV
KSSV il 8 Ott 2018
What are your inputs?

Accedi per commentare.


Stephen23
Stephen23 il 8 Ott 2018
Modificato: Stephen23 il 8 Ott 2018
Your code has several bugs. In particular:
  • Your use of input evaluates the input string. MATLAB will assume that the input is a variable name or a function name (e.g. m, etc) and try to evaluate this... and then throw an error when this variable/function cannot be found. If you really want to use input then use it with its optional 's' argument. I recommend to avoid using input.
  • using == to test for equivalence of character vectors: the == operator is a element-wise operator, which is not useful for what you are trying to do. As KSSV wrote, you should use strcmp or strcmpi.
  • using [] to define x. In MATLAB [] are a concatenation operator (NOT a "list" operator), so your code x=['m','cm','in','ft'] is exactly equivalent to this: x='mcminft', which is unlikely to be very useful for you.
  • You ignore all of the input arguments by reallocating all of those variables on the first lines of your function.
  • You try to put a multiple-element char array into one element of a char array: u(k)='cm'. Each element of a char array contains one character, and one character only. It is not possible to force multiple characters into one element of a char array, like you are trying to do. One element, one character.
  • Naming a variable Length, which only differs by case from the inbuilt function length, is not recommended.
  • Using length is not recommended because which dimension is measures changes depending on the size of the input array. Use numel or size instead.
  • The variable Length is defined inside the loop with Length=L(k), so you simply overwrite its value on every iteration, leaving only the last iteration's value when the function finishes. This means it bears no correlation to what the user has requested.
Here is a simple version of your function, using meter as the intermediate unit (most conversion functions convert to SI units as the intermediate unit):
function val = border(val,unitIn,unitOut)
switch unitIn
case 'm'
% do nothing
case 'cm'
val = val/100;
case 'mm'
val = val/1000;
case 'in'
val = val*0.0254;
case 'ft'
val = val*0.3048;
otherwise
error('Unknown input units.')
end
switch unitOut
case 'm'
% do nothing
case 'cm'
val = val*100;
case 'mm'
val = val*1000;
case 'in'
val = val/0.0254;
case 'ft'
val = val/0.3048;
otherwise
error('Unknown output units.')
end
end
And tested:
>> border(1,'in','cm')
ans = 2.5400
>> border(1,'ft','in')
ans = 12.0000
>> border(1,'m','cm')
ans = 100
Once you have a working version you can think about simplifications, e.g. using a loop or indexing.
You might also be interested in simply downloading an existing function that performs this conversion:

Categorie

Scopri di più su Characters and Strings in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by