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:44:02

Message: 1 of 2

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: Dave Brackett

Date: 29 Jul, 2009 22:19:02

Message: 2 of 2

"Pierre Queloz" <pierre.queloz@gmail.com> wrote in message <h4p5l2$h89$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


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

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