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:
create and save 3D matrix

Subject: create and save 3D matrix

From: Tobias

Date: 9 Sep, 2010 15:01:38

Message: 1 of 6

Hi,

I am stuck in creating a 3 d matrix automatically and save after its finished.

I want to read in several thousands of txt. files, reshape them and build a 3D Matrix where each text file is one layer in the 3D matrix

My code so far:

pr = dir('*_u.txt');
names = {pr.name};
numfiles = numel(names);
for i = 1:numfiles
    n(i) = n+1;
    xx = textread(names{1,i});
    xx = reshape(xx,325,325);
    uvwnd(i) = (xx(:,1),xx(1,:),n(i)); <--- does not work
end

Can anybody help me with that?

Subject: create and save 3D matrix

From: someone

Date: 9 Sep, 2010 15:13:04

Message: 2 of 6

"Tobias " <tobias.aigner@elkraft.ntnu.no> wrote in message <i6assh$2ek$1@fred.mathworks.com>...
> Hi,
>
> I am stuck in creating a 3 d matrix automatically and save after its finished.
>
> I want to read in several thousands of txt. files, reshape them and build a 3D Matrix where each text file is one layer in the 3D matrix
>
> My code so far:
>
> pr = dir('*_u.txt');
> names = {pr.name};
> numfiles = numel(names);
> for i = 1:numfiles
> n(i) = n+1;
> xx = textread(names{1,i});
> xx = reshape(xx,325,325);
> uvwnd(i) = (xx(:,1),xx(1,:),n(i)); <--- does not work
> end
>
> Can anybody help me with that?

What is n? Where does it get initialized?

The line:
> n(i) = n+1;
does not make any sense!

Subject: create and save 3D matrix

From: Sean

Date: 9 Sep, 2010 15:17:05

Message: 3 of 6

"Tobias " <tobias.aigner@elkraft.ntnu.no> wrote in message <i6assh$2ek$1@fred.mathworks.com>...
> Hi,
>
> I am stuck in creating a 3 d matrix automatically and save after its finished.
>
> I want to read in several thousands of txt. files, reshape them and build a 3D Matrix where each text file is one layer in the 3D matrix
>
> My code so far:
>
> pr = dir('*_u.txt');
> names = {pr.name};
> numfiles = numel(names);
> for i = 1:numfiles
> n(i) = n+1;
> xx = textread(names{1,i});
> xx = reshape(xx,325,325);
> uvwnd(i) = (xx(:,1),xx(1,:),n(i)); <--- does not work
> end
>
> Can anybody help me with that?

One way:
unwnd = zeros(325,325,numfiles)
for ii = 1:numfiles %use ii since i is the sqrt(-1)
     unwnd(:,:,ii) = reshape( textread(names{1,ii})325,325); %set all rows and columns of the iith slice to the new matrix
end

%SCd

Subject: create and save 3D matrix

From: Tobias

Date: 9 Sep, 2010 15:24:21

Message: 4 of 6

"someone" <someone@somewhere.net> wrote in message <i6ati0$feo$1@fred.mathworks.com>...
> "Tobias " <tobias.aigner@elkraft.ntnu.no> wrote in message <i6assh$2ek$1@fred.mathworks.com>...
> > Hi,
> >
> > I am stuck in creating a 3 d matrix automatically and save after its finished.
> >
> > I want to read in several thousands of txt. files, reshape them and build a 3D Matrix where each text file is one layer in the 3D matrix
> >
> > My code so far:
> >
> > pr = dir('*_u.txt');
> > names = {pr.name};
> > numfiles = numel(names);
> > for i = 1:numfiles
> > n(i) = n+1;
> > xx = textread(names{1,i});
> > xx = reshape(xx,325,325);
> > uvwnd(i) = (xx(:,1),xx(1,:),n(i)); <--- does not work
> > end
> >
> > Can anybody help me with that?
>
> What is n? Where does it get initialized?
>
> The line:
> > n(i) = n+1;
> does not make any sense!

thanks of the answer.

Then skip the n.

pr = dir('*_u.txt');
names = {pr.name};
numfiles = numel(names);
for i = 1:numfiles
    xx = textread(names{1,i});
    xx = reshape(xx,325,325);
  end

How can I stack my reshaped matrix xx??
So what I want is a 3D matrix (xx,number of files)
Anybody an idea?

Subject: create and save 3D matrix

From: Walter Roberson

Date: 9 Sep, 2010 18:42:09

Message: 5 of 6

On 10-09-09 10:01 AM, Tobias wrote:
> Hi,
>
> I am stuck in creating a 3 d matrix automatically and save after its
> finished.
>
> I want to read in several thousands of txt. files, reshape them and
> build a 3D Matrix where each text file is one layer in the 3D matrix
>
> My code so far:
>
> pr = dir('*_u.txt');
> names = {pr.name};
> numfiles = numel(names);
> for i = 1:numfiles
> n(i) = n+1;
> xx = textread(names{1,i});
> xx = reshape(xx,325,325);
> uvwnd(i) = (xx(:,1),xx(1,:),n(i)); <--- does not work
> end
>
> Can anybody help me with that?

You probably want to use cat(3, ...) to add the new layer on to the existing
layers.

Subject: create and save 3D matrix

From: Tobias

Date: 9 Sep, 2010 18:45:21

Message: 6 of 6

"Sean " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <i6atph$17r$1@fred.mathworks.com>...
> "Tobias " <tobias.aigner@elkraft.ntnu.no> wrote in message <i6assh$2ek$1@fred.mathworks.com>...
> > Hi,
> >
> > I am stuck in creating a 3 d matrix automatically and save after its finished.
> >
> > I want to read in several thousands of txt. files, reshape them and build a 3D Matrix where each text file is one layer in the 3D matrix
> >
> > My code so far:
> >
> > pr = dir('*_u.txt');
> > names = {pr.name};
> > numfiles = numel(names);
> > for i = 1:numfiles
> > n(i) = n+1;
> > xx = textread(names{1,i});
> > xx = reshape(xx,325,325);
> > uvwnd(i) = (xx(:,1),xx(1,:),n(i)); <--- does not work
> > end
> >
> > Can anybody help me with that?
>
> One way:
> unwnd = zeros(325,325,numfiles)
> for ii = 1:numfiles %use ii since i is the sqrt(-1)
> unwnd(:,:,ii) = reshape( textread(names{1,ii})325,325); %set all rows and columns of the iith slice to the new matrix
> end
>
> %SCd

Hey Sean,

thank you very much!!

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