Question:
MATLAB how to make function accept input type 'char' (as in words)?
anonymous
2012-04-16 08:52:07 UTC
Working with MATLAB trying to create a function called "Nation". The idea is to execute the function with a nation as the input; ex:Nation('Bulgaria') then have the inputted word broken down into the letter numerical values

function result=Nation('strA')

values=double('strA')


however, when i try it out i get the error code:

-----" Undefined function or method 'Nation' for input arguments of type 'char'"

i assume this means my coding is incorrect in that my function hasnt been told how to accept words/characters as input?

***How do you create a function so that it will accept a character string for input?***


thank you
Four answers:
wiseguy
2012-04-20 06:47:53 UTC
Dear Jonathan,



You create a function that will accept character strings for input the same as you do for numeric inputs. The problem you have appears to be that you are trying to define your function with its formal argument in quotes. You should not quote the formal argument there, but simply give it any allowable variable name. That is, for the first line of your function definition you appear to have written



function result = Nation('strA')



but instead should merely have something like



function result = Nation(strA)



where "strA" (without quotes) is the formal argument of the function. Also, make sure you save your function in a file called "Nation.m" so that MATLAB will find it.



When you actually call the function, only then would it be acceptable to have a character string as the actual argument, such as



Nation('Bulgaria')



or



Nation(country_name)



(where "country_name" is some string variable you would have defined before using it here).
?
2016-09-28 18:46:17 UTC
Matlab Char
chipmania
2012-04-19 04:21:15 UTC
qq = input('Give me a character', 's') %basically put it in a string
anonymous
2016-02-20 06:49:59 UTC
think you need 1./(2*x) or even ones(size(x))/2./x


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...