zenab
zenab

Reputation: 229

Get the file name without Extension in matlab

I want to get the image file name without the Extension in MATLAB. I have Tried to use (fileparts )function as:

[pathstr, name, ext, versn] = fileparts(filename); 

in this function the (filename) must be with complete Path in order to get the file name without extension in the the variable (name).

when I have just the file name like ('D10_11.jpg'), I get the following Error :

"Input must be a row vector of characters"

Please, if their is another function to solve this problem

Upvotes: 12

Views: 28656

Answers (3)

Vahid G
Vahid G

Reputation: 1

Sorry for the super late answer :(, but I was facing the same problem. When I was searching for the answer I got the same question asked by someone else. There is no problem with the query you have written, only problem I see here is you are missing the format of the filename location.

filename = 'C:\Users\Public\myfile.csv';

[pathstr,name,ext] = fileparts(filename);

Output is

pathstr =
C:\Users\Public
name =
myfile
ext =
.csv

Upvotes: -1

Jonas
Jonas

Reputation: 74940

From your error message, I guess that the input could be a cell array, rather than a char array.

Thus, instead of

[pathstr,name,ext] = fileparts(filename)

you'd have to write

[pathstr,name,ext] = fileparts(filename{1})

Upvotes: 14

gnovice
gnovice

Reputation: 125854

This works fine for me:

>> filename = 'D10_11.jpg';
>> [pathstr,name,ext,versn] = fileparts(filename)

pathstr =

     ''

name =

D10_11

ext =

.jpg

versn =

     ''

You should check to make sure filename is actually what you think it is. The error suggests that it isn't just a row vector of characters like 'D10_11.jpg'.

Upvotes: 5

Related Questions