Create a series of vector with different name
Show older comments
I have a vector list of data and i want to subdivided this vector in sub vector that contains data for each interval of ten minutes. In the first vector the data is like this '161724' that is 16:17:24 hh/mm/ss. i want to assign the name of the vectors for example vec_number from 1 to 144 in a loop. How i can do this?
2 Comments
"i want to assign the name of the vectors for example vec_number from 1 to 144 in a loop."
Putting meta-data (e.g. pseudo-indices) into variable names will force you into writing slow, inefficient, complex, obfuscated, buggy code that is difficult to debug. Read this to know more:
The simple and efficient alternative is to use indexing.
Jon
on 1 Oct 2020
In case the motivation wasn't clear, Steven's comment explains why I had earlier provided an indexing approach in my answer below
Answers (3)
s = '161724'
v = [s(1:2),':',s(3:4),':',s(5:6)]
If array
s = ['161724'; '161725'; '161726'] ;
k = repelem(":",size(s,1),1)
v = [s(:,1:2),k,s(:,3:4),k,s(:,5:6)]
I would suggest rather than naming your subvectors, you reorganize your data to put it into a matrix where each column is one of your 10 minute subvectors. Assuming for example if your original data is in vector, x, and your sample rate is given by pointsPerMinute, you could do this with
intervalLength = 10; % 10 minute intervals
numPoints = length(x)
samplesInInterval = pointsPerMinute*intervalLength
numIntervals = numPoints/samplesInInterval;
X = reshape(x,samplesInInterval,numIntervals)
Then if you wanted for example the data in the third interval you could get it using
xInterval = X(:,3)
3 Comments
elena galvano
on 1 Oct 2020
Jon
on 1 Oct 2020
If the number of data points in each interval is different you could save the data in a structure indexed by interval. So something like data(1).values, data(2).values etc where the index is the interval number, so data(1).values is a vector, and data(2).values is vector but not necessarily of the same length.
Jon
on 1 Oct 2020
Steven Lord's suggestion is also a very good approach
Steven Lord
on 1 Oct 2020
0 votes
If you have time and/or date based data, consider storing it in a timetable array. Then you can access data from a certain range of times with timerange or resample the data with retime and/or synchronize.
Categories
Find more on Resizing and Reshaping Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!