Error => Unable to open file "Image1" for writing. You might not have write permission.

13 views (last 30 days)
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
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.

Sign in to comment.

Answers (2)

Adam
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
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)
. GDS-CACHE hsperfdata_wguser worker.properties .. Image7 jetty-0.0.0.0-8080-worker-webapp-_-any- ws_override.properties Editor_dswgc hsperfdata_mcguser matlabpref
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']);

Sign in to comment.


Milad Kassaie
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
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.

Sign in to comment.

Categories

Find more on Environment and Settings in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!