How can I parse a file name string in MATLAB?

I would like the original string 'black.txt' parsed for a = 'black' and ext = '.txt' . Each file name / line will have the extension '.txt' . I am wondering what would be the easiest way to achieve this in MATLAB so that I can appropriately bind a new line?

+4
source share
4 answers

In fact, the standard strrep function from Matlab works quite well in my case.

+1
source

I would suggest using the FILEPARTS function to parse a file name string. Here is an example:

 >> fileString = '\home\matlab\black.txt'; >> [filePath,fileName,fileExtension] = fileparts(fileString) filePath = \home\matlab fileName = black fileExtension = .txt 

You can then put the line of the file along with a simple concatenation of lines (only for the file name) or with the FULLFILE function (for an absolute or partial path to the file):

 fileString = [fileName fileExtension]; %# Just the file name fileString = fullfile(filePath,[fileName fileExtension]); %# A file path 

Using FULLFILE is simpler and more reliable in terms of running your code on different operating systems, as it will select the appropriate file separator for you ("\" for Windows or "/" for UNIX).

+10
source

FileParts is probably better for this application.

eg. [PATHSTR, NAME, EXT, VERSN] = fileparts ('matlab_script.m');

+2
source

Take a look here at the Matlab Central repository.

Perhaps this is what you want.

 %EXPLODE Splits string into pieces. % EXPLODE(STRING,DELIMITERS) returns a cell array with the pieces % of STRING found between any of the characters in DELIMITERS. % 
0
source

All Articles