fprintf(fid, xml) 报错记录

本文详细探讨了在MATLAB环境下写入XML文件时遇到的“Invalid escape sequence”错误,通过实例分析,指出问题源于对文件夹路径中转义字符的不当处理,并提供了排查与修正的具体步骤。

 

writeXML(fullfile(HOMEfolderAnnotationLM,nameXML), xml); 函数下的fprintf(fid, xml) 报错

>> Warning: Invalid escape sequence appears in format string. See help sprintf
for valid escape sequences. 
警告:在格式化string中存在不合法的转义序列。输入help sprintf查看合法的转义序列。

个人的问题出现在写入xml文件时总是报错“Invalid escape sequence appears in format string”,理解为无效的字符串序列。而所谓的不合法的转义序列则表示写入过程中的               “\”         不能被识别,因此在生成xml文件中去具体查看xml的内容哪里出现了 “\”  ,然后修改之。

 个人费了很长时间才更深入的发现folder指的的文件夹,不是路径名。 (真是个逻辑糊脑袋)

 


 
function convkml(infile, outfile, ts, te, tint, qflg, offset, tcolor, pcolor, outalt, outtime) % Constants SIZP = 0.2; % Mark size of rover positions SIZR = 0.3; % Mark size of reference position TINT = 30.0; % Time label interval (sec) HEADKML1 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; HEADKML2 = "<kml xmlns=\"http://earth.google.com/kml/2.1\">"; MARKICON = "http://maps.google.com/mapfiles/kml/pal2/icon18.png"; % Default output file name if not provided if isempty(outfile) [~, name] = fileparts(infile); outfile = [name '.kml']; end % Read solution file solbuf = readsolt(infile, ts, te, tint, qflg); % Mean position rr = mean(solbuf.data.rr, 1); % Add offset pos = ecef2pos(rr); dr = enu2ecef(pos, offset); for i = 1:length(solbuf.data) solbuf.data(i).rr = solbuf.data(i).rr + dr; end % Save KML file savekml(outfile, solbuf, tcolor, pcolor, outalt, outtime); end function savekml(file, solbuf, tcolor, pcolor, outalt, outtime) % Open file for writing fid = fopen(file, 'w'); if fid == -1 error('File open error: %s', file); end % Write KML header fprintf(fid, '%s\n%s\n', HEADKML1, HEADKML2); fprintf(fid, '<Document>\n'); % Define styles colors = {'ffffffff', 'ff008800', 'ff00aaff', 'ff0000ff', 'ff00ffff', 'ffff00ff'}; for i = 1:6 fprintf(fid, '<Style id="P%d">\n', i-1); fprintf(fid, ' <IconStyle>\n'); fprintf(fid, ' <color>%s</color>\n', colors{i}); fprintf(fid, ' <scale>%.1f</scale>\n', (i == 1) ? SIZR : SIZP); fprintf(fid, ' <Icon><href>%s</href></Icon>\n', MARKICON); fprintf(fid, ' </IconStyle>\n'); fprintf(fid, '</Style>\n'); end % Output track if tcolor > 0 outtrack(fid, solbuf, colors{tcolor}, outalt, outtime); end % Output points if pcolor > 0 fprintf(fid, '<Folder>\n'); fprintf(fid, ' <name>Rover Position</name>\n'); qcolors = [0, 1, 2, 5, 4, 3, 0]; for i = 1:solbuf.n pos = ecef2pos(solbuf.data(i).rr); outpoint(fid, solbuf.data(i).time, pos, '', ... (pcolor == 5) ? qcolors(solbuf.data(i).stat+1) : pcolor-1, outalt, outtime); end fprintf(fid, '</Folder>\n'); end % Output reference point if norm(solbuf.rb, 2) > 0 pos = ecef2pos(solbuf.rb); outpoint(fid, solbuf.data(1).time, pos, 'Reference Position', 0, outalt, 0); end % Close document and file fprintf(fid, '</Document>\n'); fprintf(fid, '</kml>\n'); fclose(fid); end function outtrack(fid, solbuf, color, outalt, outtime) fprintf(fid, '<Placemark>\n'); fprintf(fid, '<name>Rover Track</name>\n'); fprintf(fid, '<Style>\n'); fprintf(fid, '<LineStyle>\n'); fprintf(fid, '<color>%s</color>\n', color); fprintf(fid, '</LineStyle>\n'); fprintf(fid, '</Style>\n'); fprintf(fid, '<LineString>\n'); if outalt fprintf(fid, '<altitudeMode>absolute</altitudeMode>\n'); end fprintf(fid, '<coordinates>\n'); for i = 1:solbuf.n pos = ecef2pos(solbuf.data(i).rr); if outalt == 0 pos(3) = 0; elseif outalt == 2 pos(3) = pos(3) - geoidh(pos); end fprintf(fid, '%13.9f,%12.9f,%5.3f\n', pos(2)*R2D, pos(1)*R2D, pos(3)); end fprintf(fid, '</coordinates>\n'); fprintf(fid, '</LineString>\n'); fprintf(fid, '</Placemark>\n'); end function outpoint(fid, time, pos, label, style, outalt, outtime) fprintf(fid, '<Placemark>\n'); if ~isempty(label) fprintf(fid, '<name>%s</name>\n', label); end fprintf(fid, '<styleUrl>#P%d</styleUrl>\n', style); if outtime ep = time2epoch(time); if outtime == 2 time = gpst2utc(time); elseif outtime == 3 time = timeadd(gpst2utc(time), 9*3600.0); end if isempty(label) && mod(ep(6)+0.005, TINT) < 0.01 str = sprintf('%02.0f:%02.0f:%02.0f', ep(4), ep(5), ep(6)); fprintf(fid, '<name>%s</name>\n', str); end str = sprintf('%04.0f-%02.0f-%02.0fT%02.0f:%02.0f:%05.2fZ', ... ep(1), ep(2), ep(3), ep(4), ep(5), ep(6)); fprintf(fid, '<TimeStamp><when>%s</when></TimeStamp>\n', str); end fprintf(fid, '<Point>\n'); if outalt fprintf(fid, '<extrude>1</extrude>\n'); fprintf(fid, '<altitudeMode>absolute</altitudeMode>\n'); alt = pos(3) - (outalt == 2)*geoidh(pos); else alt = pos(3); end fprintf(fid, '<coordinates>%13.9f,%12.9f,%5.3f</coordinates>\n', pos(2)*R2D, pos(1)*R2D, alt); fprintf(fid, '</Point>\n'); fprintf(fid, '</Placemark>\n'); end 帮我改一下这段MATLAB代码中出错的地方
05-27
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值