Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Matlab while loop, Please help!

  1. Apr 22, 2010 #1
    Hi everyone,

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

    Code (Text):
    clear all

    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 ($)? ');

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

    functionanswer = Evaluation(info);

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

    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 cant seem to get it! Any help would be great! Thanks!
  2. jcsd
  3. Apr 22, 2010 #2

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    Have you tried reading the documentation on the input function?
  4. Apr 22, 2010 #3
    Yes I did, I still cant 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: Apr 25, 2017
  5. Apr 22, 2010 #4

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    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: Apr 25, 2017
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook