|
"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
|