Thread Subject: load txt with different row length

Subject: load txt with different row length

From: Corinna Schmitt

Date: 18 Jan, 2008 08:28:01

Message: 1 of 8

Hallo,
I have an inputfile which is stored as a txt tab-delimited.
The problem is that the input consists of n columns and m
rows where each row has not the same number of columns. How
can I load this into matlab if possible in a cell array.
How can I than sort each row?

Here is an example:

Input:
1 o1.2 ABC o6.5 GAD o1.6
2 GAD o1.9 o6.3

sorted output:
1 o1.2 o1.6 o6.5 ABC GAD
2 o1.9 o6.3 GAD

How can I than store this as a txt file?


Thanks, Corinna

Subject: load txt with different row length

From: Kris De Gussem

Date: 18 Jan, 2008 09:42:57

Message: 2 of 8

> Input:
> 1 o1.2 ABC o6.5 GAD o1.6
> 2 GAD o1.9 o6.3
>
> sorted output:
> 1 o1.2 o1.6 o6.5 ABC GAD
> 2 o1.9 o6.3 GAD
>
> How can I than store this as a txt file?
>
>
> Thanks, Corinna

You should have a look at the loadcell tool on the matlab file exchange
server:

http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=1965&objectType=file

This is optimized for tables though, so I guess that you need to
transform the resulting 2D cell array to a 1D cell array afterwards (and
remove the empty or zeros fields). An alternative is the readtext function:

http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=10946&objectType=FILE

Kris

Subject: load txt with different row length

From: Corinna Schmitt

Date: 18 Jan, 2008 12:29:02

Message: 3 of 8

Hallo Kris,

sorry, I cannot run those programs. Once the pc says he
cannot find the matlab-file but I copied it in the right
folder. otherwise he said input is wrong. Can you give me
another solution.

The input is a txt-delimited files and the delimiter
between entries is a tab.

> > Input:
> > 1 o1.2 ABC o6.5 GAD o1.6
> > 2 GAD o1.9 o6.3
> >
> > sorted output:
> > 1 o1.2 o1.6 o6.5 ABC GAD
> > 2 o1.9 o6.3 GAD
> >
> > How can I than store this as a txt file?

Any idea?

Corinna



Subject: load txt with different row length

From: Kris De Gussem

Date: 18 Jan, 2008 15:25:22

Message: 4 of 8

> otherwise he said input is wrong.

What do you mean? Which function fails? I always use something like:
[data,b,numdata]=loadcell ('c:\data.txt', ['"' char(9)], '');

or
[data,b,numdata]=loadcell('c:\data.txt', ['"' char(9)], 'single-string')

%convert input to strings
[p1,p2]=find(isnan(numdata) == false);
for i=1:length(p1)
data{p1(i),p2(i)} = num2str (data{p1(i),p2(i)});
end

%sorted output (2D cell array)
for i=1:size(data,1)
     p=find (numdata(i,:) == 0); %find the empty items
     tmp=data(i,:);
     tmp(p)=[];
     data2{i}=sort(tmp);
end


> The input is a txt-delimited files and the delimiter
> between entries is a tab.
>
>>> Input:
>>> 1 o1.2 ABC o6.5 GAD o1.6
>>> 2 GAD o1.9 o6.3
>>>
>>> sorted output:
>>> 1 o1.2 o1.6 o6.5 ABC GAD
>>> 2 o1.9 o6.3 GAD
>>>
>>> How can I than store this as a txt file?

[f,msg] = fopen('c:\data2.txt', 'wb', 'l');
for i=1:size(data2,2);
fprintf(f, '%s\t', data2{i}{:})
fprintf(f, '\n');
end
fclose(f);

Subject: load txt with different row length

From: Corinna Schmitt

Date: 18 Jan, 2008 15:43:03

Message: 5 of 8

Hallo Kris,

I found another solution. Now I tried to store my
resultfile (dimension 8x11 cell) as a tab-delimited
textfile with save but it doesn't work.

EDU>> save 'ergebnis.txt' 'result' -ascii
Warning: Attempt to write an unsupported data type to an
ASCII file.
Variable 'result' not written to file.

Also your solution does not work
  
> [f,msg] = fopen('c:\data2.txt', 'wb', 'l');
> for i=1:size(data2,2)
> fprintf(f, '\n');
> end
> fclose(f);

Have you got an alternative?

Thanks, Corinna

Subject: load txt with different row length

From: Corinna Schmitt

Date: 18 Jan, 2008 15:58:02

Message: 6 of 8

Hallo,

here is the error message:

EDU>> [f,msg] = fopen('data2.txt', 'wb', 'l');
EDU>> for i=1:size(result,2)
fprintf(f, '%s\t', result{i}{:})
fprintf(f, '\n')
end
??? Cell contents reference from a non-cell array object.


What means this?

Corinna

Subject: load txt with different row length

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 18 Jan, 2008 16:50:26

Message: 7 of 8

In article <fmqia9$heh$1@fred.mathworks.com>,
Corinna Schmitt <csc@igb.fhg.de> wrote:

>here is the error message:

>EDU>> [f,msg] = fopen('data2.txt', 'wb', 'l');
>EDU>> for i=1:size(result,2)
>fprintf(f, '%s\t', result{i}{:})
>fprintf(f, '\n')
>end
>??? Cell contents reference from a non-cell array object.

>What means this?

You have not given a value to result. You open the file but
you do not read from the file.
--
We regret to announce that sub-millibarn resolution bio-hyperdimensional
plasmatic space polyimaging has been delayed until the release
of Windows Vista SP2.

Subject: load txt with different row length

From: hazenz@gmail.com

Date: 1 Feb, 2008 21:18:43

Message: 8 of 8

On Jan 18, 10:50=A0am, rober...@ibd.nrc-cnrc.gc.ca (Walter Roberson)
wrote:
> In article <fmqia9$he...@fred.mathworks.com>,
>
> Corinna Schmitt <c...@igb.fhg.de> wrote:
> >here is the error message:
> >EDU>> [f,msg] =3D fopen('data2.txt', 'wb', 'l');
> >EDU>> for i=3D1:size(result,2)
> >fprintf(f, '%s\t', result{i}{:})
> >fprintf(f, '\n')
> >end
> >??? Cell contents reference from a non-cell array object.
> >What means this?
>
> You have not given a value to result. You open the file but
> you do not read from the file.
> --
> We regret to announce that sub-millibarn resolution bio-hyperdimensional
> plasmatic space polyimaging has been delayed until the release
> of Windows Vista SP2.


Maybe something like this? This one reads all odd rows with 5
arguments and even rows with even rows with 8. If there is a pattern
to how many arguments are in each row, this should work fine.

fid =3D fopen('filename.txt');
for i =3D 1:2:362
    rawdat{i,:} =3D textscan(fid,'%n %n %n %s %s',1);
    rawdat{i+1,:} =3D textscan(fid,'%n %n %n %n %n %n %n %s',1);
end
fclose(fid);

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

Public Submission Policy

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.

Contact us at files@mathworks.com