Selecting part of a string

I have a list of file names in a variable called file_name e.g:
[a.txt b.txt c.txt d.txt]
I would like to create a new variable which only contains:
[a b c d]
Is this possible?
Thanks in advance Matt

3 Comments

Gerd
Gerd on 14 Jun 2011
Hi Matt,
is your file_name variable just one string like
file_name = ['a.txt' 'b.txt' 'c.txt' 'd.txt']?
Matt
Matt on 14 Jun 2011
I'm not sure what you mean, it is a 4x1 matrix (4 rows, 1 column).
Thanks,
Matt
You say 4 rows and 1 column but your example is 4 columns and 1 row

Sign in to comment.

 Accepted Answer

surely not the best way to do it but here is my way
v=['a.txt' 'b.txt' 'c.txt' 'd.txt']
cell2mat(arrayfun(@(x)strtok(x,'.txt'),v,'uni',false))
or
v=['a.txt' 'b.txt' 'c.txt' 'd.txt']'
cell2mat(arrayfun(@(x)strtok(x,'.txt'),v,'uni',false))

3 Comments

Matt
Matt on 14 Jun 2011
Hi,
I'm getting the following error for both of the above methods
"Cannot support cell arrays containing cell arrays or
objects."
Thanks,
Matt
Matt
Matt on 14 Jun 2011
The following kind of works:
arrayfun(@(x)strtok(x,'.txt'),file_list,'uni',false)
but in the new variable there are quotes around the text e.g
'a.txt' is it possible to remove the quotes?
Matt
Matt on 14 Jun 2011
Hi,
this works perfectly:
x = strtok(file_list,'.txt')
thanks,
Matt

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 14 Jun 2011

Community Treasure Hunt

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

Start Hunting!