Organizing Vectors into Structs then Structs into a Matrix

4 views (last 30 days)
Okay I'm working with some kind of confusing code so I'm going to do my best to explain things without copying the exact code I'm using.
I have a bunch of files I need to process which contain two related vectors, one vector is time (non linear) and the other vector contains amplitude values.
I'm trying to separate the vectors into structs of trials, each trial being 40 seconds long. (so any values between 0 and 40 seconds would be trial 1, any values between 41 and 81 would be trial 2.. etc)
My problem stems from writing the data into structs, if I have two values within one trial they get overwritten.
Example Vectors:
Time: 01, 05, 28, 43, 56, 69, 80, 102, 165
Amp: .0012, .005, .003, .456, .56, .098, .435, .454, .234
Example Code:
% Set up trial number and trial time
trialNum = 1;
trialTime = 40;
for i = 1: numel(files)
if time(i) <= trialTime;
trial(trialNum) = amp(i);
else
trialNum = trialNum +1;
trialTime = trialTime + 40;
trial(trialNum) = amp(i);
end
end
How can I change this so that it outputs things as a vector of values
For example with the given vectors the code would output:
trial(1) = [.0012,.005,.003]
trial(2) = [.456,.56,.098,.435]
trial(3) = [.435]
trial(4) =[0]
trial(5) = [.234]

Accepted Answer

Walter Roberson
Walter Roberson on 5 Mar 2014
binnum = 1 + floor(Time(:) / 40);
trial = accumarray( binnum, Amp(:), [], @(V) {V});
Note: 0 to 40 is 41 seconds not 40. The above code uses 0 to 40-eps, 40 to 79-eps and so on. If you want to be using 1 to 41-eps, 41 to 81-eps, and so on, then
binnum = 1 + floor( (Time(:)-1) / 40);

More Answers (1)

Mary
Mary on 5 Mar 2014
Thank you for such a quick response - I apologize for my delay in getting back to you.
THIS WORKS PERFECTLY and has saved me a TON of time.
Accumarray was just what I needed!!!

Categories

Find more on Structures 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!