switch statement help

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

Jan
Jan 2012년 5월 4일
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.

댓글을 달려면 로그인하십시오.

답변 (4개)

Thomas
Thomas 2012년 5월 4일

0 개 추천

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

Nasir Qazi
Nasir Qazi 2012년 5월 4일
by input 3 component to nc , I have to assign the 3 components there respective Tc values in order of component selection , yours above edited code only give me one particular value.which I am not looking at
Walter Roberson
Walter Roberson 2012년 5월 4일
Then you asked the wrong question.
Thomas
Thomas 2012년 5월 4일
Check Edit..
Nasir Qazi
Nasir Qazi 2012년 5월 4일
exactly Thomas , The one you edit is closer to what I am expecting

댓글을 달려면 로그인하십시오.

Walter Roberson
Walter Roberson 2012년 5월 4일

0 개 추천

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
Nasir Qazi 2012년 5월 4일

0 개 추천

is that correct
clc
clear
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:');
switch c(i)
case 1
if c(i)==1;
Tc(i,:) = 190;
Pc(i,:) = 2;
end
case 2
if c(i)==2;
Tc(i,:) = 120;
Pc(i,:) = 4;
end
case 3
if c(i)==3;
Tc(i,:) = 890;
Pc(i,:) = 6;
end
case 4
if c(i)==4;
Tc(i,:) = 1210;
Pc(i,:) = 4;
end
case 5
if c(i)==5;
Tc(i,:) = 1990;
Pc(i,:) = 8;
end
otherwise
disp('enter the comp')
end
end

댓글 수: 7

Walter Roberson
Walter Roberson 2012년 5월 4일
Probably not. You would not reach case 1 unless c(i)==1 so testing that within the case is redundant.
Nasir Qazi
Nasir Qazi 2012년 5월 4일
but it runs perfect
Walter Roberson
Walter Roberson 2012년 5월 4일
Well if you are happy with it then that is what matters.
Nasir Qazi
Nasir Qazi 2012년 5월 4일
its not about me happy our not , I want it to run it perfectly that's why I need advice from experts like you .
Walter Roberson
Walter Roberson 2012년 5월 4일
You are the one who knows what it needs to do. You already said that it runs perfectly. If it solves your problem then style differences are not important at this point.
If you are asking me whether your code solves your problem, then I cannot answer because you have not clearly expressed what you are trying to do (an example with multiple components would help.) When _I_ look at your code, I see parts of it that look unlikely to be right to _me_, but that could just be because I do not understand what you are trying to do.
Nasir Qazi
Nasir Qazi 2012년 5월 4일
let me explain , I have multicomponent e.g nc = 7, and I already assign the value of Tc and Pc to all of them , and for example I want to use 4 component from above and only want to use there assign Tc and Pc values and leave the rest , so I need a code for this purpose , Hope you cleared now.
Walter Roberson
Walter Roberson 2012년 5월 4일
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
Andrei Bobrov 2012년 5월 4일

0 개 추천

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);

카테고리

도움말 센터File Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

태그

질문:

2012년 5월 4일

Community Treasure Hunt

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

Start Hunting!

Translated by