How to save values into a data structure within a loop?

7 次查看(过去 30 天)
Hello,
I have a loop in which I find data points that are within a certain hour (e.g. 0 - 1 am) for a given day, and I want to do this for multiple days in a loop.
Here is my loop:
for day = 10; % would like 10:25
for hour = 0:23;
hour
date = [num2str(day),'-Jun-2014', ' ', make_string10(hour),':']; % make_string10 adds '0' before % hour
d = strmatch(date, datestr(date524t)); % find strings in time data
dt = date524t(d); % select time values
dy = y524t(d); % select y values (depth)
dz = z524t(d); % select z values (Temperature)
d = struct; % now I want a structure to contain this organised data
end
end
I want to save values as a structure array as follows:
d.day(day).hour(hour) = dt for example % or something similar
My ultimate goal is to plot data as a function of time of day, please let me know how I can do this or whether there is an easier method. However, if there is an easier method can someone also explain how I can do this using data structure, as it will very useful for other things I am doing using MATLAB.
Thanks alot in advance!
Michael

采纳的回答

Stephen23
Stephen23 2015-3-24
编辑:Stephen23 2015-3-24
Although I wrote a big answer saying that you could try using non-scalar structures, the simplest answer may be to keep all of your data in simple numeric arrays: this will work well if your data values are scalar, in which case simple numeric arrays will be the fastest and easiest solution to storing and plotting the data. Simply assign one dimension to represent the days, and the other the hours. Simple.
If these values are not scalar, then you should read on...
You could use a non-scalar structure to store this. A non-scalar structure can be indexed just like any other array in MATLAB, and you could make one axis represent the hours, and another the days:
d(days,hours).depth = [...]
d(days,hours).temp = [...]
Note that these are indices, not values, so they must be positive integer values. You can include fields for each kind of data (e.g. Process type, Flow data, Temperature data, Notes, Units, etc), and the structure itself can be indexed just like any other MATLAB array, here shown with some subscripts day and hour:
d(day,hour).depth = [..];
d(day,hour).temp = [..];
d(day,hour).(...) = [..];
Note that you can even define structure fieldnames dynamically, and that there are many functions that support working on structures and cell arrays, and can access these data easily, and they can also be used in vectorized code (which is something you need to learn about).
Non-scalar structures allow very easy access to the data, for example you can obtain all of the depth values in a comma separated list like this:
d.depth
which means you can create a cell array from all depth values like this:
{d.depth}
or concatenate all the depth data matrices like this:
[d.depth]
or use them all as inputs to a function like this:
vertcat(d.depth)
You can retrieve all of the fields and values of for one group of data simply using the index, e.g.:
d(9,2)
returns the scalar structure with all of the fields for the ninth day and the 2nd hour, and
d(9,2).depth
gives the corresponding depth value. The index can, just like any other MATLAB array, be logical, subscripts or linear indexing . so you can easily perform operations on a subset of your data like this:
myfunction(d(logical_array).temp)
to input into some function the selected temp values. You can also subset a non-scalar structure, just like you would any other array:
B = d(3:6)
assigns the subset of the original structure to the variable B using linear indexing. When you first create the structure, you should consider preallocating it so that it does not keep getting expanded in a loop.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by