switch statements and trig function
    7 visualizaciones (últimos 30 días)
  
       Mostrar comentarios más antiguos
    
val = str2double(input('enter number','s')); 
chr = input('enter letter','s'); 
switch chr 
    case 's'
        disp(sin(val)) 
    case 'c' 
        disp(cos(val)); 
    case 't' 
        disp(tan(val));
    otherwise 
        disp('Oh no! Try a different character.'); 
end 
I wrote this code to find the sin,cos,or tan of an entered individual number. I now need to modify it so that instead of reading a single character/number, the program continually reads character/number pairs and displays the trigonometry result. If a number outside the range [-100, 100] is entered, the program should not display the trigonometry result and should ask for another character/number pair to be entered. I can't figure out how to make the initial modifications of reading in pairs - any shortcuts I can use?
2 comentarios
Respuestas (2)
  Kevin Chng
      
 el 24 de En. de 2019
        I think the most simple approach is to add another loop which is
val = str2double(input('enter number','s')); 
chr = input('enter letter','s'); 
if val>=-100 && val<=100
    switch chr 
        case 's'
            disp(sin(val)) 
        case 'c' 
            disp(cos(val)); 
        case 't' 
            disp(tan(val));
        otherwise 
            disp('Oh no! Try a different character.'); 
    end 
else
    warning('enter new input, your input is out of range')
end
2 comentarios
  Kevin Chng
      
 el 24 de En. de 2019
				I might miss understanding your question.
you may consider to use inputdlg, https://www.mathworks.com/help/matlab/ref/inputdlg.html
However, it is still input them in separate line, if wants to input in the same line, later, you might need to separate them since one is string/char and the other one is numeric
clc;clear all;
chr = input('enter letter & number :','s'); 
% Need some logic to separate them and ensure the user key in the right thing
C = {'1','2','3','4','5','6','7','8','9','0'};
for i = 1:10
Index = strfind(chr, C{i});
if isempty(Index)~=1
    if exist('thefirstdigit')==0
        thefirstdigit=Index;
    else    
        if thefirstdigit>Index
        thefirstdigit=Index;
        end
    end
end
end
%indexing to chr, then separate them into val and chr2
val = str2double(chr(thefirstdigit:end)); 
chr2 = (chr(1:thefirstdigit-1)); 
if val>=-100 && val<=100
    switch chr2 
        case 's'
            disp(sin(val)) 
        case 'c' 
            disp(cos(val)); 
        case 't' 
            disp(tan(val));
        otherwise 
            disp('Oh no! Try a different character.'); 
    end 
else
Run the program and enter :s100
  Kevin Phung
      
 el 24 de En. de 2019
        
      Editada: Kevin Phung
      
 el 24 de En. de 2019
  
      you can just run the input once, and evaluate the input as a whole.
word = input('enter letter followed by number','s'); 
if numel(word)>1 
    chr = word(1);
    val =str2num(word(2:end))
    if isempty(val) || val >100 || val < -100 %occurs when 2nd character is a letter and not a number
        disp('Try again')
        return
    end
    switch chr 
        case 's'
            disp(sin(val)) 
        case 'c' 
            disp(cos(val)); 
        case 't' 
            disp(tan(val));
        otherwise 
            disp('Oh no! Try a different character.'); 
    end 
else
    disp('Try  again')
end
0 comentarios
Ver también
Categorías
				Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


