I want to extract the" name" of a filename without the extension- How do I use textscan to do this?

60 views (last 30 days)
So what is the option to specify when to stop reading if a period (.) is encountered? name=textscan(filename,???)

Accepted Answer

Image Analyst
Image Analyst on 11 Sep 2013
Edited: Image Analyst on 9 Mar 2020
The question in the subject line ("extract the" name" of a filename without the extension") can be solved with fileparts():
[folder, baseFileNameNoExt, extension] = fileparts(fullFileName);
For the question in the body of the message, there is not enough information, but I'll take a guess at this:
% So what is the option to specify when to stop reading if a period (.) is encountered? name=textscan(filename,???)
fileName = 'abc.567.90xyz.txt'
dotLocations = find(fileName == '.')
if isempty(dotLocations)
% No dots at all found so just take entire name.
nameBeforeFirstDot = fileName
else
% Take up to , but not including, the first dot.
nameBeforeFirstDot = fileName(1:dotLocations(1)-1)
end

More Answers (0)

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!