How to fprintf(or just print) fileparts

3 views (last 30 days)
man... I have read fileparts command, and i just can't do it... I am in a hurry right now..
I have files named such as 1.0 f.txt, 1.5 f.txt, 1.6 f.txt, 2.0 f.txt .... so on.
I want to fprintf or print only the numbers from the file name, such as 1.0, 1.5, 1.6, 2.0... so on.
I guess I have to use fileparts command, but don't quite understand how to separate the numbers, space, words, and extension..
Can anyone give me simple example or solution to this?

Accepted Answer

Walter Roberson
Walter Roberson on 26 Jan 2016
If "filename" is the name of the file, then
parts = strsplit(filename, ' ');
and then parts{1} would be the string up to (but excluding) the space.
fprintf('The parameter value was: %s\n', parts{1})
  3 Comments
Walter Roberson
Walter Roberson on 26 Jan 2016
strsplit was added in R2013a. If you are using a MATLAB older than that then you need to indicate which version so that we can avoid wasting our time proposing solutions that cannot work for your version.
On the other hand,
idx = find(filename == ' ', 1, 'first');
part1 = filename(1:idx-1);
fprintf('The parameter value was: %s\n', part1);
yc j
yc j on 26 Jan 2016
Thank you for the kind answers! means a lot to me!

Sign in to comment.

More Answers (0)

Categories

Find more on Environment and Settings in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!