switch statement help
    6 visualizaciones (últimos 30 días)
  
       Mostrar comentarios más antiguos
    
 disp('1 for CH4');disp('2 for CO2');disp('3 for N2');
nc =input('components');
if nc > 5
    disp('Too Many component')
    break
end
for i=1:nc
    c(i)=input('c:','s');
    switch c(i)
        case {'c(i)=1'}
            Tc(1,1) = 190;
        case {'c(i)=2'}
            Tc(1,2) = 120;
        case {'c(i)=3'}
            Tc(1,3) = 890;
        case {'c(i)=4'}
            Tc(1,4) = 1210;
        case {'c(i)=5'}
            Tc(1,5) = 1990;
        otherwise
            disp('enter the comp')
    end
end
 % The statement is not working kindly help
1 comentario
  Jan
      
      
 el 4 de Mayo de 2012
				Please do not state only, that this "is not working", but describe the occurring problems with any details. It is much easier to solve a problem than to guess, what the problem is.
Respuestas (4)
  Thomas
      
 el 4 de Mayo de 2012
        try
clear variables
disp('1 for CH4');disp('2 for CO2');disp('3 for N2');
nc =input('components: ');
if nc > 5
    disp('Too Many component')
    break
end
    switch nc
        case 1
            Tc(1,1) = 190
        case 2
            Tc(1,2) = 120
        case 3
            Tc(1,3) = 890
        case 4
            Tc(1,4) = 1210
        case 5
            Tc(1,5) = 1990
        otherwise
            disp('enter the comp')
    end
EDIT This might be what you want:
clear variables
disp('1 for CH4');disp('2 for CO2');disp('3 for N2');
nc =input('components: ');
if nc > 5
    disp('Too Many component')
    break
end
for ii=1:nc
     c{ii}=input('c:','s')
    switch c{ii}
        case '1'
            Tc(1,1) = 190
        case '2'
            Tc(1,2) = 120
        case '3'
            Tc(1,3) = 890
        case '4'
            Tc(1,4) = 1210
        case '5'
            Tc(1,5) = 1990
        otherwise
            disp('enter the comp')
    end
end
4 comentarios
  Walter Roberson
      
      
 el 4 de Mayo de 2012
        If the user really were entering in the literal string 'c(i)=1' (6 characters) in response to that input() prompt, then those 6 characters would not fit into the c(i) that the input() string is being assigned to. Please keep in mind that a string in MATLAB is an array of characters, not a distinct data type, so multiple array locations need to be allocated to hold a multi-character string.
Expecting the user to know to type in literally
c(i)=4
in response to the "c:" prompt is expecting too much from the user.
  Nasir Qazi
 el 4 de Mayo de 2012
        7 comentarios
  Walter Roberson
      
      
 el 4 de Mayo de 2012
				Which 4 would you want to assign to in that case? The first 4? Would the same value be assigned to Tc for each of the 4? In your array Tc(i,:) which one is the component number, and what does the other dimension represent?
  Andrei Bobrov
      
      
 el 4 de Mayo de 2012
        in your case
eg:
c = randi([1 5],10,1);
pt = [[190;120;890;1210;1990],[2;4;6;4;8]];
Tc = pt(c,1);
Pc = pt(c,2);
0 comentarios
Ver también
Categorías
				Más información sobre Get Started with MATLAB 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!



