Does MATLAB add extra data to text files?

1 view (last 30 days)
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 Comments
Geoff Hayes
Geoff Hayes on 28 Jan 2020
How are you running this script? From the command line? Does it have the execute permission set?
Stephen23
Stephen23 on 28 Jan 2020
Edited: Stephen23 on 28 Jan 2020
"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');

Sign in to comment.

Accepted Answer

Jeremy Hughes
Jeremy Hughes on 28 Jan 2020
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"

More Answers (1)

Daniel Morgan
Daniel Morgan on 28 Jan 2020
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!

Categories

Find more on Characters and Strings in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!