How to split data into two matrices

2 views (last 30 days)
I am doing some analysis for my Honor's Thesis and am having some problems pre-processing of the data. The data is in a 4x147000 matrix. Row one is the time, two and three are my signals of interest, and the fourth is the stimulus - the condition I want to split the data up by. Due to imperfect data, not all of the events are the same length, so I need to take chunks of data either 490 or 990 columns long depending on the value of the fourth row. For example, I want to take the first 490 columns whose fourth row equals 0 and put into a new matrix "datanostim." Then I want to scan the matrix until the fourth row changes to 255, and take the first 990 columns whose fourth row equals 255 and add to a new matrix "datastim." This then repeats, so the next 490 columns whose fourth row is 0 is added to the datanostim matrix, the next 990 columns whose fourth row is 255 added to the datastim matrix, and so on.
Here is my attempt, but I would love to see other methods as well, as mine gives me the following: "Error: Function definitions are not permitted in this context."
function [datastim, datanostim] = datacutoff(data, stim, nostim)
%UNTITLED Summary of this function goes here
% data = data as matrix, fourth row as stim 0 or 255
% stim = length of stimulus
% nostim = length without stimulus
datastim = [];
datanostim = [];
i = 1;
if data(4,i) ~= data(4,i+1)
i = i+1;
else if data(4,i:i+nostim) == 0
horzcat (data(:,i:i+nostim), datanostim);
i = i+nostim;
end
end
if data(4,i) ~= data (4,i+1)
i = i+1;
else if data(4,i:i+stim) == 255
horzcat (data(:,i:i+stim), datastim);
i = i+stim;
end
end
end
Thanks!

Accepted Answer

Star Strider
Star Strider on 3 Nov 2014
"Error: Function definitions are not permitted in this context."
To solve that, you need to save your ‘datacutoff’ function to a separate .m-file ‘datacutoff.m’ in your MATLAB search path. Then you should be able to use it.
Then run it.
  10 Comments
Lucas
Lucas on 7 Nov 2014
I'm glad because there is slight problem. When I run it at lengths of 490 for nostim and 990 for stim, the trial lengths aren't consistent. For example, the datanostim out put is a 4X48216, which means that out of 98 trials each is 492 bins. The problem also occurs for the datastim.
Star Strider
Star Strider on 7 Nov 2014
Edited: Star Strider on 7 Nov 2014
Delete the +1 at the end of the ‘datastim’ and ‘nodatastim’ assignments in the loops. If you’re still having problems, convert them to -1. (It wasn’t clear to me where you were counting from.)
Also, I just picked up a typo I didn’t see before. Change the ‘datanostim’ assignment to:
datanostim = [datanostim data(:,nostimind(k1):nostimind(k1)+nostimlen+1)];
changing the +1 as appropriate to get the addressing correct.
To avoid the problem with the index ranges exceeding the ‘data’ column size, you could put as the first statements in both for loops:
if (stimind(k1)+stimlen > length(data,2))
return
end
and:
if (nostimind(k1)+nostimlen > length(data,2))
return
end
respectively, and include the appropriate +1 or other indexing corrections there as well. That will test to be sure you’re not over-reading ‘data’, and if either one is true it will immediately return to the calling script rather than throwing the error. The ‘1:98’ limitation will not be necessary with these if blocks. If you’re happy with the ‘1:98’ limitation, keep them as they are now.

Sign in to comment.

More Answers (1)

Lucas
Lucas on 6 Nov 2014
Whoops, here is the data.

Community Treasure Hunt

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

Start Hunting!