Does MATLAB add extra data to text files?
조회 수: 2 (최근 30일)
이전 댓글 표시
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
2020년 1월 28일
How are you running this script? From the command line? Does it have the execute permission set?
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
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"
댓글 수: 0
추가 답변 (1개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Characters and Strings에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!