Thread Subject: Turn this into a for loop

Subject: Turn this into a for loop

From: ufellc@gmail.com

Date: 20 Mar, 2009 03:13:27

Message: 1 of 5

How would I turn this into a for loop to save the data into 3 separate
files named CmAhg1, CmAhg2, CmAhg3
///////////////////////////////////////////////////

m=0;
for a=1:1:length(CmAh);

    if CmAh(a,1)~=0;
        m=m+1;
        CmAhg1(m,1)=CmAh(a,1);

    end
end
n=0;

for a=1:1:length(CmAh);

   if CmAh(a,2)~=0
       n=n+1;
        CmAhg2(n,1)=CmAh(a,2);

    end
end
o=0;
for a=1:1:length(CmAh);
    if CmAh(a,3)~=0
        o=o+1;
        CmAhg3(o,1)=CmAh(a,3);

    end
end

Subject: Turn this into a for loop

From: Aaron Callard

Date: 20 Mar, 2009 13:06:02

Message: 2 of 5

ufellc@gmail.com wrote in message <e62cdfec-2edd-4825-a887-ddbcb676b0f7@j35g2000yqh.googlegroups.com>...
> How would I turn this into a for loop to save the data into 3 separate
> files named CmAhg1, CmAhg2, CmAhg3
> ///////////////////////////////////////////////////
>
> m=0;
> for a=1:1:length(CmAh);
>
> if CmAh(a,1)~=0;
> m=m+1;
> CmAhg1(m,1)=CmAh(a,1);
>
> end
> end
> n=0;
>
> for a=1:1:length(CmAh);
>
> if CmAh(a,2)~=0
> n=n+1;
> CmAhg2(n,1)=CmAh(a,2);
>
> end
> end
> o=0;
> for a=1:1:length(CmAh);
> if CmAh(a,3)~=0
> o=o+1;
> CmAhg3(o,1)=CmAh(a,3);
>
> end
> end

um it is a for loop what's the issue. Of course this just takes out the non zero entries so it could be a one liner
CmAhg1=CmAh(CmAh(:,1)>0,1);
etc

Subject: Turn this into a for loop

From: ufellc@gmail.com

Date: 20 Mar, 2009 13:26:33

Message: 3 of 5

Hello
Thanks your code greatly condensed mine. But it is not quite what I
had in mind.
 For example: say I have 10 columns in CmAh and want to save each
column without the zeros in CmAh into a new file CmAhg1,
CmAhg2...CmAhg10. How do I use a loop to create those 10 variables?

Subject: Turn this into a for loop

From: psdeluxe

Date: 20 Mar, 2009 13:49:34

Message: 4 of 5

You can learn about Adobe photoshop in http://www.psdeluxe.com

Subject: Turn this into a for loop

From: Ashish Uthama

Date: 23 Mar, 2009 15:24:53

Message: 5 of 5

On Fri, 20 Mar 2009 09:26:33 -0400, <ufellc@gmail.com> wrote:

> Hello
> Thanks your code greatly condensed mine. But it is not quite what I
> had in mind.
> For example: say I have 10 columns in CmAh and want to save each
> column without the zeros in CmAh into a new file CmAhg1,
> CmAhg2...CmAhg10. How do I use a loop to create those 10 variables?


Create the file name dynamically.

Example:

for ind=1:5
  data=ind*10; %some randdom data
  fileName=['myDataFile_',num2str(ind),'.mat']; %creates file names like
myDataFile_1.mat... myDataFile_5.mat
  save(fileName,'data'); %save the variable data into the named MAT file
end

Tags for this Thread

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.

rssFeed for this Thread

Contact us at files@mathworks.com