Matlab: dynamic data entry into structure

Click For Summary

Discussion Overview

The discussion revolves around dynamically entering data into a structure in Matlab, specifically for monitoring plant growth. Participants explore methods to append new data to existing fields without requiring the user to specify the iteration number, addressing concerns about potential data overwriting and the need for user input.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Exploratory

Main Points Raised

  • One participant describes a structure containing various fields and expresses a desire to automatically extend matrices with new data without user input for iteration.
  • Another participant suggests using the size function to determine the number of elements in a matrix, indicating that this could help in managing data entries.
  • A participant shares their implementation using a loop but seeks a more elegant solution, mentioning the use of numel to determine previous iterations.
  • Another reply proposes a method to append new values directly to existing arrays, emphasizing that this could simplify the data entry process.
  • One participant mentions the potential for efficiency improvements through preallocating matrices if a predetermined number of entries is known.
  • A participant highlights the challenge of managing the structure when no data has been entered previously, sharing a working code snippet that handles both existing and new plant data entries.
  • Another participant reiterates that their approach does not require knowledge of iterations, focusing instead on appending new entries directly to an array.
  • One suggestion includes using the 'exist' function to check for the presence of data instead of relying on user input, which could reduce errors and streamline the process.

Areas of Agreement / Disagreement

Participants express differing views on the best approach to dynamically manage data entries in Matlab. While some agree on the utility of appending data directly, others emphasize the need for initial checks on data existence, leading to multiple competing strategies without a clear consensus.

Contextual Notes

Participants note limitations related to user input and the need for predefined structures, as well as the complexity introduced when no prior data exists for a given plant. The discussion reflects a variety of coding strategies and assumptions about data management in Matlab.

craigpmorgan
Messages
8
Reaction score
0
Hi all,

I've created a structure containing various fields of data. The data in these fields is stored in matrices and is be added to at regular intervals. I'd like the system to identify the amount of inputs the fields already contain and then extend said matrices to include this new data.

The program concerns the monitoring of plant growth.
The code below works fine but requires the user to know the amount of measurements that have already been input. I'd like to avoid this as it could easily lead to data being overwritten:id = input('Enter plant number: ');
iteration = input('Enter iteration: ');

plant(id).entryDate(iteration) = {date};
plant(id).height(iteration) = [height]; % The variable height is already in the workspace

My idea for ensuring new data is stored in the correct place was to use numel function, something like the following (removing the user-input value of 'it' from above):

previous_iterations = numel(plant(id).height);
it = previous_iterations + 1;

I can't this this or anything similar to work.

Thanks very much for your time, I'd greatly appreciate any help.
Craig
 
Last edited:
Physics news on Phys.org
do you mean for each field, how many elements they contain?

that's simply:

size(plant(id).matrixofinterest)

where matrixofinterest is the field you want a size for.
 
Hi Pythagorean,

I can find the number of elements within the matrix of a particular field using either the numel or the size functions. However, I'm not sure of the syntax that checks the size of the existing matrix (via one of the functions mentioned) and makes the incoming data an extension of it. I require something like the following. I have now achieved what I require using a loop but I'm convinced there's a more elegant solution.

id = input('Enter plant number: ');

previous_iterations = numel(plant(id).height);
iteration = previous_iterations + 1;
plant(id).entryDate(iteration) = {date};
plant(id).height(iteration) = [height]; % The variable height is already in the workspace

As I said, I have now got the program to work with an if statement but if anyone knows how to make the above (or similar) work I would be grateful.

Thanks again,
Craig
 
id = input('Enter plant number: ');

previous_iterations = numel(plant(id).height);
iteration = previous_iterations + 1;
plant(id).entryDate(iteration) = {date};
plant(id).height(iteration) = [height]; % The variable height is already in the workspace

Ok, I think I understand what you mean. In general, you can assign x with an appended row/column/element like:

x = [x new_value]

that is, make x the old x with an additional value called "new_value"


Applied to your problem:

plant(id).entryDate(iteration) = [plant(id).entryDate(iteration) {date}];
plant(id).height(iteration) = [plant(id).height(iteration) height]



I'm not sure how your data entry is going though. You shouldn't need a for loop if you're just doing this every time you get a new entry. If you're adding batch entries, then you CAN add all entries at once:

x = [x x_new] where x_new is now a vector of the new entries (rather than entering them one at a time).

so if x = [1 2 3]

and I go

x = [x [4 5 6]]

I'll get:

x = [1 2 3 4 5 6];

is that helpful in your implementation?

You may just need to run me through the the overall goal and the general order of operations if I'm still missing the mark.
 
Also, if you have a predetermined number of entrees, we can make it a bit more efficient, depending on the sizes you're working with, by preallocating the matrices in the structure.
 
Hi,

The code you've written doesn't quite apply to this problem, though it works if the user manually inputs the iteration number.

My issue is that I don't want the user to have to specify the iteration before the data is stored, which means that the structure 'plant' needs to be previously defined. However, if no data for 'plant x' has not been entered previously, the instance of the structure plant(x) does not yet exist. The following code works, I'm happy to leave it as is, since this problem seems more awkward than I realized!

% STORES DATA IN CORRECT PLACE WITHOUT THE NEED TO INPUT ITERATION

avgHeight = 22; % In the actual program these variables will be
avgVol = 33; % present in the workspace and are used here just
% testing.

id = input('Enter ID: ');
XX = 2;
while XX ~= 1; % Variable XX used to ensure valid input
q = input('Has any data for this plant been entered previously? ','s');

if q == 'y' | q == 'Y' % Since data has been entered previously,
id = input('Enter ID: '); % plant(id) already exists and can be
it = numel(plant(id).height); % added to.
next = it + 1;

plant(id).date(next) = {date};
plant(id).height(next) = [avgHeight];
plant(id).volume(next) = [avgVol];
plant(id)
XX = 1;
elseif q == 'n' | q == 'N' % Since plant is new, plant(id) is created
next = 1; % and the variables are each stored in the
% first position of each field.

plant(id).date(next) = {date};
plant(id).height(next) = [avgHeight];
plant(id).volume(next) = [avgVol];
plant(id)
XX = 1;

else
disp(sprintf('\nThis input is not valid. Please answer Y/N.'));

end
end

As I said, this code now does what I need but seems overly messy.
Thanks for the help
 
the code i wrote does not require knowledge of iterations. It merely appends the latest entry to the end of an already established array. For instance, you could type:

a = [];

a = [a 5];
a = [a 3];

and you will now have:

a = [5 3];

note that 5 and 3 are actual entries, not number of iterations or anything, you're building up a one element at a time.

However, if no data for 'plant x' has not been entered previously, the instance of the structure plant(x) does not yet exist.

and speaking of exist... try 'help exist' rather than requiring user input. This avoid user error and reduces seconds per entry spent answering the input.
 

Similar threads

  • · Replies 12 ·
Replies
12
Views
4K
  • · Replies 1 ·
Replies
1
Views
10K
  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
5K
  • · Replies 2 ·
Replies
2
Views
5K
Replies
1
Views
2K
Replies
1
Views
4K