Matlab: User defines variable name

  • Context: MATLAB 
  • Thread starter Thread starter craigpmorgan
  • Start date Start date
  • Tags Tags
    Matlab Variable
Click For Summary

Discussion Overview

The discussion revolves around creating dynamic variable names in MATLAB based on user input, specifically for naming cell arrays according to plant identification numbers. Participants explore methods to achieve this while addressing concerns about best practices and potential pitfalls.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • One participant seeks to create a cell array named after a user-defined plant ID, expressing difficulty in implementing this in MATLAB.
  • Another participant points out that variable names cannot be purely numeric, referencing MATLAB documentation on valid variable naming.
  • A participant suggests using structured arrays as an alternative, providing an example of how to store data using alphanumeric names for identifiers.
  • The original poster expresses uncertainty about using the eval function due to warnings found in forums, indicating a preference for safer coding practices.
  • The original poster acknowledges the suggestion of using structures as a potential solution to their problem.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the best approach to dynamically name cell arrays, with some advocating for structured arrays while others highlight the limitations of using numeric names. The discussion remains unresolved regarding the optimal method for the original poster's needs.

Contextual Notes

There are limitations regarding the use of numeric variable names in MATLAB, and the discussion reflects uncertainty about the implications of using eval versus structured arrays. The original poster's requirements for data storage over time add complexity to the problem.

Who May Find This Useful

Individuals working with MATLAB who are interested in dynamic variable naming, data storage techniques, and best practices for coding in a structured manner may find this discussion relevant.

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 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
10K
  • · Replies 12 ·
Replies
12
Views
4K
  • · Replies 19 ·
Replies
19
Views
2K
  • · Replies 6 ·
Replies
6
Views
6K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K