MATLAB Matlab: User defines variable name

  • Thread starter Thread starter craigpmorgan
  • Start date Start date
  • Tags Tags
    Matlab Variable
AI Thread Summary
Creating dynamic cell arrays in MATLAB using user input as names poses challenges, particularly since variable names cannot start with a number. The discussion revolves around a user seeking to implement a system that requests a plant ID, analyzes photos, and stores data in uniquely named cell arrays corresponding to each plant's ID. The user initially considered using the eval function but was advised against it due to potential issues. A suggested alternative is to utilize structured arrays, which allow for alphanumeric names, thereby enabling the storage of data in a more manageable format. This approach simplifies the process of associating plant IDs with their respective data, facilitating ongoing data input and analysis.
craigpmorgan
Messages
8
Reaction score
0
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
 
Physics news on Phys.org
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
 
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.
 
Thanks for the help, I think using structures is key to my problem.
 

Similar threads

Replies
5
Views
3K
Replies
1
Views
3K
Replies
1
Views
9K
Replies
6
Views
5K
Replies
4
Views
1K
Replies
12
Views
3K
Replies
1
Views
3K
Replies
1
Views
3K
Back
Top