File Write Permission Issue in Matlab - matlab

I've been trying to write image files to the specified folder, but I always receive this error:
Unable to open file
"C:\Users\Dani\Desktop\code_version_1.0\myImages"
for writing. You may not have
write permission.
Is there a way to fix this? Thanks.
for i=1:numberOfFiles
filename=fileList{i};
img=imread(filename,'jpg');
image = imresize(img, [150,150]);
folder='C:\Users\Dani\Desktop\code_version_1.0\myImages';
if ~exist(folder,'dir')
mkdir(folder);
end
imwrite(image,folder,'jpg');
end

Your call to imwrite has an invalid second parameter. You gave it a folder when it is asking for a file path.
Here's a possible work-around:
outfile = fullfile(folder, 'output.jpg');
imwrite(image, outfile, 'jpg');

How about using the imwrite like this :
imwrite(image,'C:\Users\Dani\Desktop\code_version_1.0\myImages\image.jpg');
you can after append things as you like. check this link

Related

Creating a valid filename with dir and fullfile

I keep running across this error in my code:
Path = 'C:\Users\18606\OneDrive\Documents\Spheroids For Brandon\Spheroids\1-8WT';
Content = dir(Path);
SubFold = Content([Content.isdir]); % Keep only the directories
MyData = [];
for k = 3:length(SubFold)
F = fullfile(Path, SubFold(k).name);
fileID = fopen(F);
MyData{end+1} = fopen(fileID,'%s\n');
fclose(fileID);
Resulting in the error:
Error using fopen
Invalid filename (line 8)
The code is trying to iterate over multiple images in multiple subfolders of the main. The goal is to process the images with an edge detection algorithm for each file, but that's besides the point. Why would the program give an invalid file name when the path, content and subfolders are all specified in the code? Do the variables mentioned have anything to do with the error? Finally, is there a better way to open and read the images iteratively?
Reading in files sequentially is indeed usually done through looping over a dir() call, i.e. your strategy is valid. What's going wrong here, is that Path is a path to a directory, not a file. SubFold then are only directories as they are found on the Path. fullfile(Path, SubFold(k).name) finally creates a path to a subdirectory of Path. A subdirectory is not a file, thus fopen will tell you that it's an incorrect filename.
You'll probably need another dir() call, e.g. dir(F) to get all files on the path specified by F.

How do you view the plots saved by the print() function in Matlab?

This is probably a simple question, but I have some files and data. After printing each one of them to a '-dpng' file, I just want to view the plot and copy them elsewhere. However, opening the file, all I see is the "Import Wizard". I click "Finish" and nothing happens. This is my code:
files = dir('*.csv');
for file = files'
lab5 = csvread(file.name, 9)
lab5(:,1) = log10(lab5(:,1))
plot(lab5(:,1),lab5(:,2))
print(strcat(file.name,'plot'), '-dpng')
end
I tried to avoid print() by using savefig, but for some reason savefig was giving me a vague error. Only print works, but I'm not sure how to view the output.
You are saving your image as filename.csvplot, which Preview does not accept as a valid image file.
For example:
% Generate dummy file
fID = fopen('blah.csv', 'w');
fclose(fID);
% Recreate print job
files = dir('*.csv');
plot(1:10)
fname = strcat(files(1).name, 'plot');
print(fname, '-dpng');
Which gives us:
fname =
blah.csvplot
Why isn't .png appended to the filename? Per the documentation for print:
If the file name does not include an extension, then print appends the appropriate one.
Filename inputs are parsed for an extension (e.g. things prepended with .), and does not append an extension if one is found. In this case, the filename passed to print has the .csvplot extension. This might be unexpected but it does make sense, file extensions don't actually control anything about the file itself; you could save your file as image.finderpleaseopen and have it still be a valid PNG file. Finder is just too stubborn to open it without being forced because it's not a known, supported file extension.
To fix this, you should save your file with the correct file extension. There are two ways to do this, append the correct extension or remove the undesired extension with something like fileparts or regexprep and let print handle it for you.
For example:
% blah.csvplot.png
fname = strcat(files(1).name, 'plot', '.png');
print(fname, '-dpng');
% blahplot.png
[~, filename] = fileparts(files(1).name);
fname = strcat(filename, 'plot');
print(fname, '-dpng');
savefig does not produce a valid output for Finder because it does not produce any output without a .fig extension:
If the specified file name does not include a .fig file extension, then MATLAB appends the extension. savefig does not accept other file extensions.
*.fig files are not image files and cannot be opened natively by finder.

read image permission in Matlab

I'm trying to access images in a matlab interface
my code is as follows:
global im2 im
axes(handles.axes4);
[path1, user_cance]= imgetfile();
if user_cance
msgbox(sprintf('Error'), 'Error', 'Error');
return
end
srcFiles = dir('C:\Users\User\Desktop\images test\yale faces\yalefaces\..');
% yale faces is the database folder
for i = 1 : length(srcFiles)
file_name=dir(strcat('C:\Users\User\Desktop\images test\yale faces\yalefaces'));
im2=imread(strcat('C:\Users\User\Desktop\images test\yale faces\yalefaces',file_name(i).name));
%processing of read image
end
the issue is that when I run the code, it gives the following error:
Can't open file "C:\Users\User\Desktop\images test\yale faces\yalefaces" for
reading;
you may not have read permission.
Does anyone know how to solve this issue?
When you do a directory listing (without any wildcards) you are going to get the current directory '.' and parent directory as well '..'. You can't read these like files because they are directories. You will need to filter out the directories prior to trying to read them with imread.
files = dir('C:\Users\User\Desktop\images test\yale faces\yalefaces');
% Remove directories
files = files(~[files.isdir]);
As a side note, it is very hard to tell what your code is doing, but I'm pretty sure it doesn't do what you hope.
It seems like you want to get all images within the database. If that's so, you'll want to do something like.
folder = 'C:\Users\User\Desktop\images test\yale faces\yalefaces';
% Get a list of all files in this folder
files = dir(folder);
files = files(~[files.isdir]);
for k = 1:numel(files)
% Append the folder with the filename to get the path and load
im2 = imread(fullfile(folder, files(k).name));
end
I highly discourage using strcat to construct file paths particularly because it removes trailing/leading whitespace from each input which can corrupt a filename. fullfile was designed for exactly this so please use that.

Why I cannot open a txt file

I am really a new person in MATLAB and I need use it to finish my homework.
First, I try to open a txt file to get data.
So, I do like this:
folder='C:\Users\yshi20\Desktop\COSC6335\proj_1';
file='transactionDB.txt';
myData=fullfile(folder,file);
[Datafile, message] = fopen('transactionDB.txt', 'r');
But the datafile value always show -1 which means it failed to open.
So, I use this to check why I cannot open it:
if Datafile < 0
disp(message);
c = [];
else
Data = fread(Datafile, 5, 'uint8=>char')'
end
But the result says: No such file or directory.
But I checked many times, and I am sure the file name is correct and the location folder is correct, so, how to solve the problem?
You're using the wrong variable. You need to use myData in fopen.
[Datafile, message] = fopen(myData, 'r');
myData stores the complete path to your file whereas in your original code, you're using relative referencing which means that it's going to look for the file in the current working directory. A -1 code means that it can't find the file... and rightfully so given your error. It can't find the file in the current working directory. As such, make sure you change your fopen statement so that the correct path to your file is specified.

MATLAB Saving Figure Invalid Filename Error

I am writing a program to plot graphs in a loop and I want to save each graph that comes out as a .jpg file with a variation of the file name. Here is my code for saving the graphs:
filename = strcat('WI_Pollutants_', D(i,6), '_200706_O3');
saveas(gcf, filename, 'jpg');
The saved file should come out as the following with D(i,6) changing each iteration of the loop.
WI_Pollutants_003-0010_200706_O3.jpg
However, I'm running an error: (Maybe it has to due with saveas wanting a string only?)
Error using saveas (line 81)
Invalid filename.
saveas only accepts characters as the filename. But when filename was created, strcat made it a cell array. Therefore, the filename needs to be converted to a character array.
filename = char(strcat('WI_Pollutants_', D(i,6), '_200706_O3'));
saveas(gcf, filename, 'jpg');
This solves the problem.
I think your D{i,6} is ending up wrapped up as an array, from this line:
D{i,6} = [num2str(D{i,6}) '-' num2str(D{i,7})];
To solve this, just removing the []'s
D{i,6} = num2str(D{i,6}) '-' num2str(D{i,7});
I think what happened is your D{i,6}=['someString'], and concatinating added in the []'s that werent' desired.
As a check, if something like this happens again, just fprintf(filename) right before use, and look at what comes out. I suspect you'll find the issue there. You can always remove the print statement later on.