Keep some inputs constant whilst requesting others.

2 views (last 30 days)
I am creating a code which requires four inputs of data. To do this I am using the input function. However for two of the inputs will only require inputting once, the other two will require changing each time.
ie.
x1=input('what is the value of x1, the location of the first microphone?');
x2=input('what is the value of x2, the location of the second microphone?');
p1=input('what is the value of p1, the pressure at the second microphone?')
p2=input('what is the value of p2, the pressure at the second microphone?');
x1 and x2 remain constant for all of the experiments however p1 and p2 will change each time I run the code. Is there a way of the code asking if x1&x2 have changed and if they haven't it will just continue to the input of p1&p2?
Thanks in advance.

Answers (2)

Matt J
Matt J on 9 Mar 2017
Edited: Matt J on 9 Mar 2017
you could do something like this
data=input('Input the data values in syntax [x1,x2,p1,p2] or [p1,p2]');
switch numel(data)
case 2
p1=data(1); p2=data(2);
case 4
x1=data(1); x2=data(2); p1=data(3); p2=data(4);
end

Matt J
Matt J on 9 Mar 2017
Edited: Matt J on 9 Mar 2017
Is there a way of the code asking if x1&x2 have changed
Yes, just do something like this
if strcmpi( input('Update x1,x2?') , 'YES')
x1=input('what is the value of x1, the location of the first microphone?');
x2=input('what is the value of x2, the location of the second microphone?');
else
disp 'Using Previous values'
end
But it saves the user very little in terms of manual input steps. Instead of inputting new x1,x2 values, the user now has to input 'yes' or 'no'...
If x1,x2 will only be entered once, maybe it doesn't make sense to get them from keyboard input. Maybe your main function should look like this
function MAIN(x1,x2,numExperiments)
for i=1:numExperiments
p1=input('what is the value of p1, the pressure at the second microphone?')
p2=input('what is the value of p2, the pressure at the second microphone?');
....
end
end

Categories

Find more on Entering Commands in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!