I would like to have the original string 'black.txt' to be parsed into a = 'black' and ext = '.txt'. Every filename/string is going to have the extension '.txt'. I'm wondering what would be the easiest way of achieving this in Matlab so that I can concatenate the new string appropriately. Thanks in advance.
A:
Have a look here at the Matlab Central Repository.
Probably 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.
%
Kevin Boyd
2009-08-13 23:19:04
A:
Actually, the standard strrep function from Matlab works well enough in my case.
stanigator
2009-08-13 23:44:44
Did you mean to say STRTOK (http://www.mathworks.com/access/helpdesk/help/techdoc/ref/strtok.html) instead of STRREP?
gnovice
2009-08-14 01:40:37
+1
A:
fileparts is probably better for this application.
E.g. [PATHSTR,NAME,EXT,VERSN] = fileparts('matlab_script.m');
Jacob
2009-08-14 01:17:48
+6
A:
I would suggest using the FILEPARTS function to parse a file name string. Here's an example:
>> fileString = '\home\matlab\black.txt';
>> [filePath,fileName,fileExtension] = fileparts(fileString)
filePath =
\home\matlab
fileName =
black
fileExtension =
.txt
You can then put the file string back together with simple string concatenation (for just the file name) or using the FULLFILE function (for an absolute or partial file path):
fileString = [fileName fileExtension]; % Just the file name
fileString = fullfile(filePath,[fileName fileExtension]); % A file path
Using FULLFILE is easier and more robust with respect to running your code on different operating systems, since it will choose the appropriate file separator for you ("\" for Windows or "/" for UNIX).
gnovice
2009-08-14 01:18:25