MATLAB: Can I use engEvalString to create ascii and/or MATLAB files either from within the workspace or via a function call

\naenginefileMATLABtowrite

My engine file is below. Note, test.asc is created but the strings FILELIST and OUTFILE are not written to it. Also, create_hourly.m works from within the MATLAB session using the following syntax,
create_hourly('filelist','test.mat');
where filelist is a text file with a list of matlab data files, e.g.
file1.mat
file2.mat
file3.mat
The function does not output anything to the MATLAB session. All variables are saved to test.mat using the save command from within the function.

Best Answer

You must replace all '\' with '\\' in any string passed to MATLAB inside a C program because the C compiler translates two character sequences (like \n) into a single character (in this case newline) within any string. So, the following string
"fprintf(fid,'%s\n%s\n',filelist,outfile);"
when passed to MATLAB would display something like:
"fprint(fid,'%s
%s
',filelist,outfile);
This is invalid input.
You have to suppress that conversion. The C compiler also recognizes '\\' and converts it to a single '\'. So the new code becomes:
engEvalString(ep, "fid=fopen('test.asc','w');");
engEvalString(ep, "fprintf(fid,'%s\\n%s\\n',filelist,outfile);");
engEvalString(ep, "fclose(fid);");