Matlab: User defines variable name

  • MATLAB
  • Thread starter craigpmorgan
  • Start date
  • #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,

Craig
 

Answers and Replies

  • #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,
Craig
 
  • #4
272
0
I have done something similar before. My solution was to use a structured cell array. An example would be

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

Thus i would recommend you use an alphanumeric name as your ID.
 
  • #5
Thanks for the help, I think using structures is key to my problem.
 

Related Threads on Matlab: User defines variable name

Replies
1
Views
25K
Replies
5
Views
11K
  • Last Post
Replies
4
Views
3K
Replies
5
Views
127K
Replies
1
Views
2K
Replies
16
Views
192K
  • Last Post
Replies
4
Views
2K
Replies
2
Views
12K
Replies
2
Views
3K
Top