Filling a matrix with a for loop, calling content from a second matrix

I have a matrix containing a stream of continuous data in the following format:
Frame Fluo
1 0,003
2 0,003
3 0,004
4 0,001
5 0,001
6 0,001
7 0,001
8 0,002
9 0,002
10 0,002
etc...........
And a second matrix that states when a specific event happens within that stream of data
EventStartingFrame
1132
1944
2366
2994
3596
4370
5189
6005
6469
7205
etc.........
My goal is to extract the values of Fluo in an interval of +120 frames after the frame indicated by the second matrix and place it in another matrix.
So far this is what I've written. It works, but only place the last interval of values in the new matrix. How can I make it fill in the matrix with all the individual sub-series? For example one in each column?
%gets the datastream file
datastream = uigetfile('.xlsx')
%gets the event repository file
eventframes = uigetfile('.xlsx')
%puts the files into a matrix
DATA = xlsread(datastream)
EVENT = xlsread(eventframes)
%preallocate the results matrix, suppose I have 100 events
RESULTS = zeros(120,100)
%fill in the matrix
for i=EVENT(1:end,1)
j=i+119
RESULTS = M(i:j,2)
MAT(:,:) = RESULTS
end

2 Comments

This is highly unusual:
for i=EVENT(1:end,1)
Remember that for uses each column of its input values as the index.
This is the piece I was missing!! Thanks a lot!

Sign in to comment.

 Accepted Answer

Riccardo - the problem with your code is the line
MAT(:,:) = RESULTS
You are overwriting the data stored on the previous iteration and so you somehow need to store the data for each iteration (event) in MAT. Since you will have a variable number of values for each event, consider using a cell array to store your data. For example,
MAT = cell(length(EVENT), 1);
k = 1;
for i=EVENT(1:end,1)
j=i+119;
RESULTS = M(i:j,2);
MAT{k} = RESULTS;
k = k + 1;
end
Try the above and see what happens.

3 Comments

Thank you for your answer.
Now what happens is that MAT is storing only the result of the first iteration of the for cycle.
After running it
k is =2 and
MAT has 1 cell filled with the expected first array and 9 empty cells.
How can this be? I feel I'm missing something essential.. Updated code below
%gets the datastream file
datastream = uigetfile('.xlsx')
%gets the event repository file
eventframes = uigetfile('.xlsx')
%puts the files into a matrix
DATA = xlsread(datastream)
EVENT = xlsread(eventframes)
%fill in the matrix
MAT = cell(length(EVENT), 1);
k = 1;
for i=EVENT(1:end,1)
j=i+119;
RESULTS = DATA(i:j,2);
MAT{k} = RESULTS;
k = k + 1;
end
In your for loop, change the column vector to a row vector. I suspect that if you use the debugger, you will see that i is all the elements in EVENT. Try doing
for i=EVENT'
Works like a charm! Thank you very much!

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Release

R2018b

Community Treasure Hunt

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

Start Hunting!