I'm trying to have a function receive input in 2 different ways.

10 次查看(过去 30 天)
Hi I have a function that determines if 2 lines intersect, are parallel or have infinitely many solutions. I'm able to prompt the user for inputs individually or have them enter the name of the function IPF(slope1,intercept1,slope2,intercept2). If I edit the code I can have it work both ways. However, my professor would like it if the user could decide how they would like to enter the function ( either on one line or being prompted by the function) I have used, if statements, while states, etc, however I have been unable to have a user choose how they want to enter the function without errors.
Here's the current code
function[output]= IPF(m1,b1,m2,b2)
prompt1='Enter your slope value for your first function.\n';
prompt2='Enter your y-intercept value for your first function.\n';
prompt3='Enter your slope value for your second function.\n';
prompt4='Enter your y-intercept value for your second function.\n';
if (isempty (m1))
m1=input(prompt1);
elseif (isempty(b1))
b1 = input(prompt2);
elseif (isempty(m2))
m2 = input(prompt3);
elseif (isempty(b2))
b2 = input(prompt4);
end
infinite = 'These lines have infinitely many solutions.\n';
parallel = 'These lines have no solutions and are parallel.\n';
intersect = 'These lines intersect.\n';
if(m1==m2)&&(b1==b2)
fprintf (infinite)
x=linspace(-10,10,21);
plot(x,m1*x+b1,x,m2*x+b2)
elseif(m1==m2)&&(b1~=b2)
fprintf (parallel)
x=linspace(-10,10,21);
plot(x,m1*x+b1,x,m2*x+b2)
elseif (m1~=m2)
fprintf (intersect)
x=linspace(-10,10,21);
syms xi yi
eqn1 = m1*xi + b1 == yi;
eqn2 = m2*xi +b2 == yi;
sol = solve([eqn1, eqn2], [xi, yi]);
xSol = sol.xi;
ySol = sol.yi;
fprintf('The lines intersect at %d , %d.\n',xSol,ySol);
plot(x,m1*x+b1,'r',x,m2*x+b2,'b--', xSol,ySol,'*g')
end
end
This code displays the results like this:
However, I would like the user to choose between entering the function like above or as shown in the picture below.
I can get each of these to work separately but I'm having no success getting both to work within the same function. I hope that I explained it clearly enough.
I would greatly appreciate any assistance or clarification on this issue.

采纳的回答

Deepak Gupta
Deepak Gupta 2021-10-27
You can use varargin as the input to your function definition i.e.
function[output]= IPF(varargin)
if(nargin ==0)
prompt1='Enter your slope value for your first function.\n';
prompt2='Enter your y-intercept value for your first function.\n';
prompt3='Enter your slope value for your second function.\n';
prompt4='Enter your y-intercept value for your second function.\n';
m1=input(prompt1);
m2=input(prompt2);
m3=input(prompt3);
m4=input(prompt4);
elseif(nargin ==4)
m1=varargin{1};
m2=varargin{2};
m3=varargin{3};
m4=varargin{4};
end
%Rest of your code
end
You can have conditions for cases when user inputs only 1 or 2 or 3 inputs as well.
  5 个评论
Stephen23
Stephen23 2021-10-28
" I tried this way and function works as expected but i wasn't able to see the tab completion for manually written function. Does it require a separate file to accomodate tab completion?"
No, the hinting/help is automatic, based on the function signature. First type the function name, then either:
  1. type the opening parenthesis, and then wait for a second or two, or
  2. type both opening and closing parentheses and press crtl+f1.
A small box will pop up with the input names and a link to the function help. When I tried it with your function it looks like this (here with individual named input arguments):
vs. when using VARARGIN for all of the input arguments:
I thought that this was also displayed during tab completion, but my memory was incorrect.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Particle & Nuclear Physics 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by