MATLAB: How to fprintf directory name

fprintfMATLABtext file

Hi all,
I am geting a directory name using:
Dir_Name = uigetdir;
Into Dir_Name, which is of type 'char', I get something like D:\Data\My_Projects\Project_1.
I am trying to print this Dir_Name into a text file using:
Log_File = fopen('My_Log_File.txt','w');
fprinf(Log_File,Dir_Name,'\n');
I get a warning which says: Escaped character '\D' is not valid. See 'doc sprintf' for supported special characters.
I read that document very carefuly but apparently missing the hint what to do. Is the first D the problem? The second?
If I can't print the full directory into the log file, the last directory name, in this case 'Project_1' is also a good solution for me.
Is there a way to pring the full directory? Is there a way to extract the last directory name?
Thanks,
Alon

Best Answer

Hi Alon Rozen,
There are some syntax issues in your example:
Dir_Name = uigetdir;
Log_File = fopen('My_Log_File.txt','a');
fprintf(Log_File,'%s\n',Dir_Name);
fclose(Log_File);
If you use 'w' as permission of fopen() new entries would overwrite old ones. Since uigetdir() does not provide multiple lines the '\n' seems to not make sense there. If you want to append new entries to file use 'a' instead.
Kind regards,
Robert