Matlab: Problem with structures

In summary, the conversation discusses an issue with a code written in Matlab that calculates parameters and saves data to a structure. The code works fine with single digit numbers, but returns an error when the number is greater than or equal to 10. A suggestion is made to use a class instead of a structure, and a possible solution is to convert the id to a number.
  • #1
craigpmorgan
8
0
Hi All,

I've written some code in Matlab that calculates a number of parameters and saves the data to a structure. A portion of the code is shown below:

uniqueName = genvarname(date);
plant(id).(uniqueName) = {date avgHeight avgVol};
.
The various parameters such as avgHeight etc. are defined earlier in the program. The confusing thing is that the code works fine when 'id' is a single digit number. However, when 'id' >= 10, the program returns the following error:
.
? Insufficient outputs from right hand side to satisfy comma separated
list expansion on left hand side. Missing [] are the most likely cause.
.
I don't understand the error message and don't know what to do next.
Any help I receive will be greatly appreciated.
Thanks very much for your time,
Craig
 
Physics news on Phys.org
  • #2
You might consider building a class instead of using a struct.

As for the error, there's not much to go on without having more code.
 
  • #3
As far as I know, a structure is a type of class in MATLAB:http://www.mathworks.com/help/techdoc/matlab_oop/brh2rgw.html#brgljyu

craigpmorgan, try:

plant(str2double(id)).(uniqueName) = {date avgHeight avgVol};

so that you convert id to a number in case it's a string. It seems to me like it's interpreting 10 (for instance) as a 1 and 0 in a string array, rather than a single 10 in a number array.
 
Last edited by a moderator:

Related to Matlab: Problem with structures

1. What is a structure in Matlab?

A structure in Matlab is a data type that allows you to store multiple variables of different types in a single container. It is similar to a struct in other programming languages and is useful for organizing and manipulating large datasets.

2. How do I create a structure in Matlab?

To create a structure in Matlab, you can use the "struct" function and specify the field names and corresponding values. For example, struct('name', 'John', 'age', 25) will create a structure with two fields: name and age.

3. How do I access data within a structure in Matlab?

To access data within a structure in Matlab, you can use dot notation. For example, if the structure is named "person" and has a field named "name", you can access the name by typing person.name. You can also use indexing to access specific elements in a structure.

4. Can I add or remove fields from a structure in Matlab?

Yes, you can add or remove fields from a structure in Matlab using the "fieldnames" and "rmfield" functions, respectively. The "fieldnames" function returns a list of all the fields in a structure, and the "rmfield" function removes a specific field from a structure.

5. How do I loop through a structure in Matlab?

You can use the "fieldnames" function to get a list of all the fields in a structure and then use a for loop to iterate through each field. Within the loop, you can use dot notation to access the data within each field.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
12
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
5K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
8K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
3K
Back
Top