How to fprintf directory name?

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

2 Comments

madhan ravi
madhan ravi on 6 Dec 2018
Edited: madhan ravi on 6 Dec 2018
why not just use sprintf()? as shown in the message they are quite handy
I don't understand how. The message hint that I should use it but I coudn't figure out how to get it printed.

Sign in to comment.

 Accepted 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

6 Comments

Hi Robert,
Thanks for your remarks.
Later on I use this file to log a lot of information. This why I didn't included the closing line - it comes much later.
For this reson, I use 'w' because I want a new clean log file for each new run.
The '\n' is included so the next remark printed into the file will start in a new line.
My only problem is that I can't figure a way to print the full directory into the file.
Alon
Hi Alon Rozen,
Answer to your question is already given in the code snippet:
fprintf is requiring a format-string which would be '%s\n' in order to write a string and end the line.
Kind regards,
Robert
Stephen23
Stephen23 on 6 Dec 2018
Edited: Stephen23 on 6 Dec 2018
"My only problem is that I can't figure a way to print the full directory into the file. "
The main problem is not reading the fprintf help. The second argument to fprintf should be the format string: where is the format string defined in your code? (answer: nowhere).
"The '\n' is included so the next remark printed into the file will start in a new line. "
But this will only work inside the format string... which you have not defined (although Robert U showed you how).
Hi Robert,
I tried adding %s to the fprintf command but I get the same warning and it only prints:
D:
I think that the directory seperator sign '\' is the cause of the problem. But a full path might contain several of these separators. And beforehand I don't know where thay are.
Thanks again,
Alon
Jan
Jan on 6 Dec 2018
Edited: Jan on 6 Dec 2018
@Alon: The suggested code does work definitely:
fprintf(Log_File, '%s\n', Dir_Name);
Please try it exactly as written. The problem is, that '\' is the "escape character" in the format string. If you want to display it in a format string, you need to use two of them: '\\'. To display a path, do not include the folder name in the format string, but in the string to be displayed. See this:
folder = 'C:\Temp\'
fprintf(folder) % Stops because \T is not a valid control sequence
fprintf(strrep(folder, '\', '\\')) % Works because \ got \\
fprintf('%s\n', folder) % Best solution
Without specifying the file id as first input, fprintf writes to the command window, which is easier for controlling the output. Please read the documentation of fprintf again to understand the difference between the format string and the data to be written.
"tried adding %s to the fprintf command" - Obviously there is a mistake in this trial, so prefer to post the code instead of describing it by words.
Thanks Jan and Robert,
Unlike what I wrote above - I repeted exacly what Robert suggested (carfully this time) and it works! Thanks :)
When I tried to include the strrep function it added another '\' wherever there was one. Apparently the command %s takes care of this.
Thanks again,
Alon

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2016b

Community Treasure Hunt

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

Start Hunting!