Error => Unable to open file "Image1" for writing. You might not have write permission.
13 views (last 30 days)
Show older comments
Am getting an error "Unable to open file "Image1" for writing. You might not have write permission." while running this program. Please help me
x=videoinput('winvideo',1);
for i=1:10
preview(x);
pause(1);
img=getsnapshot(x);
fname=['Image' num2str(i)];
imwrite(img,fname,'jpg');
pause(0.1);
end
1 Comment
Mridul Pandey
on 18 Nov 2017
change present working directory to the desired directory where you want to save the file either change in on the command window or script. like.. cd c:\folder\folder\... and then use imsave(...) function.
Answers (2)
Adam
on 16 Mar 2017
Surely the error is self-explanatory? How exactly are you expecting us to give you write access to your folder? Can you open other files from that folder? Can you create new files in it outside of Matlab?
3 Comments
Walter Roberson
on 3 Dec 2020
img = randi([0 255], 320, 240, 3, 'uint8');
imshow(img)
i = 7;
folder = tempdir; % Adjust to your needs
fname = fullfile(folder, ['Image' num2str(i)]);
imwrite(img,fname,'jpg');
dir(folder)
Image7 is present in the output directory. Looks to me as if it worked.
Note: most of the time you want to append a file extension to the file name, such as
fname = fullfile(folder, ['Image' num2str(i) '.jpg']);
Milad Kassaie
on 3 Aug 2022
This is a common issue in MS Windows. Write access is restricted in system folders, including where MATLAB is typically installed.
The soultion is to close MATLAB. Then right-click on the MATLAB icon and choose "Run as Administrator". Matlab will now have write access everywhere.
I hope this helps.
1 Comment
Walter Roberson
on 3 Aug 2022
Run As Adminstrator is not recommended; the operating system restrictions are there to prevent malware from infecting the system, and to reduce accidental user corruption of files.
You should instead take care to write to a directory you have write access to.
See for example the use of tempdir() that Jan and I suggested, https://www.mathworks.com/matlabcentral/answers/330185-error-unable-to-open-file-image1-for-writing-you-might-not-have-write-permission#comment_437586
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!