Thread Subject: a for loop to create several arrays

Subject: a for loop to create several arrays

From: James Franklin

Date: 8 Feb, 2010 22:48:04

Message: 1 of 5

Hi

I have large data sets (#####x5 double arrays) that are chopped up using a for/if loop.

I would like to name new arrays containing the chopped parts of the data set.

I have the data set name in a character array, 080210.txt, for example.

so a simple example showing my problem...

clear all
clc
% some pretend data
a=[1:100;1:100;1:100];
data=transpose(a);
[length,width]=size (data);
counter=25;
lastchop=1;
% a pretend for loop for chopping
for rowcounter=1:length;
    if data(rowcounter,1)>=counter;
        name(lastchop:rowcounter,1:width)=data(lastchop:rowcounter,1:width);
        [choplength,chopwidth]=size(name);
        lastchop=choplength;
        counter=counter+25;
    end
end

this overwrites the array 'name'

I would like 'name' to be named 080210 using the character array 080210.txt

and..

the index '01' or similar to denote which slice of the data i am looking at

so for the above loop I would have arrays 08021001 08021002 08021003 08021004 containing rows 1..25 25..50 50..75 75..100 from the data.

I hope it's a simple answer!

Thank you

James

Subject: a for loop to create several arrays

From: Nathan

Date: 8 Feb, 2010 23:08:21

Message: 2 of 5

On Feb 8, 2:48 pm, "James Franklin" <nos...@hotmail.com> wrote:
> Hi
>
> I have large data sets (#####x5 double arrays) that are chopped up using a for/if loop.
>
> I would like to name new arrays containing the chopped parts of the data set.
>
> I have the data set name in a character array, 080210.txt, for example.
>
> so a simple example showing my problem...
>
> clear all
> clc
> % some pretend data
> a=[1:100;1:100;1:100];
> data=transpose(a);
> [length,width]=size (data);
> counter=25;
> lastchop=1;
> % a pretend for loop for chopping
> for rowcounter=1:length;    
>     if data(rowcounter,1)>=counter;
>         name(lastchop:rowcounter,1:width)=data(lastchop:rowcounter,1:width);
>         [choplength,chopwidth]=size(name);
>         lastchop=choplength;
>         counter=counter+25;
>     end
> end
>
> this overwrites the array 'name'
>
> I would like 'name' to be named 080210 using the character array 080210.txt
>
> and..
>
> the index  '01' or similar to denote which slice of the data i am looking at
>
> so for the above loop I would have arrays 08021001  08021002  08021003  08021004 containing rows 1..25  25..50  50..75  75..100 from the data.
>
> I hope it's a simple answer!
>
> Thank you
>
> James

First off: You cannot have a variable named 080210.
Secondly: DON'T TRY TO CREATE VARIABLES LIKE THAT! Use a cell array or
a struct instead!
Variables must begin with a letter! To fix this, I just put an
arbitrary letter ('n') before it.
Your loop was confusing, so I simplified it.
n080210 will be a CELL ARRAY containing the sliced matrices that you
specified (almost).
Rather than doing 1..25 25..50 50..75 75..100, I did 1..25 26..50
51..75 56..100, so they were all of equal length (length == 25). In
your case the lengths were 25,26,26,26!


% some pretend data
a=[1:100;1:100;1:100];
data=transpose(a);
[length,width]=size (data);
counter=25;
% a pretend for loop for chopping
for rowcounter=1:floor(length/counter);
        counter=counter*rowcounter;
        n080210{rowcounter} =
data(25*(rowcounter-1)+1:25*(rowcounter),:);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
n080210{1}
ans =
     1 1 1
     2 2 2
     3 3 3
     4 4 4
     5 5 5
     6 6 6
     7 7 7
     8 8 8
     9 9 9
    10 10 10
    11 11 11
    12 12 12
    13 13 13
    14 14 14
    15 15 15
    16 16 16
    17 17 17
    18 18 18
    19 19 19
    20 20 20
    21 21 21
    22 22 22
    23 23 23
    24 24 24
    25 25 25

I hope this helps.

-Nathan

Subject: a for loop to create several arrays

From: James Franklin

Date: 9 Feb, 2010 00:03:03

Message: 3 of 5

Hi Nathan

thank you!

you are right on several counts.

1. the cell array structure works well

2. i can't name a variable with a number, the letter prefix will be fine though.

however...

it's really important that the script uses the character array for the naming of the data.

Is there a way of converting the character array 'filename' containing '080210.txt' to 'n080210'?

it's important because there is a lot of data!!

good work on the loop there, it was just an example but heck i think i learnt something looking at that too.

James

Subject: a for loop to create several arrays

From: Nathan

Date: 9 Feb, 2010 00:18:07

Message: 4 of 5

On Feb 8, 4:03 pm, "James Franklin" <nos...@hotmail.com> wrote:
> Hi Nathan
>
> thank you!
>
> you are right on several counts.
>
> 1. the cell array structure works well
>
> 2. i can't name a variable with a number, the letter prefix will be fine though.
>
> however...
>
> it's really important that the script uses the character array for the naming of the data.
>
> Is there a way of converting the character array 'filename' containing '080210.txt' to 'n080210'?
>
> it's important because there is a lot of data!!
>
> good work on the loop there, it was just an example but heck i think i learnt something looking at that too.
>
> James

One way to create a variable based on text is to use a struct. Another
way (which most people will tell you not to do) is to use eval. I'll
show you a way to use struct.

%string to become variable:
str = '080210.txt';
%get rid of .txt and add 'n' to beginning
%use regexp to find where the . appears
str = ['n' str(1:regexp(str,'\.')-1)];
% some pretend data
a=[1:100;1:100;1:100];
data=transpose(a);
[length,width]=size (data);
counter=25;
% a pretend for loop for chopping
for rowcounter=1:floor(length/counter);
        counter=counter*rowcounter;
        S.(str){rowcounter} =
data(25*(rowcounter-1)+1:25*(rowcounter),:);
end

S is a struct. To access your data:
S.(str)
ans =
    [25x3 double] [25x3 double] [25x3 double] [25x3 double]
S.(str){1}
ans =
     1 1 1
     2 2 2
     3 3 3
     4 4 4
     5 5 5
     6 6 6
     7 7 7
     8 8 8
     9 9 9
    10 10 10
    11 11 11
    12 12 12
    13 13 13
    14 14 14
    15 15 15
    16 16 16
    17 17 17
    18 18 18
    19 19 19
    20 20 20
    21 21 21
    22 22 22
    23 23 23
    24 24 24
    25 25 25

OR
S.n080210
ans =
    [25x3 double] [25x3 double] [25x3 double] [25x3 double]

I hope this helps.

-Nathan

Subject: a for loop to create several arrays

From: James Franklin

Date: 9 Feb, 2010 00:36:04

Message: 5 of 5

Nathan

That is perfect, thank you for taking the time to reply to my posts. I think structures are going to import to my further work because i will need to recall and search for/within data, a structure will keep it all in one place. I suppose i can just keep filling S with data everyday,I wont ask any more questions.

cheers

James

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
structure James Franklin 8 Feb, 2010 19:41:57
cell array James Franklin 8 Feb, 2010 19:41:43
array loop class James Franklin 8 Feb, 2010 17:49:04
rssFeed for this Thread

Contact us at files@mathworks.com