Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
dlmwrite in a loop

Subject: dlmwrite in a loop

From: Antonio

Date: 15 Aug, 2011 19:08:08

Message: 1 of 5

Hi all,

I'm trying to use the dlmwrite funtion in a loop to write variables from workspace, and the loop I wrote did not work like I want.
The code is like:


for m=1:20;

    filename = ['file' num2str(m) '.dat'];
    var = {'var_' num2str(m)};
    dlmwrite(filename, var, 'delimiter', ' ');
    
end

I want to write each variable, which has a number from 1 to 20 in a file with the same number.

But when I run this part of the code I only save inside the file var_num2str.

Thanks in advance and cheers.

Subject: dlmwrite in a loop

From: jrenfree

Date: 15 Aug, 2011 21:19:35

Message: 2 of 5

On Aug 15, 12:08 pm, "Antonio " <antonio.viudez-m...@nasa.gov> wrote:
> Hi all,
>
> I'm trying to use the dlmwrite funtion in a loop to write variables from workspace, and the loop I wrote did not work like I want.
> The code is like:
>
> for m=1:20;
>
>     filename = ['file' num2str(m) '.dat'];
>     var = {'var_' num2str(m)};
>     dlmwrite(filename, var, 'delimiter', ' ');
>
> end
>
> I want to write each variable, which has a number from 1 to 20 in a file with the same number.
>
> But when I run this part of the code I only save inside the file var_num2str.
>
> Thanks in advance and cheers.

Replace your curly brackets with square brackets:

var = ['var_' num2str(m)];

Subject: dlmwrite in a loop

From: TideMan

Date: 16 Aug, 2011 01:32:20

Message: 3 of 5

On Aug 16, 7:08 am, "Antonio " <antonio.viudez-m...@nasa.gov> wrote:
> Hi all,
>
> I'm trying to use the dlmwrite funtion in a loop to write variables from workspace, and the loop I wrote did not work like I want.
> The code is like:
>
> for m=1:20;
>
>     filename = ['file' num2str(m) '.dat'];
>     var = {'var_' num2str(m)};
>     dlmwrite(filename, var, 'delimiter', ' ');
>
> end
>
> I want to write each variable, which has a number from 1 to 20 in a file with the same number.
>
> But when I run this part of the code I only save inside the file var_num2str.
>
> Thanks in advance and cheers.

This is what comes of using silly variable names like var_1, var_2,
etc
You could have used structures:
s(10).var
or cell arrays
s{10}=var
Then things would be much easier.

Subject: dlmwrite in a loop

From: tonet

Date: 17 Aug, 2011 17:27:10

Message: 4 of 5

jrenfree <jrenfree@gmail.com> wrote in message <656b7304-6d86-41d6-b76c-6ea7af3b78a7@s7g2000yqk.googlegroups.com>...
> On Aug 15, 12:08 pm, "Antonio " <antonio.viudez-m...@nasa.gov> wrote:
> > Hi all,
> >
> > I'm trying to use the dlmwrite funtion in a loop to write variables from workspace, and the loop I wrote did not work like I want.
> > The code is like:
> >
> > for m=1:20;
> >
> >     filename = ['file' num2str(m) '.dat'];
> >     var = {'var_' num2str(m)};
> >     dlmwrite(filename, var, 'delimiter', ' ');
> >
> > end
> >
> > I want to write each variable, which has a number from 1 to 20 in a file with the same number.
> >
> > But when I run this part of the code I only save inside the file var_num2str.
> >
> > Thanks in advance and cheers.
>
> Replace your curly brackets with square brackets:
>
> var = ['var_' num2str(m)];

Hi

Thanks for your comment, but I have done this change at the brackets and did not work.

Thanks

Subject: dlmwrite in a loop

From: tonet

Date: 19 Aug, 2011 20:05:10

Message: 5 of 5

"tonet" wrote in message <j2gtle$nbe$1@newscl01ah.mathworks.com>...
> jrenfree <jrenfree@gmail.com> wrote in message <656b7304-6d86-41d6-b76c-6ea7af3b78a7@s7g2000yqk.googlegroups.com>...
> > On Aug 15, 12:08 pm, "Antonio " <antonio.viudez-m...@nasa.gov> wrote:
> > > Hi all,
> > >
> > > I'm trying to use the dlmwrite funtion in a loop to write variables from workspace, and the loop I wrote did not work like I want.
> > > The code is like:
> > >
> > > for m=1:20;
> > >
> > >     filename = ['file' num2str(m) '.dat'];
> > >     var = {'var_' num2str(m)};
> > >     dlmwrite(filename, var, 'delimiter', ' ');
> > >
> > > end
> > >
> > > I want to write each variable, which has a number from 1 to 20 in a file with the same number.
> > >
> > > But when I run this part of the code I only save inside the file var_num2str.
> > >
> > > Thanks in advance and cheers.
> >
> > Replace your curly brackets with square brackets:
> >
> > var = ['var_' num2str(m)];
>
> Hi
>
> Thanks for your comment, but I have done this change at the brackets and did not work.
>
> Thanks

Finally I found another options to write or save each variable in a file, I found it at

http://www.mathworks.co.uk/matlabcentral/newsreader/view_thread/238239

Thanks

Tags for this Thread

No tags are associated with this thread.

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.

Contact us