Does MATLAB add extra data to text files?

조회 수: 1 (최근 30일)
Daniel Morgan
Daniel Morgan 2020년 1월 28일
답변: Daniel Morgan 2020년 1월 28일
I'm using MATLAB to build a script for another program (said program has very limited built in scripting capablities, hence the need to write a script so that I can write a script). I've run into some very strange behavior from this generated script. If run as generated, the other program loads the script, then completely ignores it with no error messages. However, if I copy the text of this script into a new file, it runs fine. The only thing I can think to explain this is that MATLAB is adding something extra to its text files, something that this other program reads and interprets. I'd like to figure out if this is true, and if so, then if I can disable it.
I'm including the script below, though it's not doing anything particularly special.
plant_table = readtable('secret_table.xlsx');
dest_points = table({"title1", "title2", "title3", "title4"}', {"33333","44444","55555","66666"}', 'VariableNames', {'Destination', 'BusNumber'});
aux_file_name = 'analysis.aux';
save_file_name = 'data.csv';
fid = fopen(aux_file_name,'w');
for idx = 1:height(dest_points)
fprintf(fid, '// %s Interface \r \r', string(dest_points.Destination(idx)));
for jdx = 1:height(plant_table)
fprintf(fid, 'Script PTDFCalculation \r ');
fprintf(fid, '{ \r ');
fprintf(fid,'CalculatePTDF([BUS %s], [BUS %s], AC); \r \r ',string(plant_table.BusNumber(jdx)), dest_points.BusNumber{idx});
fprintf(fid,'SetData(CaseInfo_Options, [KeyFieldsUse], ["Secondary"]); \r \r ');
fprintf(fid,'SaveDataWithExtra("%s",CSVCOLHEADER,Interface,[Name,Number,PTDF,MW,HasCTG,MonDirection],[],"filtername",[],[],[],); \r', save_file_name);
fprintf(fid, '} \r \r');
end
fprintf(fid, '\r');
end
fclose(fid);
  댓글 수: 4
Geoff Hayes
Geoff Hayes 2020년 1월 28일
How are you running this script? From the command line? Does it have the execute permission set?
Stephen23
Stephen23 2020년 1월 28일
편집: Stephen23 2020년 1월 28일
"The only thing I can think to explain this is that MATLAB is adding something extra to its text files..."
Or that you are using an invalid newline character.
No modern OS uses \r as a newline, and certainly no OS that MATLAB currently runs on uses \r for the newline:
Most likely you should use \n in the format strings together with the 't' option in fopen:
fid = fopen(aux_file_name,'wt');

댓글을 달려면 로그인하십시오.

채택된 답변

Jeremy Hughes
Jeremy Hughes 2020년 1월 28일
I'd check the new line characters "\r" isn't typically used for a new line in most modern environments.
It may be that the text editor you're using is replacing these with "\n" or "\r\n".
Try replacing "\r" in your (MATLB) code with "\n"

추가 답변 (1개)

Daniel Morgan
Daniel Morgan 2020년 1월 28일
Yup, that appears to have been the issue. I suspect that what was happening when it was copied over to another text document is that Windows was automatically replacing the \r with a \n.
Thanks!

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by