Matlab: User defines variable name

In summary, the conversation discusses creating a program that takes user input to name a cell array and then stores relevant data in the cell array. The use of structures is recommended to solve the issue of naming the cell arrays according to the input.
  • #1
craigpmorgan
8
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
  • #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
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 to Matlab: User defines variable name

1. What is the purpose of user-defined variable names in Matlab?

User-defined variable names in Matlab allow users to assign a name to a specific value or data in the program. This makes it easier to reference and manipulate the data throughout the code.

2. How do I define a variable name in Matlab?

To define a variable name in Matlab, use the assignment operator (=) followed by the desired name and the value to be assigned. For example, "x = 5" will define the variable "x" with the value of 5.

3. Can I use any name for a variable in Matlab?

Yes, you can use any combination of letters, numbers, and underscores for a variable name in Matlab. However, the name cannot begin with a number and cannot be a reserved keyword in Matlab.

4. Can I change the name of a variable after it has been defined?

Yes, you can change the name of a variable in Matlab by using the assignment operator (=) and assigning a new name to the variable.

5. Is there a limit to the length of a variable name in Matlab?

Yes, the maximum length of a variable name in Matlab is 63 characters. It is recommended to use concise and meaningful names for variables to improve code readability.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • Programming and Computer Science
Replies
10
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
12
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
9K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
19
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
  • Programming and Computer Science
Replies
29
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
2K
Back
Top