Cell Array to .txt file

3 次查看(过去 30 天)
Hi,
I am working on importing a .txt file, altering some of the text inside of it then, writing it to a new .txt file.
I import the .txt file as a cell array, perform my math then, attempt to write it back to a new .txt file.
The text I am attempting to alter is CNC code. Therefore, at the end of each line I need a semi-colon.
This is my code:
fid = fopen('newpartcode.txt','w');
for i=1:m
fprintf(fid,'%s',pcn{i,:});
fprintf(fid,';');
fprintf(fid,'\n');
end
fclose(fid);
The results I am getting look like:
%;O0000;G20;G0G17G40G49G80G90;T1M6;
When I need it to create a new line after each semi-colon
Any help is welcome

采纳的回答

per isakson
per isakson 2013-7-31
编辑:per isakson 2013-8-1
Try
str = '%;O0000;G20;G0G17G40G49G80G90;T1M6;';
cac = regexp( str, ';', 'split' );
str = sprintf( '%s;\n', cac{1:end-1} );
str
it returns
%;
O0000;
G20;
G0G17G40G49G80G90;
T1M6;
or better replace
fprintf(fid,'%s',pcn{i,:});
fprintf(fid,';');
fprintf(fid,'\n');
by
fprintf(fid,'%s;\n',pcn{i,:});
Is
%;O0000;G20;G0G17G40G49G80G90;T1M6;
how it looks in Notepad? You might want to replace '\n' by '\n\r' - mistake: it should be \r\n.

更多回答(2 个)

Jan
Jan 2013-8-1
fid = fopen('newpartcode.txt','w');
pcnT = pcn.';
fprintf(fid,'%s;\n', pcn{:});
fclose(fid);
Now there are linebreaks behind each semicolon, but you cannot see them, when the file is opened by the childish NotePad ("Editor" in German). This program insists on the old DOS linebreaks '\r\n' since Windows 3.1. All other editors can handle the \n linebreaks correctly.
In your case it matters, which kind of linebreaks the CNC machine expects. So please find out this important detail and install NotePad++ or use WordPad as standard viewer for txt files.
  1 个评论
Paul Anderson
Paul Anderson 2013-8-1
编辑:Paul Anderson 2013-8-1
Jan,
Thank you for the help! I have a question for your though. What does the ".'" at the end of the pcnT=pcn.' statement do?

请先登录,再进行评论。


Paul Anderson
Paul Anderson 2013-8-1
Guys,
Thank you both for the help. Has anyone found a setting the can be changed in Notepad that would allow the code to be displayed correctly?
  1 个评论
per isakson
per isakson 2013-8-1
There is no such setting in NotePad. Why do you use NotePad? Matlabs editor is better. The free NotePad++ is a good replacement for NotePad.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Desktop 的更多信息

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by