简体   繁体   中英

How to reuse parts of code? (MATLAB)

Recently I wrote a basic script that uses the ideal gas law. I managed to get the script working fine but it seems very clunky and inelegant, I was wondering if there is a better way of writing it. The program needs 2 inputs from the user, here's the part of the code that gets the input.

x = input('Are you going to enter a [t]emperaute, a [p]ressure or a [v]olume?\n','s');

while(double(x) ~= double('t') && double(x) ~= double('p') && double(x) ~= double('v'))
% stop the user from entering anything but t,v or p
x = input('Please enter either t (for temperaure), p (for pressure) or v (for volume)\n', 's');
end  

if (double(x) == double('t')) 
T = input('OK, please tell me the temperature in Kelvin\n');
% Get the temperature value from the user
while (T <= 0) % negative temperatures aren't allowed
  T = input('Please enter a positive value for the temperature\n');
end
elseif (double(x) == double('p'))
P = input('OK, please tell me the pressure in Pascals\n'); 
% Get the pressure value from the user
while (P <= 0) % negative presures arent allowed
  P = input('Please enter a positive value for the pressure\n');
end
elseif(double(x) == double('v'))
V = input('OK, please tell me the volume in m^3\n');
% Get the volume value from the user 
 while (V <= 0) % Don't allow the user to enter negative or zero values for the volume
  V = input('Please enter a positive value for the volume\n');
 end      
end  

Already I think this looks a bit daft, the if statements look very similar. When I was writing this I was just copy pasting the conditions and changing a few words.

It gets even worse later on in the script when I have to get another variable from the user, I just paste the exact same code, change the variable to y and stick a while (double(x) == double(y)) condition on the end to stop the user putting in the same input twice.

Is there a way to reuse parts of code in MATLAB? I tried to define a function for the big block of code up above but MATLAB kept complaining because I wasn't specifying any variables to pass to the function.

I'm sure that there will be a basic programming thing that does this but i'm new to programming (in MATLAB and in general).

Thank you in advance.

I think that this can be quite easily done:

http://www.mathworks.co.uk/help/matlab/matlab_prog/local-functions.html

All you need to do is create a local function as seen in the link. All you're doing after all is giving a prompt and checking the value received is below 0.

After that, compare the first input to a collection to give you the correct value of the prompt using the map containers:

http://www.mathworks.co.uk/help/matlab/map-containers.html?s_tid=doc_12b

So you might use something like:

function X = foo(x)

keySet =   {'t', 'p', 'v'};
valueSet = ['Kelvin', 'Pascal', 'm^3'];
units = containers.Map(keySet,valueSet)
X = input('OK, please tell me the temperature in'+ units(x) +'\n');
% Get the value from the user
while (X <= 0)
X = input('Please enter a positive value for the value \n');

end

Then you just need to call the function:

Temperature = foo('t')

Have a look around the links I've posted. They offer a good place to start.

PS I wouldn't trust those input prompts, I can't remember how the strings work in Matlab, but the gist should be right.

PPS You'll also maybe want to add a check that a good value has been passed into the function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM