필터 지우기
필터 지우기

Problem over switch function

조회 수: 3 (최근 30일)
Joel
Joel 2014년 9월 22일
댓글: Steven Lord 2020년 1월 9일
So this is what I have so far.
and here is the problem:
I've made the switches, and when I go to HELP SWITCH, it shows me how to do the switch, but I don't really understand how to integrate it within the problem. I was trying to figure out if there was a way to input the element needed, and then it would add the values of a and b, and then I could execute the equation, but I can't really figure out how to go about doing that (if I'm right).
  댓글 수: 1
Joel
Joel 2014년 9월 22일
So this is what I have so far now. I'm trying to use an input function so you can choose what element you want, and apparently I'm doing something wrong... or maybe I'm using an input function in the wrong way, but every time I type one of my elements it says it doesn't have a value.
T = 300 ; V = 20; R = .08206;
myelement = input('My element: ');
switch (myelement)
case 'he'
a= .0341 ; b = .0237;
case 'hydrogheen'
a = .244 ; b = .0266;
case 'oxygen'
a = 1.36 ; b = .0318;
case 'chlorine'
a = 6.49 ; b = .0562;
case 'carbondioxide'
a = 3.59 ; b = .0427;
end
P = (R*T)/ (V - b) - a / V.^2;

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

채택된 답변

Adam
Adam 2014년 9월 22일
편집: Adam 2014년 9월 22일
Your code works, but you have to input the element as:
'he'
(with the quotation marks), not just
he
which Matlab tries to evaluate as a function or variable rather than a string.
If you want to get fancy you can also use
validatestring
to ensure a sensible input string (this also allows you to input, for example just 'o' if one of your defined valid strings is 'oxygen' and it will convert 'o' to 'oxygen' on output, though, of course, just 'h' would be ambiguous.
By the way, your spelling of 'Hydrogen' is a little off too which may cause a failure if the user types it in correctly at the command line.

추가 답변 (2개)

Guillaume
Guillaume 2014년 9월 22일
Please don't post images of code when you could just copy and paste the code.
Your switch syntax is correct. However, for it to be useful, you need to put it before the equation not after.
If you'd copy-pasted the code, I would have copied it below and rearranged it for you. Can't do that with a picture.
  댓글 수: 1
Joel
Joel 2014년 9월 22일
So this is what I have so far now. I'm trying to use an input function so you can choose what element you want, and apparently I'm doing something wrong... or maybe I'm using an input function in the wrong way, but every time I type one of my elements it says it doesn't have a value.
T = 300 ; V = 20; R = .08206;
myelement = input('My element: ');
switch (myelement)
case 'he'
a= .0341 ; b = .0237;
case 'hydrogheen'
a = .244 ; b = .0266;
case 'oxygen'
a = 1.36 ; b = .0318;
case 'chlorine'
a = 6.49 ; b = .0562;
case 'carbondioxide'
a = 3.59 ; b = .0427;
end
P = (R*T)/ (V - b) - a / V.^2;

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


Julia
Julia 2014년 9월 22일
편집: Julia 2014년 9월 22일
Hi,
you have to write a function:
function P = VanDerWaals(T,V,e)
% switch-case
% computation of P
Since R is the same for every gas you can enter the value directly in the equation.
  댓글 수: 2
Shammi Doly
Shammi Doly 2020년 1월 9일
hi,
I am trying to read data from the RADAR sensor through the USB port. I used switch function to read the real time data. But I am getting some error like this.'Not enough input arguments.Error in f_serial (line 2).
function p = f_serial(p,str)
switch(str)
case 'clear'
a = instrfindall();
if(isempty(a)==0)
fclose(a);
delete(a);
end
case 'openBGT'
try
p.sr = serial(p.serialPortBGT,'BAUD',9600);
set(p.sr,'Timeout',0.5);
set(p.sr,'InputBufferSize',8192);
fopen(p.sr);
catch
disp('Error while opening USB Connection. Check the connection or the portname');
return
end
case 'closeBGT'
fclose(p.sr);
delete(p.sr);
end
Steven Lord
Steven Lord 2020년 1월 9일
That suggests you called f_serial with only one input. If you did, when MATLAB tries to evaluate the switch expression str is undefined. MATLAB can see that str was defined to be an input argument to your function and so gives you a (hopefully) more informative error, that you called your function with fewer inputs than your function requires.
Call your function with the two inputs that it requires.

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

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by