MATLAB Help -- how to ask user for 2 input variables

In summary, to create a program that prompts the user for a number of data points and then graphs a linear regression, you can use the inputdlg function in a loop to ask for the values of x and y. Alternatively, you can use the INPUT function with the 's' argument to prompt for text input and then parse the input to extract the x and y values.
  • #1
sflesock
1
0
I have an assignment where we have to ask the user for a number of data points which then we will graph a linear regression.


Enter the number of input data points: 7
Enter [x y] pair for point 1: [1.1 1.01]
Enter [x y] pair for point 2: [2.2 2.30]
Enter [x y] pair for point 3: [3.3 3.05]
Enter [x y] pair for point 4: [4.4 4.28]
Enter [x y] pair for point 5: [5.5 5.75]
Enter [x y] pair for point 6: [6.6 6.48]
Enter [x y] pair for point 7: [7.7 7.84]


This is what the program should look like as we are asking the user for the inputs. (The brackets are what the user inputs). How should the code look to get 2 separate variables? Any help would be greatly appreciated!
 
Physics news on Phys.org
  • #2
Matlab provides you with the inputdlg function for these purposes. Here is the documentation:
https://in.mathworks.com/help/matlab/ref/inputdlg.htmlAt the prompt, you will get a dialog box asking you for the various inputs. Put your code into a loop, and for each set, ask for the values of x and y using the above function.
Matlab:
prompt = {'Enter x:', 'Enter y:'};
dlgtitle1 = 'Enter values for point ';
for i=1:7
    dlgtitle = [dlgtitle1, num2str(i)];
    answer = inputdlg(prompt, dlgtitle);
    %extract x and y from answer
end
The answer matrix will have the two input values of x and y. Just extract them into your matrix for x and y.
 
Last edited:
  • #3
If you're looking to prompt for text input as in your example, you can use the INPUT function with the 's' argument, you can add parsing code yourself. The 's' option captures the input as a string, so it can contain anything. Like so:

Code:
question = ['Enter [x y] pair for point ', num2str(k), ' '];  % I'm assuming k is the point number.
userinput = input(question, 's');
xy = str2num(userinput);

That will work if the user properly formats their input as two numbers separated by a space or comma, but might throw an error if their input syntax is incorrect. So you should probably embed it in a TRY... CATCH... END block.
 

1. How do I ask the user for 2 input variables in MATLAB?

To ask the user for 2 input variables in MATLAB, you can use the input() function. This function takes in a prompt message as the first argument and returns the user's input as a string. You can then use the str2double() function to convert the string to a numerical value if needed.

2. Can I ask for multiple inputs in one line?

Yes, you can ask for multiple inputs in one line by separating the prompts with a comma inside the input() function. For example, input('Enter value 1, value 2:') will prompt the user to enter two values separated by a comma.

3. How do I assign the user's input to variables?

You can assign the user's input to variables by using the input() function and assigning it to a variable. For example, var1 = input('Enter value 1:') will store the user's input for value 1 in the variable var1.

4. What if the user enters a non-numerical input?

If the user enters a non-numerical input, you can use conditional statements to handle the error. For example, you can use the isnumeric() function to check if the input is a numerical value and use an if statement to prompt the user to re-enter the input if it is not.

5. Can I set a default value for the input?

Yes, you can set a default value for the input by adding it as the second argument in the input() function. For example, var1 = input('Enter value 1:', 5) will set the default value for var1 to 5 if the user does not enter any input.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
25K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
11K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
8K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
5K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
13K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
Back
Top