Shorthand for grabbing files in a subdirectory in Matlab - matlab

I have a data folder in /home/me/project/data that contains image files. In project is my script. I wish to imread all the images in this file. When I do the following dirData = dir('.\data\*.jpg'); I get Error using imread (line 349). Is there a way to refer to this directory without having to specify the full path?

Related

read an image from windows with imread

i want to read an image from my windows with imread in matlab. my image name is "chest.jpg" but when i read I = imread('chest.jpg'); it shows errors :
File "chest.jpg" does not exist.
and
fullname = get_full_filename(filename);
how can I read my image from my folder?
If you image is in 'project_folder/another_folder/.../' you must use
imread('project_folder/another_folder/.../chest.jpg')
I suggest to use a relative path if the image is inside the script folder or in some internal folder. If it is in an independent path you can use an absolute path.

Failure to read jpeg files in 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?

Read wave file in matlab

this is the code and i have no idea why it's not workinng
y=audioread('Arabic_Male_Alain_G_Corp');
Error using audioread (line 74)
The filename specified was not found in the MATLAB path.
Is this file located in your active folder? If not, you need to specify the full filepath (e.g. C:\...)

Load an image from its absolute path in Matlab also if is not included in working directory

I have the absolute path of an image myabspath
D:\myimages\venus\surface\im0012.jpg
I have tried
im=imread(myabspath);
but doesn't work because seems that imread accept only the name of a file in the current working directory.
I have also tried
f=load(myabspath);
But get an error "Argument must contain a string".
Seems a pretty basilar operation but unfortunately I haven't found the solution.
EDIT
Seems that the problem is caused by the fact myabspathis not a regular String but a cell, I have tried to use
myabspath=cellstr(myabspath)
but I continue to receive the error that tell me that myabspath is not a string, but if I call
display(myabspath)
I see the right path. Any solution?
If you have a cell that contains the String path, you don't need any conversion, is enough access the content of the cell using {index}.
Eg
if you have to get the first element use myabspath{1}
imread is able to read images from absolute paths
It seems strange.
Did you get a specific error message?
Does the image actually exists?
Did you, perhaps, write a "your own" imread function which overrides the "MatLab" one?
According to MatLab (R2012b) help, imread also accepts "full pathname"
imread Read image from graphics file.
A = imread(FILENAME,FMT) reads a grayscale or color image from the file
specified by the string FILENAME. If the file is not in the current
directory, or in a directory on the MATLAB path, specify the full
pathname.
I've replicated your folder structure, I did not add it to the MatLab path, nevertheless and I've been able to read an image with imread by specifying the full pathname.
This is the output I've got:
>> myabspath='D:\myimages\venus\surface\im0012.jpg'
myabspath =
D:\myimages\venus\surface\im0012.jpg
>> im=imread(myabspath);
>> whos
Name Size Bytes Class Attributes
im 421x500x3 631500 uint8
myabspath 1x36 72 char

access folders and get files

I start with Matlab and would like to know how could I access to a folder and get contents to access files and read them.
I have a variable in workspace tmpfolder that is equal to 'path to folder' but I don't find how could I make dir(tmpfolder) and get files, browse any file content to get a string value...
I would start with dir() and fopen().
More generally, try starting at the beginning: Working with Files and Folders.
If you have an image file in jpeg format in another folder named myimage and a text file called mytext, use:
prefix_image='myimage';
prefix_data='mytext';
fileformat='.jpg';
dataformat='.txt';
folder='C:\Users\khaled\Documents\MATLAB\';
image = imread(strcat(folder,prefix_image,fileformat));
data=textread(strcat(folder,prefix_data,fileformat),'%f');