Thread Subject: looping dlmwrite with changing filename

Subject: looping dlmwrite with changing filename

From: Linda

Date: 9 Feb, 2012 19:04:13

Message: 1 of 5

I'm trying to subdivide a number of large matrices into 35 smaller ones, and saving them as separate ASCII files. So first, I loop over the 150 large matrices, and then I want to loop over the 35 subdivision within each matrix.

I've got this up to now, loading the big matrices:

for k = 1:150(pats)
  baseFileName = pats(k).name;
  fullFileName = fullfile(fol, baseFileName);
  load fullFileName;
  matrix=MNE_raw.data; %this is the actual matrix
  matrix=double(matrix);

I'm stuck trying to loop over the subdivision and saving. Every submatrix needs to be 4096x142 (the large matrix is 466000x142), and would ideally be saved as an ASCII file with the original name in pats, with an addition for each of the 35 subdivisions. I can only think of doing it manually:

  e1=matrix(1:4096,1:142);
  e2=matrix(4097:8193,1:142);
  e3=matrix(8193:12288,1:142);
  e4=matrix(12289:16384,1:142);
  .....

and then saving manually as well:

 save('origname_e1.txt','e1','-ascii','-tabs');
 ....

And so on. Is there a way to do this using loops?

Thanks!
  
  


 

Subject: looping dlmwrite with changing filename

From: someone

Date: 9 Feb, 2012 20:23:16

Message: 2 of 5

"Linda" wrote in message <jh15bd$fj2$1@newscl01ah.mathworks.com>...
> I'm trying to subdivide a number of large matrices into 35 smaller ones, and saving them as separate ASCII files. So first, I loop over the 150 large matrices, and then I want to loop over the 35 subdivision within each matrix.
>
> I've got this up to now, loading the big matrices:
>
> for k = 1:150(pats)
> baseFileName = pats(k).name;
> fullFileName = fullfile(fol, baseFileName);
> load fullFileName;
> matrix=MNE_raw.data; %this is the actual matrix
> matrix=double(matrix);
>
> I'm stuck trying to loop over the subdivision and saving. Every submatrix needs to be 4096x142 (the large matrix is 466000x142), and would ideally be saved as an ASCII file with the original name in pats, with an addition for each of the 35 subdivisions. I can only think of doing it manually:
>
> e1=matrix(1:4096,1:142);
> e2=matrix(4097:8193,1:142);
I think this should be:
     e2=matrix(4097:8192,1:142);

> e3=matrix(8193:12288,1:142);
> e4=matrix(12289:16384,1:142);
> .....
>
> and then saving manually as well:
>
> save('origname_e1.txt','e1','-ascii','-tabs');
> ....
>
> And so on. Is there a way to do this using loops?

I don't have MATLAB on this computer,
but you might try something like this:

nmax = 466000/4096 % or use something like size(1) in place of 466000
% Are you sure about the 466000?

% This asssumes you don't need to access the "e's" within you code:
for n = 1:nmax
   e=matrix((1 + (n-1)*4096):(n*4096),1:142);
   save(['origname_e' num2str(n) '.txt'],'e','-ascii','-tabs');
end

% If you do, then use something like":
for n = 1:nmax
   e=matrix((1 + (n-1)*4096):(n*4096),1:142);
   save(['origname_e' num2str(n) '.txt'],'e','-ascii','-tabs');
   e{n} = e;
end



>
> Thanks!
>
>
>
>
>

Subject: looping dlmwrite with changing filename

From: Linda

Date: 10 Feb, 2012 00:24:11

Message: 3 of 5

"someone" wrote in message <jh19vk$2d6$1@newscl01ah.mathworks.com>...
> "Linda" wrote in message <jh15bd$fj2$1@newscl01ah.mathworks.com>...
> > I'm trying to subdivide a number of large matrices into 35 smaller ones, and saving them as separate ASCII files. So first, I loop over the 150 large matrices, and then I want to loop over the 35 subdivision within each matrix.
> >
> > I've got this up to now, loading the big matrices:
> >
> > for k = 1:150(pats)
> > baseFileName = pats(k).name;
> > fullFileName = fullfile(fol, baseFileName);
> > load fullFileName;
> > matrix=MNE_raw.data; %this is the actual matrix
> > matrix=double(matrix);
> >
> > I'm stuck trying to loop over the subdivision and saving. Every submatrix needs to be 4096x142 (the large matrix is 466000x142), and would ideally be saved as an ASCII file with the original name in pats, with an addition for each of the 35 subdivisions. I can only think of doing it manually:
> >
> > e1=matrix(1:4096,1:142);
> > e2=matrix(4097:8193,1:142);
> I think this should be:
> e2=matrix(4097:8192,1:142);
>
> > e3=matrix(8193:12288,1:142);
> > e4=matrix(12289:16384,1:142);
> > .....
> >
> > and then saving manually as well:
> >
> > save('origname_e1.txt','e1','-ascii','-tabs');
> > ....
> >
> > And so on. Is there a way to do this using loops?
>
> I don't have MATLAB on this computer,
> but you might try something like this:
>
> nmax = 466000/4096 % or use something like size(1) in place of 466000
> % Are you sure about the 466000?
>
> % This asssumes you don't need to access the "e's" within you code:
> for n = 1:nmax
> e=matrix((1 + (n-1)*4096):(n*4096),1:142);
> save(['origname_e' num2str(n) '.txt'],'e','-ascii','-tabs');
> end
>
> % If you do, then use something like":
> for n = 1:nmax
> e=matrix((1 + (n-1)*4096):(n*4096),1:142);
> save(['origname_e' num2str(n) '.txt'],'e','-ascii','-tabs');
> e{n} = e;
> end

Thanks so much for this solution. I don't need the e's, so I used the first solution, and it works for the most part.
One problem remains, which I probably wasn't clear on. I would like the 'origname' to take the baseFileName defined before, but I only get files called baseFileName_e1.txt etc. How can I fix this?

Subject: looping dlmwrite with changing filename

From: someone

Date: 10 Feb, 2012 03:53:18

Message: 4 of 5

"Linda" wrote in message <jh1o3b$hp9$1@newscl01ah.mathworks.com>...
> "someone" wrote in message <jh19vk$2d6$1@newscl01ah.mathworks.com>...
> > "Linda" wrote in message <jh15bd$fj2$1@newscl01ah.mathworks.com>...
> > > I'm trying to subdivide a number of large matrices into 35 smaller ones, and saving them as separate ASCII files. So first, I loop over the 150 large matrices, and then I want to loop over the 35 subdivision within each matrix.
> > >
> > > I've got this up to now, loading the big matrices:
> > >
> > > for k = 1:150(pats)
> > > baseFileName = pats(k).name;
> > > fullFileName = fullfile(fol, baseFileName);
> > > load fullFileName;
> > > matrix=MNE_raw.data; %this is the actual matrix
> > > matrix=double(matrix);
> > >
> > > I'm stuck trying to loop over the subdivision and saving. Every submatrix needs to be 4096x142 (the large matrix is 466000x142), and would ideally be saved as an ASCII file with the original name in pats, with an addition for each of the 35 subdivisions. I can only think of doing it manually:
> > >
> > > e1=matrix(1:4096,1:142);
> > > e2=matrix(4097:8193,1:142);
> > I think this should be:
> > e2=matrix(4097:8192,1:142);
> >
> > > e3=matrix(8193:12288,1:142);
> > > e4=matrix(12289:16384,1:142);
> > > .....
> > >
> > > and then saving manually as well:
> > >
> > > save('origname_e1.txt','e1','-ascii','-tabs');
> > > ....
> > >
> > > And so on. Is there a way to do this using loops?
> >
> > I don't have MATLAB on this computer,
> > but you might try something like this:
> >
> > nmax = 466000/4096 % or use something like size(1) in place of 466000
> > % Are you sure about the 466000?
> >
> > % This asssumes you don't need to access the "e's" within you code:
> > for n = 1:nmax
> > e=matrix((1 + (n-1)*4096):(n*4096),1:142);
> > save(['origname_e' num2str(n) '.txt'],'e','-ascii','-tabs');
> > end
> >
> > % If you do, then use something like":
> > for n = 1:nmax
> > e=matrix((1 + (n-1)*4096):(n*4096),1:142);
> > save(['origname_e' num2str(n) '.txt'],'e','-ascii','-tabs');
> > e{n} = e;
> > end
>
> Thanks so much for this solution. I don't need the e's, so I used the first solution, and it works for the most part.
> One problem remains, which I probably wasn't clear on. I would like the 'origname' to take the baseFileName defined before, but I only get files called baseFileName_e1.txt etc. How can I fix this?

You should be able to replace:

save(['origname_e' num2str(n) '.txt'],'e','-ascii','-tabs');

with:

save([baseFileName '_e' num2str(n) '.txt'],'e','-ascii','-tabs');

Subject: looping dlmwrite with changing filename

From: Linda

Date: 10 Feb, 2012 14:09:10

Message: 5 of 5

Fantastic, thanks!

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