export() only works the first time
3 views (last 30 days)
Show older comments
I have a Live Script with Inline outputs (several figures and tables) that I'd like to export as pdf. The export() function will work the first time I run the code upon opening MATLAB, but if I try to run the export command again, it just stalls and won't export. I then have to force-quit MATLAB, reopen, and run the section again. I thought maybe export() was getting confused if the file already existed so I added a delete() call right before export(), but that isn't fixing it.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Code & Outputs here
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Export Section
ExpFile = "myScript.pdf";
delete(ExpFile) % returns a warning if the file doesn't exist but this is ok
export("ChainBudgetsCustom.mlx",ExpFile); % this line only works the first time
4 Comments
Steven Lord
on 1 Apr 2024
If you hold down Ctrl-C for a few seconds, does MATLAB throw an error interrupting the export call? If so, can you post the stack for that error (everything displayed in red in the Command Window)?
Answers (1)
prabhat kumar sharma
on 1 Apr 2024
Hi Cristian,
I understand you're encountering issues with the export() function in MATLAB when attempting to run your script for a second time. To investigate, I replicated your scenario in MATLAB R2023a, where the function operated as expected. However, when testing in MATLAB R2021b, it became apparent that the export function is not available in versions prior to R2022a. This discrepancy might explain the difficulties you're experiencing. I recommend verifying your MATLAB version and considering an update to the latest release to access this functionality.
Here's a concise code snippet used for testing:
% Sample MATLAB Live Script to Export as PDF
Generate Sample Figure
figure;
plot(rand(10,1));
title('Sample Figure');
Generate Sample Table
T = table([1;2;3],[4;5;6],[7;8;9],'VariableNames',{'A','B','C'});
disp(T);
Export Section
Define the export file name
ExpFile = "myScript.pdf";
% Checking if the file exists, and delete it if it does
if isfile(ExpFile)
delete(ExpFile);
end
% Exported the live script to PDF
export("test.mlx", ExpFile);
For further details on the export function, please refer to the: https://www.mathworks.com/help/matlab/ref/export.html
2.If the export function continue to be unreliable, you might consider alternative export methods, such as the publish function, although it may not provide the same level of output fidelity for live scripts specifically
More information on the publish function can be found in the: https://www.mathworks.com/help/matlab/ref/publish.html
3. Sometimes, MATLAB or other applications might not properly release a file handle after creation or deletion attempts. Ensure no other applications are accessing the file and that MATLAB itself hasn't locked the file. This situation may require closing any unnecessary applications or processes that could interfere.
I hope it helps to resolve your issue.
3 Comments
prabhat kumar sharma
on 2 Apr 2024
You can try closing all other apps and processes which might be using that file , like code editor or something which can aquire the lock over the file and try running the script again.
See Also
Categories
Find more on Function Creation 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!