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

Click For Summary
SUMMARY

This discussion focuses on how to prompt users for two input variables in MATLAB for a linear regression assignment. The recommended approach utilizes the inputdlg function to create a dialog box for user input, allowing for structured data entry. Additionally, the input function can be employed for text input, with the 's' argument to capture user responses as strings. Error handling is advised using a TRY... CATCH... END block to manage potential input syntax errors.

PREREQUISITES
  • Familiarity with MATLAB programming language
  • Understanding of linear regression concepts
  • Knowledge of MATLAB's inputdlg function
  • Basic error handling in MATLAB using TRY... CATCH
NEXT STEPS
  • Explore MATLAB's inputdlg documentation for advanced usage
  • Learn about MATLAB's data types and how to handle user input
  • Research error handling techniques in MATLAB
  • Study linear regression implementation in MATLAB
USEFUL FOR

This discussion is beneficial for students and educators in data science, MATLAB programmers, and anyone involved in statistical analysis using MATLAB.

sflesock
Messages
1
Reaction score
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
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:
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.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
26K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 5 ·
Replies
5
Views
12K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 3 ·
Replies
3
Views
8K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 5 ·
Replies
5
Views
6K
Replies
1
Views
14K