error in print function output name
4 views (last 30 days)
Show older comments
When I try to run the following bit of script, MATLAB successfully generates the pdf in the directory, but an error pops up in the command window that says:
Output file XXX\MATLAB_Output\pdf_generation\Fig1.pdf was not created. The file name may not be valid
(XXX being properly printed as the directory location).
The most frustrating part is, the pdf was created, and opens fine! Please let me know if I am missing anything, I am trying to run this print within a for loop and it keeps ending my script too early.
oo = 1;
fig_name = append(pwd,'\MATLAB_Output\pdf_generation\Fig',string(oo),'.pdf')
figure(oo) % this figure is already defined, so it here pops up when this runs
print(fig_name,'-dpdf')
5 Comments
dpb
on 22 Jul 2024
@Steven Lord didn't ask for more of the propietary code, he asked if you could manage to create the job that causes the error other than from the full code. What if you change something in the figure and then try or if you can just attach the figure as a .fig file...
Other alternatives -- change the path to local, change the output file type to see if the symptom remains, goes away or changes.
The other thing I'd not is that the error message doesn't say the filename is NOT valid; it says it MAY be -- I'd infer from that it's an error being thrown that isn't diagnosed completely internally -- looking at the top level of the print routine, it doesn't contain that error message text, at least with R2021b which I have installed here. Maybe poke around inside and see if you can find the condition(s) that are being tested to cause the message...
Answers (1)
Nivedita
on 23 Jul 2024
It seems like the issue is related to how MATLAB handles file paths and the "print" function. One common issue is the use of backslashes (`\`) in file paths, which can sometimes cause problems. MATLAB handles file paths more reliably when forward slashes (`/`) are used.
Here’s a revised version of your script that uses forward slashes instead:
oo = 1;
fig_name = append(pwd, '/MATLAB_Output/pdf_generation/Fig', string(oo), '.pdf');
figure(oo); % this figure is already defined, so it here pops up when this runs
print(fig_name, '-dpdf');
Additionally, you might want to ensure that the directory structure exists before attempting to save the file. You can use "mkdir" to create the necessary directories if they do not exist:
oo = 1;
output_dir = fullfile(pwd, 'MATLAB_Output', 'pdf_generation');
if ~exist(output_dir, 'dir')
mkdir(output_dir);
end
fig_name = fullfile(output_dir, ['Fig', string(oo), '.pdf']);
figure(oo); % this figure is already defined, so it here pops up when this runs
print(fig_name, '-dpdf');
This script uses "fullfile" to construct the file path, which ensures that the correct file separators are used for your operating system. It also checks if the output directory exists and creates it if necessary.
Try running the revised script and see if it resolves the issue.
4 Comments
Walter Roberson
on 23 Jul 2024
The OP responded that is on Windows for which the file separation character IS the backslash
Actually...
The internal Windows file seperation character is forward slash. The use of backwards slash is in a wrapper layer.
dpb
on 23 Jul 2024
I guess I never looked into the bowels, Walter...but in MATLAB with fullfile() it'll build the name with the system user-visible separator character. I suppose that could lead to some issue regarding such a translation internally.
It still would be interesting to delve into the internals and see what it is testing to throw that particular "...may be..." error message.
See Also
Categories
Find more on Environment and Settings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!