AndreaF
AndreaF

Reputation: 12395

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?

Upvotes: 0

Views: 175

Answers (2)

Silverstorm
Silverstorm

Reputation: 15845

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

Upvotes: 2

il_raffa
il_raffa

Reputation: 5175

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    

Upvotes: 2

Related Questions