How to Correctly Prompt for Yearly Maintenance Costs in a MATLAB While Loop?

  • MATLAB
  • Thread starter akhanijow
  • Start date
  • Tags
    Loop Matlab
In summary, the code attempts to calculate the present value of a project, but is having difficulty doing so.
  • #1
akhanijow
7
0
Hi everyone,

I am writing a MATLAB program and am having some issues. Here is the code I have written so far:

Code:
clear all
clc


info(1) = input('What is the MARR (%)? ');
info(2) = input('What is the lifetime of the project? ');
info(3) = input('What is the initial investment ($)? ');


m=0;
while m < info(2)
    m = m+1;
    info(4) = m;
    info(5) = input('What is the maintenance cost for year',num2str(m),' ? ');
end


functionanswer = Evaluation(info);

disp('The Present Value of the project is');
disp(functionanswer(1));


For info(5), I am trying to make it say "what is the maintenance cost for year __?"
I have tried using num2str, and may other things but can't seem to get it! Any help would be great! Thanks!
 
Physics news on Phys.org
  • #2
Have you tried reading the documentation on the input function?
 
  • #3
Hey,
Yes I did, I still can't seem to figure it out...I tried using the %d function and num2str, but its failed to work. I also tried using 's', but that is for a string, and I am just trying to have the input show up as:

"What is the maintenance cost for year __?"

Any idea, thanks so much!

Also, http://www.uni-koeln.de/rrzk/software/mathematik/matlab_help/techdoc/ref/input.html" is what I read on the input function:
 
Last edited by a moderator:
  • #4
akhanijow said:
So why are you trying to use it in a way that will not work? The function takes one or two arguments only. The only allowed value for the second argument is 's', and that simply bypasses the normal Matlab interpretation of the user input. The first argument is the prompt string, and it must be a string.

You need to combine multiple strings to form a single string. How do you do that? (Hint: Use the documentation.)
 
Last edited by a moderator:
  • #5


Hi there,

It looks like you are trying to prompt the user for the maintenance cost for each year of the project using a while loop. Your code looks good so far, but you just need to make a small change in the input statement for info(5). Instead of using num2str, you can use the sprintf function to create a string with the year number. Here is an example:

info(5) = input(sprintf('What is the maintenance cost for year %d? ', m));

This will create a string that says "What is the maintenance cost for year 1?" for the first iteration of the loop, "What is the maintenance cost for year 2?" for the second iteration, and so on.

Hope this helps! Let me know if you have any other questions.
 

Related to How to Correctly Prompt for Yearly Maintenance Costs in a MATLAB While Loop?

1. What is a while loop in Matlab?

A while loop in Matlab is a programming structure that allows a set of instructions to be repeatedly executed as long as a certain condition is met. This is useful for automating repetitive tasks or for performing calculations until a certain criteria is reached.

2. How do I use a while loop in Matlab?

To use a while loop in Matlab, first define the initial condition that needs to be met for the loop to start. Then, write the code that needs to be executed within the loop. Finally, update the condition inside the loop so that it eventually becomes false and the loop ends.

3. What is the difference between a while loop and a for loop in Matlab?

A while loop in Matlab is used when the number of iterations is not known beforehand, while a for loop is used when the number of iterations is predetermined. In a while loop, the condition is checked before each iteration, while in a for loop, the number of iterations is specified at the beginning.

4. Can I nest while loops in Matlab?

Yes, while loops can be nested in Matlab. This means that a while loop can be placed inside another while loop or inside a for loop. However, it is important to ensure that the condition for each loop is properly updated to avoid an infinite loop.

5. How do I exit a while loop in Matlab?

To exit a while loop in Matlab, the condition inside the loop needs to be updated so that it eventually becomes false. This can be achieved using a break statement or by using a counter variable that keeps track of the number of iterations and is incremented until the condition is no longer met.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
14
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
Back
Top