Matlab help for a beginner! - Cell arrays and indexing
Hi All
First off.....I apologize for the lame question.
I'm looking for some matlab help. I have a long data set (1 column, over 200,000 points) and would like to pick out specific 'groups' of data from it. I have a program developed that will do this if I use the input command and manually type my start and end point for the group of data I want. However, since I have a lot of these 'start-end' times, I would like to call in another file that has all of them listed, and my program run through each 'start-end' time.
make sense? i didn't think this would be hard to do, but I'm having considerable trouble!
Any recommendations would be great!
Thanks
Rance
First off.....I apologize for the lame question.
I'm looking for some matlab help. I have a long data set (1 column, over 200,000 points) and would like to pick out specific 'groups' of data from it. I have a program developed that will do this if I use the input command and manually type my start and end point for the group of data I want. However, since I have a lot of these 'start-end' times, I would like to call in another file that has all of them listed, and my program run through each 'start-end' time.
make sense? i didn't think this would be hard to do, but I'm having considerable trouble!
Any recommendations would be great!
Thanks
Rance
0
Comments
if you format the start and end times in a file as follows
starttime1, endtime1
starttime2, endtime2
...
and so on.
times = csvread('timefile.csv');
will put the times into a 2-column matrix called 'times'.
Then, if you say times(1,: ), you'll get starttime1 endtime1. If you write these times as indicies to 'timefile.csv', then you can just do the following
indicies = times(1,1):times(1,2);
group = longdataset(indicies);
then you can loop through if you want:
[php]
times = csvread('timefile.csv');
for i=1:length(times)
indicies = times(i,1):times(i,2);
group = hugedata(indicies);
%do stuff with group
end
[/php]
Here is what I have based on what you suggested, but still have a similar issue as before:
times = csvread('info_file.csv');
start = times(:,1);
endp = times(:,2);
for i=1:length(times)
indicies=start(i,1):endp(i,2);
group=hugedata(indicies);
<code style="white-space: nowrap;"><code>%do other stuff with group </code></code>
end
It seems to do what I need for the last group of start-end times. But doesn't carry on from the first to the next. Any suggestions?
Thanks again.
Where on earth are all these Matlab "help me" questions coming from?
Don't get me wrong - I'm really happy that short-media is here for you guys, but it's been driving me nuts - how on earth did our site somehow end up a resource for Matlab help?
Rance, if you don't mind, just to satisfy my curiosity, of course, could you tell me how you found us?
Thanks! Carry on.....
Sorry for all the bother. I'll figure out the rest of my problem based on what shwaip has suggested.
Although I haven't posted here before, I've actually been reading threads on this board for awhile.
Hi rance:
once you've assigned start and endp to be vectors, they're one-dimensional. I'm surprised that matlab isn't giving you errors when you try
indicies=start(i,1):endp(i,2)
because endp(i,2) should be out of the index bounds.
just try indicies=start(i):endp(i)
also, just ignore prime. He's just some creepy (old) guy that's always around, for some reason.
This seems to work. indicies will loop through all start-endpoints in the command window, but only show an indicies (1 x specific number matrix) in the workspace - which is the last start-end point.
The last start-endpoint then takes the correct data (based on the start-endpoint) from 'hugedata'.
How would I go about getting all start-endpoints (from hugedata) in one large 'group' matrix . IE
start-end1, start-end2
#,#
#,#
#,#
#,#
[php]
times = csvread('timefile.csv');
startp = times(:,1);
endp = times(:,2);
dataLen = startp(1)-endp(1); %get the length of a segment
groupmat = zeros(dataLen,length(times));
for i=1:length(times)
indicies = startp(i):endp(i);
groupmat(:,i) = hugedata(indicies);
end
[/php]
Then, each column will be a shorter group.
But, if each segment isn't the same length:
[php]
%this assumes hugedata is a column
times = csvread('timefile.csv');
startp = times(:,1);
endp = times(:,2);
groupmat = cell(1,length(times));
for i=1:length(times)
indicies = startp(i):endp(i);
groupmat{i} = hugedata(indicies); %note the curybraces
end
[/php]
Then to access a single group from groupmat, you'd need to use cell indexing:
groupmat{1} would return the hugedata(startp(1):endp(1)) (again, note the curly braces on groupmat).
I have never seen the {} feature before! It's great!
Got another question for ya.
Depending on how many start stop times there are....I will have that # of groupmats (ie {1}, {2}, {3} and so on). Can I take the data from each of these, and put them in one large data set (ie 1,: 2,: and so on)??