0

I have a cell array of which the last part looks like this:

Columns 8372 through 8375

    {'w20091231_2000.nc'}    {'w20091231_2020.nc'}    {'w20091231_2040.nc'}    {'w20091231_2100.nc'}

  Columns 8376 through 8379

    {'w20091231_2120.nc'}    {'w20091231_2140.nc'}    {'w20091231_2200.nc'}    {'w20091231_2220.nc'}

  Columns 8380 through 8383

    {'w20091231_2240.nc'}    {'w20091231_2300.nc'}    {'w20091231_2320.nc'}    {'w20091231_2340.nc'}

  Columns 8384 through 8387

    {'wD1.nc'}    {'wD2.nc'}    {'wD3.nc'}    {'wD4.nc'}

Now I want to rearrange this array so that it only contains the last four strings.{'wD1.nc'} {'wD2.nc'} {'wD3.nc'} {'wD4.nc'}

I tried

IndexC = strfind(names,'wD*.nc');
Index = find(not(cellfun('isempty',IndexC)))

and

Index = find(contains(names,'wD*.nc'));
names2=names(Index)

both work if wD*.nc is wD4.nc but then of course I only select the one value and not the four that I want. How do I get to use the * ?

2 Answers 2

1

I had to do some googling but found this https://www.mathworks.com/matlabcentral/answers/77039-comparing-strings-with-wildcards , and something like the following seems to work:

IndexC = regexp(names, regexptranslate('wildcard', 'wD*.nc'));
Index = find(not(cellfun('isempty',IndexC)));
names2=names(Index)
Sign up to request clarification or add additional context in comments.

Comments

1

In one line using regexp with the match option:

x = regexp([x{:}],'wD\d+\.nc','match')

1 Comment

Although using [x{:}] could give undesired results if there happened to be a match across consecutive elements of x. You could avoid this by replacing [x{:}] with something like strjoin(x,'#') to concatenate them with some non-matching character in between.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.