Error: Unable to open file for writing. Check write permission.
83 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Walter Roberson
am 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.
Antworten (1)
Vandit
am 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 Kommentar
Walter Roberson
am 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?
Siehe auch
Kategorien
Mehr zu Environment and Settings finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!