Solving Input Issue in MATLAB Code

In summary, the conversation discussed an issue with a code that does not wait for input and skips to the end. It was suggested to initialize the input to a positive value and to use a valid character for the printf argument. The conversation also mentioned a potential bug with the input() function in FreeMat. Finally, a solution was provided to fix the code and get it running correctly.
  • #1
geft
148
0
I have the following code which does not wait for input and skips right to the end. Is there something I miss?

Code:
no = 0;
total = 0;

while ans>=0
    ans = input('Input a number (negative to quit): ');
    no = no+1;
    total = total + ans;
end;

printf('\n');
disp('The input average is ');
total/no
 
Physics news on Phys.org
  • #2
1) You need to initialize ans to a positive value to start with or your while statement will never be satisfied. You can subtract that value off later so it doesn't affect your count. 2) Your printf argument is not a valid character. I'm not sure what you had in mind there.
3) You'll get a cleaner output with the format in the last line below


no = 0;
total = 0;
ans = 1;

while ans>=0
ans = input('Input a number (negative to quit): ');
no = no+1;
total = total + ans;
end;
total = total - 1;

disp(['The input average is ',num2str(total/no)])
 
  • #3
Thanks! I believe the input problem was because I was running the code on FreeMat. I tried running my original code on MATLAB and it worked. For some reason I can't get input() to work on FreeMat. EDIT: It's an existing bug.
 
Last edited:

1. How do I identify input issues in my MATLAB code?

To identify input issues in your MATLAB code, you can use the "dbstop if error" command to pause the code when an error occurs. This will allow you to examine the variables and inputs at the point of the error and determine the source of the issue.

2. What is the most common input issue in MATLAB code?

The most common input issue in MATLAB code is incorrect data type or size. This can happen when the code expects a specific type or size of input, but receives something different. For example, if a function expects a matrix as an input, but receives a vector, this could cause an input issue.

3. How can I prevent input issues in my MATLAB code?

To prevent input issues in your MATLAB code, it is important to carefully define and document the inputs of your functions. This includes specifying the data type and size of each input, as well as any restrictions or assumptions. It can also be helpful to validate inputs within the code to ensure they meet the expected criteria before using them.

4. How can I troubleshoot input issues in my MATLAB code?

If you encounter an input issue in your MATLAB code, the first step is to carefully examine the error message to determine the source of the issue. You can then use the "dbstop if error" command to pause the code and inspect the inputs at the point of the error. If necessary, you may need to adjust your code or inputs to resolve the issue.

5. Are there any tools or resources available to help with solving input issues in MATLAB code?

Yes, MATLAB offers several tools and resources to help with solving input issues in code. The "dbstop if error" command can be useful for pausing the code and examining inputs, and the "debugging" feature can help you step through your code and track the values of variables. Additionally, the MATLAB documentation and online forums can provide helpful tips and solutions for common input issues.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
9
Views
996
  • Engineering and Comp Sci Homework Help
Replies
3
Views
735
  • Engineering and Comp Sci Homework Help
Replies
19
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
899
  • Engineering and Comp Sci Homework Help
Replies
2
Views
989
  • Engineering and Comp Sci Homework Help
Replies
10
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
742
  • Engineering and Comp Sci Homework Help
Replies
9
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
957
Back
Top