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 defines variable name

  1. Jul 28, 2011 #1
    Hi all,

    I'm trying to create a program that takes a user input and uses this input as the name of a cell array. I'm looking for something like the following:

    id = input('Please Enter Plant I.D: ')
    >> 112
    % The system then calls the cell array named 112

    % The user is then asked to input the relevant data, which is stored in a new row of the cell array 112 (I think I can program this, it is taking the initial input as the name of the cell array which I'm struggling with).

    Any help will be gratefully appreciated.
    Thanks for your time,

  2. jcsd
  3. Jul 28, 2011 #2
  4. Jul 29, 2011 #3
    Thanks for the response, but I'm sure if genvarname is what I need (I don't think I explained myself very well before).

    I have a variety of plants, each individually labelled and have set up some Matlab code to analyse certain properties such as height etc. when photos are uploaded.

    I'd like the system to:
    - request the plant i.d code from the user
    - Analyse photos (already coded)
    - Allocate a unique cell array to each plant, named in accordance with the i.d. entered
    - Store the analysis data in a new row of the cell array

    I can code everything except the creation of the cell arrays named in accordance with the i.d. numbers (the cell array of each plant is used for numerous data inputs over time)

    I've tried the eval function but various forums warn against this. Sorry for the long winded message!

    Again your time and advice is greatly appreciated,
  5. Jul 29, 2011 #4
    I have done something similar before. My solution was to use a structured cell array. An example would be

    Code (Text):
     name = input('What car would you like to drive?')
    mpg = input('What mpg does it have (0-50)')
    year = input('What what is the year it was made?')
    car.(name) = [mpg year]; %this is where the magic is done
    So for an input such as
    name = bentley
    mpg = 10
    year = 2010

    the result is
    Code (Text):
    car.bentley = [10 2010]
    Thus i would recommend you use an alphanumeric name as your ID.
  6. Aug 2, 2011 #5
    Thanks for the help, I think using structures is key to my problem.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook