How do I use a string in my input?

1 visualización (últimos 30 días)
Matthew Orsie
Matthew Orsie el 3 de Mzo. de 2018
Respondida: Matthew Orsie el 6 de Mzo. de 2018
I have a homework question that asks:
Write a function called my_wish that takes input arguments in string format such as "square, double, cubic, or half" and second input in a double format of any number such as 4. Use switch statement for this function according to:
>>my_wish('cubic',4) The cubic of 4 is 64
I've been trying all day and I can't figure it out. I know how it could be done but I think I just don't know how to call on the proper variables.
function [] = my_wish(square,double)
x = double.^square;
square = 2;
switch x
case 'square', 2;
disp('The square of d% is d%')
end

Respuesta aceptada

Akira Agata
Akira Agata el 3 de Mzo. de 2018
Like this?
function my_wish(str,val)
switch str
case 'square'
result = sprintf('The square of %d is %d',val,val^2);
case 'cubic'
result = sprintf('The square of %d is %d',val,val^3);
end
disp(result)
end
And here is the result:
>> my_wish('square',2)
The square of 2 is 4
>> my_wish('cubic',4)
The square of 4 is 64

Más respuestas (1)

Matthew Orsie
Matthew Orsie el 6 de Mzo. de 2018
That's perfect! Thank you so much!

Categorías

Más información sobre Resizing and Reshaping Matrices en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by