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:
saving data in a loop

Subject: saving data in a loop

From: Dawei Liu

Date: 18 Aug, 2009 12:18:20

Message: 1 of 3

Hi,
I was wandering if anyone know how to save a one dimenional matrix as 2 dimensional matrix in a loop. The code is as below, but only the last one is saved. I want "all.txt" is a matrix 100*100.

name='all.txt';
for i=1:100
a=rand(1:100,1);
save(name,'a','-ascii');
end

Any help will be greatly appreciated.
David

Subject: saving data in a loop

From: Miroslav Balda

Date: 18 Aug, 2009 20:58:04

Message: 2 of 3

"Dawei Liu" <ldw636@msn.com> wrote in message <h6e66c$l0c$1@fred.mathworks.com>...
> Hi,
> I was wandering if anyone know how to save a one dimenional matrix as 2 dimensional matrix in a loop. The code is as below, but only the last one is saved. I want "all.txt" is a matrix 100*100.
>
> name='all.txt';
> for i=1:100
> a=rand(1:100,1);
> save(name,'a','-ascii');
> end
>
> Any help will be greatly appreciated.
> David

Hi David
Your code is wrong., because
1. a(1:100,1) has 1st argument vector, however it should be scalar, hence rand(100,1) is a proper expression.
2. if the code is repaired to rand(100,1), function save generates text file 'all.txt', which will be rewritten by a new file of the same name for each i. This is the reason why you obtained only the last vecror of written data, if you loaded it ,say, bythe statement:
b = load(name)

If you want to record all vectors in text file, you have to run this:

name='all.txt';
fid = fopen(name,'a'); % see help fopen
FORMAT = '%g '; % see help fprintf for FORMAT string
n = 100;
for i=1:n
     a=rand(n,1);
     fprintf(fid,FORMAT,a);
     fprintf(fid,'\n');
end
fclose(fid);

A = zeros(n);
A = load(name)';
size(A) % it will be 100 100
delete('all.txt')

Hope it helps.

Mira

Subject: saving data in a loop

From: Dawei Liu

Date: 19 Aug, 2009 15:46:04

Message: 3 of 3

"Miroslav Balda" <miroslav.nospam@balda.cz> wrote in message <h6f4ks$2c1$1@fred.mathworks.com>...
> "Dawei Liu" <ldw636@msn.com> wrote in message <h6e66c$l0c$1@fred.mathworks.com>...
> > Hi,
> > I was wandering if anyone know how to save a one dimenional matrix as 2 dimensional matrix in a loop. The code is as below, but only the last one is saved. I want "all.txt" is a matrix 100*100.
> >
> > name='all.txt';
> > for i=1:100
> > a=rand(1:100,1);
> > save(name,'a','-ascii');
> > end
> >
> > Any help will be greatly appreciated.
> > David
>
> Hi David
> Your code is wrong., because
> 1. a(1:100,1) has 1st argument vector, however it should be scalar, hence rand(100,1) is a proper expression.
> 2. if the code is repaired to rand(100,1), function save generates text file 'all.txt', which will be rewritten by a new file of the same name for each i. This is the reason why you obtained only the last vecror of written data, if you loaded it ,say, bythe statement:
> b = load(name)
>
> If you want to record all vectors in text file, you have to run this:
>
> name='all.txt';
> fid = fopen(name,'a'); % see help fopen
> FORMAT = '%g '; % see help fprintf for FORMAT string
> n = 100;
> for i=1:n
> a=rand(n,1);
> fprintf(fid,FORMAT,a);
> fprintf(fid,'\n');
> end
> fclose(fid);
>
> A = zeros(n);
> A = load(name)';
> size(A) % it will be 100 100
> delete('all.txt')
>
> Hope it helps.
>
> Mira

Hi Mira,
The problem is solved by your suggestion.
Thanks,
David

Tags for 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