whenver i insert this if statement inside a for loop, i get an error. Could someone please explain why? Much appreciated!
Afficher commentaires plus anciens
if class(input(i))=='double'
in this case, i want to check if the value of input(i) in the string 'input' is a number
Moved and formatted from an answer by champions2015 about 6 hours ago (by per isakson)
Some things I now understand where I was going wrong; as suggested I changed the variable name, which explains why I was getting one of the errors. I am now trying to use the 'isdouble' as Cam mentioned, but seem to be getting the following error:
Error in dial (line 6)
if isdouble(message{i})
My entire function is as follows:
function output=dial(message)
letters={'ABC', 'DEF', 'GHI', 'JKL', 'MNO', 'PQRS', 'TUV', 'WXYZ'};
number=[];
for i=1:length(message)
if isdouble(message(i))
number(i)=message(i);
else
for j=1:length(letters)
if ~isempty(strfind(letters{j},message(i))) %this function creates an array
number(i)=j+1;
end
end
end
end
output=uint64(number);
end
The actual question I'm trying to answer is as follows:
Each number on telephone keypads, except 0 and 1, corresponds to a set of uppercase letters as shown in this list: 2 ABC, 3 DEF, 4 GHI, 5 JKL, 6 MNO, 7 PQRS, 8 TUV, 9 WXYZ Hence, a phone-number specification can include uppercase letters and digits. Write a function called dial that takes as its input argument a char vector of length 16 or less that includes only these characters and returns as its output argument the telephone number as a uint64. Here is the input and output for one example of a call of the function: Input: '1FUNDOG4YOU' Output: 13863644968 You can assume that a phone number never starts with 0. If the input contains any illegal characters, the function returns 0. You are not allowed to use the built-in function strrep.
Thanks again in advance!
2 commentaires
Maybe because of this:
But without any code, context, or error message you are making it very hard for us to help you.
Rather than solving this with lots of ugly loops and ifs, see also:
Réponse acceptée
Plus de réponses (2)
Walter Roberson
le 3 Sep 2017
0 votes
Use strcmp() to compare character vectors
Cam Salzberger
le 3 Sep 2017
Modifié(e) : Cam Salzberger
le 3 Sep 2017
As Stephen is probably indicating, the input argument to the input function needs to be a character vector. This is likely to be the error message you are getting:
Error using input
The first argument to INPUT must be a character vector.
Instead, I'd recommend getting the input, storing it to a variable, and then check the value. Otherwise you'll have no way to access the value later. Something like this:
inVal = input('Give me a number: ');
if ...check condition here...
...
end
Now, there are a few other issues here. Firstly, I would not recommend using "==" to compare two character vectors. You may get an error if they're not the same length, and you'll get a vector output otherwise. What you probably want to use is strcmp instead.
Secondly, you don't need to compare the output of "class", you can just use the isdouble function. This will be more efficient.
inVal = input('Give me a number: ');
if isdouble(inVal)
...
end
If you are trying to do "input(i)" because "input" is an array that you are trying to access into... don't. Don't overload built-in functions or constants with variable names. Pick a different variable to use.
Hope this helps.
-Cam
2 commentaires
Even more reliably, and avoiding security issues entirely:
num = str2double(input(...,'s'))
Steven Lord
le 5 Sep 2017
Note that isdouble is part of the Fixed-Point Designer product. For something similar that's in MATLAB itself, use isa(x, 'double').
Catégories
En savoir plus sur MATLAB dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!