Problem over switch function

3 visualizaciones (últimos 30 días)
Joel
Joel el 22 de Sept. de 2014
Comentada: Steven Lord el 9 de En. de 2020
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 comentario
Joel
Joel el 22 de Sept. de 2014
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;

Iniciar sesión para comentar.

Respuesta aceptada

Adam
Adam el 22 de Sept. de 2014
Editada: Adam el 22 de Sept. de 2014
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.

Más respuestas (2)

Guillaume
Guillaume el 22 de Sept. de 2014
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 comentario
Joel
Joel el 22 de Sept. de 2014
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;

Iniciar sesión para comentar.


Julia
Julia el 22 de Sept. de 2014
Editada: Julia el 22 de Sept. de 2014
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 comentarios
Shammi Doly
Shammi Doly el 9 de En. de 2020
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 el 9 de En. de 2020
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.

Iniciar sesión para comentar.

Categorías

Más información sobre Logical 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