PDA

View Full Version : Matlab: User defines variable name


craigpmorgan
Jul28-11, 06:14 AM
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,

Craig

viscousflow
Jul28-11, 10:00 PM
No cell array can be named "112", however see http://www.mathworks.com/help/techdoc/ref/genvarname.html

craigpmorgan
Jul29-11, 05:56 AM
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,
Craig

viscousflow
Jul29-11, 10:37 AM
I have done something similar before. My solution was to use a structured cell array. An example would be

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
car.bentley = [10 2010]

Thus i would recommend you use an alphanumeric name as your ID.

craigpmorgan
Aug2-11, 04:54 AM
Thanks for the help, I think using structures is key to my problem.