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

Matlab user input variable name

  1. Feb 25, 2009 #1

    I am writing a program where I have the user input the name of two previously created vectors, the computer subtracts them and then generates a variable in the workspace. I was wondering if there was a way to let the user pick this name. I have so far (Y and N have been previously defined as 1 and 0):

    subtract_data=input('Would you like to subtract any data sets? Y/N');
    if subtract_data == 1
    higher=input('Enter the name of the spectrum you would like to subtract from');
    lower=input('Enter the name of the spectrum you are subtracting');
    filename=input('Input file name: ', 's');



    I am not sure where to go from here how to get the variable new_data to be titled what the user inputs as filename.

  2. jcsd
  3. Feb 26, 2009 #2


    User Avatar
    Science Advisor

    You can use the eval function to evaluate text strings:

    >> bob = 'eye(3)'
    >> eval(bob)

    would run the command to create a 3x3 identity matrix. Really helpful (if inefficient) command since you can generate stuff on the fly, e.g. plotting a variable number of datasets on a 2D graph.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook