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.
Related
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.
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?
We have a number of internal image formats which I process in Matlab. I have read/write functions for all of them. For specificity, consider the TGA image format, for which there is a file exchange image reader.
Matlab has reasonable drag and drop support for image formats supported by imread.
That is, you can drag an image from explorer, drop it on the "Workspace" pane, and Matlab will read in the image, and copy it into your workspace.
I'd like to be able to add drag and drop support, and imread support for TGA files. (imread has nice autocomplete for filenames for instance, tga_read_image does not.)
I think this is what you are looking for. Quoting the official documentation:
open name opens the specified file or variable in the appropriate
application
You can extend the functionality of open by defining your own
file-handling function of the form openxxx, where xxx is a file
extension. For example, if you create a function openlog, then the
open function calls openlog to process any files with the .log
extension. The open function returns any single output defined by your
function.
For example:
function opentga(file)
% Your logic for reading and, eventually,
% displaying TGA files when drag and drop
% or other opening events occur.
end
And here is a full working example directly taken from the link:
function opentxt(filename)
[~, name, ext] = fileparts(filename);
fprintf('You have requested file: %s\n', [name ext]);
if exist(filename, 'file') == 2
fprintf('Opening in MATLAB Editor: %s\n', [name ext]);
edit(filename);
else
wh = which(filename);
if ~isempty(wh)
fprintf('Opening in MATLAB Editor: %s\n', wh);
edit(wh);
else
warning('MATLAB:fileNotFound', ...
'File was not found: %s', [name ext]);
end
end
end
An alternative path consists in overloading the uiopen function, as shown in this File Exchange release.
Starting off from Tommaso's answer, I created the following M-file on my MATLAB path:
function out = openics(filename)
img = readim(filename);
if nargout==1
out = img;
else
[~,varname] = fileparts(filename);
disp(['assigning into base: ',varname])
assignin('base',varname,img);
end
Dragging and dropping an ICS file onto MATLAB's command window shows the following on the command line:
>> uiopen('/Users/cris/newdip/examples/cermet.ics',1)
assigning into base: cermet
Check:
>> whos cermet
Name Size Bytes Class Attributes
cermet 256x256 65714 dip_image
Reading the code for uiopen (you can just type edit uiopen) shows that this calls open with the filename, which then calls openics with the filename and no output argument.
You can also type
img = open('/Users/cris/newdip/examples/cermet.ics');
to call openics and load the image into variable img.
NOTE 1: I'm using ICS because I don't have any TGA images to test with. ICS is a microscopy image file format.
NOTE 2: readim is a function in DIPimage
NOTE 3: This is cool, I had never bothered trying to drag and drop files onto MATLAB before. :)
The other answers address the "drag and drop" question. They do not address the question of how to integrate a proprietary image format into imread. This can be done fairly straight forwardly with the imformats command.
The issue of how/why it took me 3.5 years to figure that out will remain unanswered I'm afraid.... The feature has been around for 15+ years.
I wrote a MATLAB program with a GUI (to enter the measurement settings) and a measurement function which gets called when pressing "START" in the GUI.
In both I use separate files for sub functions to keep it easier to read and maintain.
The file structure looks something like this
C:/../folder/+measure/measure.m
C:/../folder/+measure/getData.m
C:/../folder/+measure/plot.m
C:/../folder/+measure/evalutate.m
C:/../folder/+measureGUI/getGuiData.m
C:/../folder/+measureGUI/calcLimits.m
C:/../folder/+measureGUI/saveGuiState.m
C:/../folder/+measureGUI/loadGuiState.m
C:/../folder/+measureGUI/background.png
C:/../folder/+measureGUI/guiState.mat
C:/../folder/measureGUI.fig
C:/../folder/measureGUI.m
This works, if I'm executing the measureGUI.m in "folder".
The current settings in the GUI are saved in the guiState.mat file when closing the GUI in saveGuiState.m
filename = '+autoProberGUI/guiState.mat';
save(filename, 'guiState');
And loaded (by loadGuiState.m) the next time the GUI gets opened.
Now I have to put the finished program on a network drive and add the folder to my matlab search path to call measureGUI.m.
The program works but it can't save or load the guiState.mat due to the relative path (I guess the path is relative to the folder I'm currently in, and not the folder the calling function is in).
I think I could include the subfolder to the search path or use an absolute path in filename. But both solutions seem to me to be not the proper way.
Is there a way to have relative paths to the file from where the function is located on the drive? Meaning relative to
I:/..NetworkDrive../folder/+measureGUI/saveGuiState.m
instead of relative from where I call measureGUI.m
(Sorry for the poor English, I hope it is not too confusing)
You can use pwd to get the full path to you current working directory.
Then you can concatenate with [pwd '/folder/+measureGUI/saveGuiState.m'].
To locate the function you can use which.
I am reading 50 files from a folder as follows:
list_of_files=dir(fullfile('/home/user/Desktop/MTP/schemes/o33smnpimp/data/', '*.dat'));
My problem is until & unless I have same exact folder opened as path in MATLAB path (one above the path window) this command won't work. What is the reason behind this? Actually there are multiple schemes and every time I need to run a particular scheme, I have to go to the data folder of that particular scheme. How can it be solved?
The issue is that you can get the list of files using the full path like you have but you ALSO need to specify the full path when you use it. For example, try changing your code to:
baseDir = '/home/user/Desktop/MTP/schemes/o33smnpimp/data/'; % <--- will use this twice
list_of_files=dir(fullfile(baseDir, '*.dat'));
for ind = 1:length(list_of_files)
myFilenameFull = fullfile(baseDir, list_of_files(ind).name); % <---- must use fullfile here too!
D1 = getData(myFilenameFull, 'stuff');
end