Failure to read jpeg files in Matlab - matlab

I'm trying to open images with imread but it keEps telling me that the files do not exist.
Here's the message from the command window
Error using imread>get_full_filename (line 516)
File "Pic1.jpg" does not exist.
Error in imread (line 340)
fullname = get_full_filename(filename);
Error in ImageDetection (line 2)
img1 = imread('Pic1.jpg');
And here are the sections of code that it's referencing from the function itself
if (fid == -1)
if ~isempty(dir(filename))
% String 'Too many open files' is from strerror.
% So, no need for a message catalog.
if contains(errmsg, 'Too many open files')
error(message('MATLAB:imagesci:imread:tooManyOpenFiles', filename));
else
error(message('MATLAB:imagesci:imread:fileReadPermission', filename));
end
else
error(message('MATLAB:imagesci:imread:fileDoesNotExist', filename));<--LINE 516
end
if isempty(fmt_s)
% The format was not specified explicitly.
% Get the absolute path of the file
fullname = get_full_filename(filename); <--LINE 340

Image isn't in Current Directory (Or Path)
If your image is in your working directory, you can call it by its name ("Pic1.jpg"). However, MATLAB doesn't search all folders on your computer. If, for example, if your program is running in C:\Users\user\Documents\MATLAB, and the image is in C:\Users\user\Pictures, you could reference the picture using:
Absolute paths ("C:\Users\user\Pictures\Pic1.jpg")
Relative paths ("..\..\Pictures\Pic1.jpg")
Usually, if the pictures only exist because of your program, it'd be somewhere in the same directory, so you wouldn't need to use ".." to move up any directories.
If you want the user to be able to select a picture each time the program is run, I'd recommend looking into uigetfile. If you want to know more about where MATLAB searches for files, see this article.
Secondly, you may want to check your file name. While it seems obvious, a simple misspelling can be hard to notice at times, for example "Pic1.jpg" vs "Pic1,jpg" vs "Pic1.jpeg"

Just above the line of your code where the error occurs, write a new line:
dir
To output in the prompt the files of the current folder, and check that the name of your file exactly appears there. Could you copy us that output?

Related

Error using "save" to save in a directory

I would like to save a workspace in another directory and I have written the following in Matlab for it:
fileName = [datestr(now, 'dd-mmm-yyyy_HHMMSS') '_test'];
save('C:\Users\User\project',fileName)
It gives me the error: Error using save: '05-Nov-2019_083736_test' is not a valid variable name.
But if I run without giving an address of the directory it workes perfectly.
Why does it happen?
You can either use il_raffa's suggestion from the comments (with a small correction):
save(['C:\Users\User\project\' fileName])
% ^ add a folder separator here
or use the fullfile function, to avoid errors due to forgotten folder separators:
save(fullfile('C:\Users\User\project', fileName));
This works also for subfolders and filenames, e.g.
save(fullfile('C:\Users\User\project', 'matfiles', fileName));

Error message "using openfig" with Matlab 2018b and Octave

I am trying to run a teaching tutorial on Fourier space in MRI image generation, and consistently getting the following error:
Error using openfig
Too many input arguments.
Error in k_space_tutorial (line 20)
fig = openfig(mfilename, 'reuse')
I have made sure that the folder with the code and related files is in the directory:
>> isdir('k_Space_tutorial_David_Moratal')
ans =
logical
1
and in the path:
>> path
MATLABPATH
C:\Users\Myname\Documents\MATLAB
These are the contents of the directory:
>> dir
. half_fov.m openfig.m
.. image_test.bmp rectangular_matrix.m
README.txt imatge_i_espai_k_originals.m replay_pid10644.log
add_awgnoise.m k_space_tutorial.fig replay_pid11732.log
filtre_pas_alt.m k_space_tutorial.m replay_pid13344.log
filtre_pas_baix.m modaldlg.fig
half_fourier_fe.m modaldlg.m
half_fourier_pe.m motion_artifacts.m
Results of the debugger:
openfig is a function that comes with MATLAB, and according to its documentation, the syntax used is correct.
One of the possible reasons for a function being called correctly but leading to an error is that a different function is called inadvertently. This happens when a different function with the same name shadows (hides) the original function. An M-file in the current directory, or in an earlier directory on the path, with the same name will cause this.
Typing which openfig at the MATLAB command prompt will tell you what function is called when that name is used.
In this case, there is an M-file in the current directory with the same name. Deleting this file (or renaming it) will solve the problem.

File addressing of code for MATLAB compiler

In my codes, there are some functions like imshow or fopen files that need addressing. When I use my program in MATLAB I use pwd like imshow([pwd '/image.jpg']) for addressing and the program run and work correctly, but when I compiled my program after installing it (redistribution) when I open shortcut in desktop, an error message appear with the title that my program can't find image.jpg . When I check the address of searching, it is like :
C:/User/Desktop/image.jpg
I read this page but I don't know how to use this addressing.
http://www.mathworks.com/matlabcentral/answers/59148-for-stand-alone-exe-how-do-i-include-a-folder-of-files-and-know-how-to-access-them
Beside It I don't know where I should add these files ( images and texts ) in MATLAB compiler options. In file required for your application to run or file installed with your application.
Thanks.
That is because your image is not located in your current path (i.e. the desktop in this case).
If you want to use images, you should include the image in deploytool's "shared Resources and helper files" and in your script/function reference the image as specified in the link, using:
if isdeployed
imagepath = [ctfroot filesep 'image.jpg'];
else
imagepath = [pwd filesep 'image.jpg'];
end
% Now use imagepath as if it was [pwd filesep 'image.jpg']
[A] = imread(imagepath);
Other option is including the file (image.jpg) in the same path as your final executable, since you are calling the image from pwd.

concatenating file path to changing folders

I am somewhat new to MATLAB and am trying to set up a changing file path in a loop to go into a series of folders and grab image files from each folder. I'm not sure if the problem is with the concatenated parts of the path itself, or with the wildcard search I am using.
I've used similar changing file paths before that have worked, but this one is giving me a "Index exceeds matrix dimensions" error. I thought it was the '*' element that was problematic (similar concatenated paths have worked for me, but only when I specify a file extension or part of a file name), but I am trying to grab DICOM files that do not have any extension, which might make it difficult.
The line within the for loop is as follows:
inputs{1, crun} = cellstr(spm_select('FPList'[allinput,'T1Rawunzip',filesep,OrderForDicoms3{crun,1}],'*'));
I've tried different ways of specifying this--using spm_select, not using spm_select, using commas instead of filesep or vice versa, but nothing has worked.
Any advice would be very much appreciated.
(for reference:
crun is the counter the moves the loop forward, 'allinput' is a previously-specified path, OrderForDicoms3 is a .mat file with a list of folder names that are being individually concatenated to the path each time the loop runs)
Thanks!
-Victoria
I can tell you the most general approach of grabbing files from a folder. If you specify the input folder through uigetdir, then all the files can be grabbed using dir command:
folder = uigetdir;
files = dir(folder);
for i =1:length(files)
if(~files.isdir())
filename = fullfile(folder, files(i).name);
% ... read in the data %
end
end
You can always do it for multiple levels.

octave load function

i'm trying to load a mat file from a subdirectory using the following code:
% filename_str is read from a text file
directoryname_str = "./data";
f = fullfile(directoryname_str, filename_str);
load(f);
when i run this sequence, load says it can't find the file...but when i copy or type the relative path and the file name by hand into an active octave session, everything works like a champ with no errors.
i assume this has something to do with how octave searches for mat files? if so, what's the correct environment variable or function call i need to make in order for this code to work?
thanks!
Are you sure that what you put into the variable f is the same as what you input manually in octave?
Are you also in the same directory? Because you're specifying relative paths, this should be the case.. you can get the current directory octave is in with pwd
And last of all, you can double check file existence in octave itself using exist
exist(f,'file')
If this returns false, there's definitely something wrong with your current directory, are something's very weird going on..