Got Questions? Get Answers.
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:
FILEREAD and additional blank lines

Subject: FILEREAD and additional blank lines

From: Pierre Queloz

Date: 29 Jul, 2009 09:41:02

Message: 1 of 4

Hello,

I'm using 'fileread' in order to import a .m file as a string, then replace some characters in it and rewrite it into a .m file. The problem is that when I import the .m file with 'fileread', it adds a blank line after each end of line. I end up with a file completely "spread".
Does anybody have a clue to avoid this problem?
Thanks in advance, cheers

Pierre

Subject: FILEREAD and additional blank lines

From: Shanmugam Kannappan

Date: 29 Jul, 2009 10:04:01

Message: 2 of 4

"Pierre Queloz" <pierre.queloz@gmail.com> wrote in message <h4p5fe$6h6$1@fred.mathworks.com>...
> Hello,
>
> I'm using 'fileread' in order to import a .m file as a string, then replace some characters in it and rewrite it into a .m file. The problem is that when I import the .m file with 'fileread', it adds a blank line after each end of line. I end up with a file completely "spread".
> Does anybody have a clue to avoid this problem?
> Thanks in advance, cheers
>
> Pierre
Hi!
Try using fopen & replace the string using regular expression command.......
Shan........

Subject: FILEREAD and additional blank lines

From: Rune Allnor

Date: 29 Jul, 2009 10:37:04

Message: 3 of 4

On 29 Jul, 11:41, "Pierre Queloz" <pierre.que...@gmail.com> wrote:
> Hello,
>
> I'm using 'fileread'

Why?

> in order to import a .m file as a string, then replace some characters in it and rewrite it into a .m file. The problem is that when I import the .m file with 'fileread', it adds a blank line after each end of line. I end up with a file completely "spread".
> Does anybody have a clue to avoid this problem?

Use FGETL and do the conversions on a line-by-line
basis. M-files are small, so any gains in speed by
using FILEREAD are on the order of milliseconds.

Rune

Subject: FILEREAD and additional blank lines

From: Dave Brackett

Date: 29 Jul, 2009 22:28:02

Message: 4 of 4

Rune Allnor <allnor@tele.ntnu.no> wrote in message <89d040e5-21ad-4169-995e-ed47687d7101@e11g2000yqo.googlegroups.com>...
> On 29 Jul, 11:41, "Pierre Queloz" <pierre.que...@gmail.com> wrote:
> > Hello,
> >
> > I'm using 'fileread'
>
> Why?
>
> > in order to import a .m file as a string, then replace some characters in it and rewrite it into a .m file. The problem is that when I import the .m file with 'fileread', it adds a blank line after each end of line. I end up with a file completely "spread".
> > Does anybody have a clue to avoid this problem?
>
> Use FGETL and do the conversions on a line-by-line
> basis. M-files are small, so any gains in speed by
> using FILEREAD are on the order of milliseconds.
>
> Rune


You have posted this query twice so I thought I would post my response to the other one here as well:

How about using textscan instead of fileread. It's a bit more flexible. The altered file can then be written using fprintf. There may be a better way to write this file but this way works.

% read in .m file
fid=fopen('example.m','r');
a=textscan(fid,'%s','delimiter','','whitespace','');
fclose(fid);

% make the alterations to 'a' here.

% write to new .m file
fid=fopen('altered_example3.m','a');
for i=1:size(a{1},1)
    fprintf(fid,'%s',a{1}{i});
    fprintf(fid,'\n');
end
fclose(fid);

hope that helps.

Tags for 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