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?
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With