Setting variable names inside function based on input arguement in MATLAB

조회 수: 4 (최근 30일)
I have code that looks like this:
str = '';
x = NaN;
while isnan(x)
x = str2double(input([str, 'Enter a value for x: '], 's'));
str = 'Please enter a number. ';
end
str = '';
y = NaN;
while isnan(y)
y = str2double(input([str, 'Enter a value for y: '], 's'));
str = 'Please enter a number. ';
end
The two blocks are identical in use and the only difference is that one uses x as the variable name whilst the other uses y as the variable name.
I was wondering if there was a way to create a function which is able to take in the variable name as its input arguement and be able to set the variable names inside that function as well as what is displayed in terms of the input arguement.
Something like:
x = testNum(x); % Error using testNum; Too many output arguments.
y = testNum(y);
z = x + y;
fprintf("Sum is %d", z)
function testNum(~)
str = '';
val = NaN;
while isnan(val)
val = str2double(input([str, 'Enter a value for %s: '], 's')); % Not sure how to get variable name to appear in this format
str = 'Please enter a number. ';
end
end
A few problems I have with this is that there are too many output arguements for when I'm initialising x and y. Another thing is that I'm not sure on the formatting of text when using an array for input.
Essentially, I would want it so that one function is able to take in the variable names that it is initialised with so that if I had multiple input lines, I would be able to have one function to check all inputs. Alternatively it would probably be preferable for the input line to be outside the function to account for any lines that might want specific text to be displayed rather than generic text.

채택된 답변

David Hill
David Hill 2020년 4월 2일
편집: David Hill 2020년 4월 2일
function ans=testNum(z)
str = '';
NaN;
while isnan(ans)
input([str, 'Enter a value for ',z,': ']);
str = 'Please enter a number. ';
end
end
Then just execute the function.
x=testNum('x');
And x will equal whatever number you enter.
  댓글 수: 2
Richard Cheung
Richard Cheung 2020년 4월 2일
편집: Richard Cheung 2020년 4월 2일
Your code works but with the line:
input([str, 'Enter a value for ',z,': ']);
if a non-numerical character is input, matlab will display an error:
Enter a value for x: a
% Error using input
% Unrecognized function or variable 'a'.
%
% Error in program>testNum (line 28)
% input([str, 'Enter a value for ',z,': ']);
%
% Error in program (line 18)
% x=testNum('x');
Enter a value for x:
The program still runs properly albeit it displays the error each time a wrong character is input.
Using str2double will let it run flawlessly though.
str2double(input([str, 'Enter a value for ',z,': ']);
Soon realising that z in the function corresponds to the string set as the input arguement for the function, I realised I could remove the 'Enter a value for ' and just leave it as a blank string and set a custom string as the input arguement. I ended up with something like this
h=testNum('Enter your height (m)');
w=testNum('Enter your weight (kg)');
bmi = w/(h^2)
fprintf('Your BMI is ' + bmi);
function ans=testNum(z)
str = '';
NaN;
while isnan(ans)
str2double(input([str, '',z,': '],'s'));
str = 'Please enter a number. ';
end
end
Richard Cheung
Richard Cheung 2020년 4월 2일
I also realised -- (I must have forgotten) -- that in matlab, it forces you to enter a value that it wants to take in, unlike some languages like java where it would just terminate the program.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Text Data Preparation에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by