Matlab help for a beginner! - Cell arrays and indexing

edited May 2007 in Science & Tech
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

Comments

  • shwaipshwaip bluffin' with my muffin Icrontian
    edited May 2007
    check out how to use dlmread or csvread.

    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]
  • edited May 2007
    Thank you so much for the reply. It helped, and made me realize I am on the right track. I have been using csvread.

    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.
  • primesuspectprimesuspect Beepin n' Boopin Detroit, MI Icrontian
    edited May 2007
    On a completely unrelated note:

    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.....
  • edited May 2007
    for me - i guess it was the result of others posting matlab questions. I did a quick google search and some previous matlab questions from this board came up.

    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.
  • primesuspectprimesuspect Beepin n' Boopin Detroit, MI Icrontian
    edited May 2007
    Oh no, it's not a bother at all! I'm really happy you found us, I was just curious because search stats are sort of a hobby of mine (you could say I have a vested interest ;) ) and if we can optimize things to bring even more people here for matlab help, then I want to learn as much as I can about where the search results are coming from :)
  • shwaipshwaip bluffin' with my muffin Icrontian
    edited May 2007
    rance wrote:
    Thank you so much for the reply. It helped, and made me realize I am on the right track. I have been using csvread.

    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.

    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.
  • primesuspectprimesuspect Beepin n' Boopin Detroit, MI Icrontian
    edited May 2007
    Not old yet - give me another month and a half...
  • edited May 2007
    Thanks again shwaip.

    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
    #,#
    #,#
    #,#
    #,#
  • shwaipshwaip bluffin' with my muffin Icrontian
    edited May 2007
    are the lengths of all the start-endpoints the same? If so, you could do this:

    [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).
  • edited May 2007
    shwaip - Just wanted to send out a big thanks! Everything worked out perfectly! Thanks for all the help and speedy replies!

    I have never seen the {} feature before! It's great!
  • edited May 2007
    Hey Shwaip

    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)??
  • shwaipshwaip bluffin' with my muffin Icrontian
    edited May 2007
    I don't think you can do it unless they are all the same length.
Sign In or Register to comment.