Error: Unable to open file for writing. Check write permission.

71 views (last 30 days)
I´m doing a code to generate a system composer report with sequence diagrams, but when i run the code, i get the error: Unable to open file 'C:\Users\user\OneDrive\Documents\MATLAB\image.png for writing. Check write permission. I checked that path and i do have writing permission, and i try running matlab as admin, but i get the same error. The tricky part is, if I run a simple code using the imwrite comand to save an image in that path (before running the report generator code i have), it works well and it writes an image there, but as soon as i run the repor generator code, i get the error, and if again, try to use the imwrite command (that previously worked) it throws the same error that said i don´t have write permission, but it had worked moments ago. Also, in the report generator code, in anypart of the code I specifed that i want to save the images in that specific path, matlab uses that path by default.
This is the report generator code:
import mlreportgen.report.*
import slreportgen.report.*
import slreportgen.finder.*
import mlreportgen.dom.*
import mlreportgen.utils.*
import systemcomposer.query.*
import systemcomposer.rptgen.finder.*
rpt = slreportgen.report.Report('OutputPath', 'FullArchitecture', ...
'CompileModelBeforeReporting', false);
model = systemcomposer.loadModel(modelFileName);
if isempty(model)
error('Model could not be loaded: %s', modelFileName);
end
seqDiagrams = model.getInteractions;
if isempty(seqDiagrams)
warning('No sequence diagrams found in the model: %s', modelFileName);
return; % Exit if there are no diagrams
end
seqChapter = Chapter("Title", "Sequence Diagrams");
Sect = Section("Title", model.Name);
add(seqChapter, Sect);
try
% Loop through each sequence diagram and add it to the report
for i = 1:length(seqDiagrams)
seqDiagram = seqDiagrams(i);
% Create a SequenceDiagram reporter for each diagram
seqReporter = systemcomposer.rptgen.report.SequenceDiagram("Name", ...
seqDiagram.Name, "ModelName", model.Name);
seqSect = Section("Title", seqDiagram.Name);
add(seqSect, seqReporter);
add(Sect, seqSect);
end
% Add the chapter to the report
add(rpt, seqChapter);
catch ME
fprintf('Error encountered: %s\n', ME.message);
end
% Finally, close and view or save the report (depending on platform)
close(rpt);
  3 Comments
Walter Roberson
Walter Roberson on 12 Sep 2024
Writing directly to OneDrive is prone to sychronization errors. It is not rare to see write errors in tight loops but then to see the write errors disappear during debugging.
Victoria Ramirez
Victoria Ramirez on 17 Sep 2024
It doesn´t let me change where it saves the photos, because in anywhere in the code i especify the onedrive path, matlab just take that as a default and i don´t know how to change it. Any ideas on how can i do that?

Sign in to comment.

Answers (1)

Vandit
Vandit on 12 Sep 2024
Hello Victoria,
Here are a few steps you can take to troubleshoot and potentially resolve the issue:
  • Try changing the temporary directory used by MATLAB for storing images. You can set a different temporary directory by using "tempdir" functions to ensure it's writable. To know more about "tempdir" function, please refer to the below documentation: https://www.mathworks.com/help/matlab/ref/tempdir.html
  • If MATLAB defaults to a specific path for saving images, try explicitly setting the path for the images in your report to a location you know is writable:
imageOutputPath = fullfile(tempDir, 'images');
mkdir(imageOutputPath);
% Ensure any image saving uses this path
setpref('Images', 'OutputPath', imageOutputPath);
Additionally, if you are using "save" function, please refer to the below MATLAB Answers thread which might resolve your query:
Hope this helps.
  1 Comment
Walter Roberson
Walter Roberson on 17 Sep 2024
setpref('Images', 'OutputPath', imageOutputPath);
I do not recognize that ? Could you indicate where OutputPath of Images is documented?
OutputPath is a common property for report generator objects, but it is being set via
rpt = slreportgen.report.Report('OutputPath', 'FullArchitecture', ...
'CompileModelBeforeReporting', false);
which does not appear to have anything to do with Images preferences?

Sign in to comment.

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!