Matlab: Problem with structures

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

The discussion addresses a coding issue in MATLAB where a structure assignment fails when the variable 'id' is 10 or greater. The error message indicates insufficient outputs for comma-separated list expansion, likely due to incorrect data type handling. A proposed solution is to convert 'id' to a numeric value using the str2double function, ensuring proper indexing of the structure. This adjustment resolves the issue by preventing MATLAB from misinterpreting the 'id' as a string array.

PREREQUISITES
  • Familiarity with MATLAB programming language
  • Understanding of MATLAB structures and their usage
  • Knowledge of data type conversions in MATLAB
  • Basic debugging skills in MATLAB
NEXT STEPS
  • Explore MATLAB structure data types and their properties
  • Learn about the str2double function for data type conversion
  • Investigate error handling and debugging techniques in MATLAB
  • Study object-oriented programming concepts in MATLAB, particularly classes
USEFUL FOR

MATLAB programmers, data analysts, and anyone troubleshooting structure-related errors in MATLAB code.

craigpmorgan
Messages
8
Reaction score
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
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.
 
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:

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 12 ·
Replies
12
Views
4K
  • · Replies 6 ·
Replies
6
Views
6K
  • · Replies 1 ·
Replies
1
Views
9K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K