replace spaces in txt file

  • Follow


I would like to replace the spaces in this txt file

a3;c;R06;7 7 7 NaN NaN NaN NaN
a4;d;R12;2 3 1 NaN NaN NaN NaN

with semicolons so it looks like this

a3;c;R06;7;7;7;NaN;NaN;NaN;NaN
a4;d;R12;2;3;1;NaN;NaN;NaN;NaN

I suppose it is a quite simple command statement but I can't find it.
Who does?

Thanks in advance, Ton
0
Reply t.schomaker (31) 8/17/2012 3:32:10 PM

fid=fopen(sourcefile,'r');
c=fread(fid,Inf,'char*1');
fclose(fid);

c(c==32)=';';

fid=fopen(destfile,'w');
fwrite(fid,c,'char*1');
fclose(fid);

% Bruno
0
Reply b.luong5955 (6359) 8/17/2012 4:23:09 PM


"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <k0lr5d$72f$1@newscl01ah.mathworks.com>...
> fid=fopen(sourcefile,'r');
> c=fread(fid,Inf,'char*1');
> fclose(fid);
> 
> c(c==32)=';';
> 
> fid=fopen(destfile,'w');
> fwrite(fid,c,'char*1');
> fclose(fid);
> 
> % Bruno

Thanks a lot Bruno,

It works fine.
Is there a way to restrict the number of spaces I want to replace?
Since the header (not given above) is much larger I get a trail of semi colons at each line I don't want (e.g.  '.....NaN;NaN;;;;;;;;;;;;;;')

Do you have a work around for this also?

Thanks in advance, Ton
0
Reply t.schomaker (31) 8/17/2012 4:48:05 PM

c = sprintf('ae;fe;afea;;;\nfeafeaf;feafafe;faef;;;;\n');
% So, you can just do:
regexprep(c, ';;+', ';')
0
Reply jamesbejon (121) 8/17/2012 5:02:11 PM

On 8/17/2012 11:23 AM, Bruno Luong wrote:
> fid=fopen(sourcefile,'r');
> c=fread(fid,Inf,'char*1');
> fclose(fid);
>
> c(c==32)=';';
>
> fid=fopen(destfile,'w');
> fwrite(fid,c,'char*1');
> fclose(fid);

Accomplishes same thing but more "stringy-looking" if prefer is

c=fread(fid,Inf,'char*1');
strrep(c,' ',';');
....

--
0
Reply none1568 (6816) 8/17/2012 6:38:20 PM

On 8/17/2012 11:48 AM, Ton Schomaker wrote:
....

> Is there a way to restrict the number of spaces I want to replace?
> Since the header (not given above) is much larger I get a trail of semi
> colons at each line I don't want (e.g. '.....NaN;NaN;;;;;;;;;;;;;;')
>
> Do you have a work around for this also?

I missed the second question entirely...

Unless you need the trailing blanks,

strrep(deblank(c),' ',';');

If you do, simplest is to probably find the last 'NaN' location and 
operate only on the substring of length required.

As noted earlier, this does same thing as the "==" solution Bruno posted 
but looks more "string-like" and doesn't require remembering the ASCII 
code for a blank as a small side benefit.

doc strfun  % for string manipulation functions (w/o repmat :( )

--
0
Reply none1568 (6816) 8/18/2012 7:48:19 PM

On 8/17/2012 11:48 AM, Ton Schomaker wrote:
....

> Is there a way to restrict the number of spaces I want to replace?
> Since the header (not given above) is much larger I get a trail of semi
> colons at each line I don't want (e.g. '.....NaN;NaN;;;;;;;;;;;;;;')
>
> Do you have a work around for this also?

I missed the second question entirely...

Unless you need the trailing blanks,

strrep(deblank(c),' ',';');

If you do, simplest is to probably find the last 'NaN' location and 
operate only on the substring of length required.

As noted earlier, this does same thing as the "==" solution Bruno posted 
but looks more "string-like" and doesn't require remembering the ASCII 
code for a blank as a small side benefit.

doc strfun  % for string manipulation functions (w/o repmat :( )

--
0
Reply none1568 (6816) 8/18/2012 7:56:28 PM

On 8/18/2012 2:48 PM, dpb wrote:
....

> strrep(deblank(c),' ',';');

Oh, to use the shorthand version w/o a temporary to store the result of 
deblank(c) you need the functional output form for strrep()...

c=strrep(deblank(c),' ',';');

--


0
Reply none1568 (6816) 8/18/2012 9:09:21 PM

On 8/18/2012 2:48 PM, dpb wrote:
....

> Unless you need the trailing blanks,
>
> strrep(deblank(c),' ',';');
>
> If you do, simplest is to probably find the last 'NaN' location and
> operate only on the substring of length required.
....

Excepting that's contingent on a per-line basis.  For a whole file 
regular expression is probably the better route as another poster 
suggested...

Of course, wouldn't be complete if didn't note the simplest way would be 
to generate the files as wanted to begin with... :)

--
0
Reply none1568 (6816) 8/19/2012 1:29:33 PM

8 Replies
30 Views

(page loaded in 0.191 seconds)


Reply: