0

I have this string:

aa= 'SAN/MOST/MOD10A1.005/2000.02.26/MOD10A1.A2000057.h01v10.005.2007163034521.hdf.xml' 

I want to change it to:

'MOD10A1.A2000057.h01v10.005.2007163034521.hdf.xml'

How can i do that by using Matlab?? I try this code but it does not work correctly.

s=regexp(aa, '[_.]', 'split');

Anybody can help??

user34484
  • 11
  • 5

3 Answers3

2

You can use the strfind function instead:

idx = strfind(aa,'/');
s = aa(idx(end)+1:end)
Highman
  • 145
  • 6
2

strfind is only available from R2013a and onwards. You can certainly use regexp like you have done before, but look for the / symbol instead. Look for the last occurrence of the / symbol, then use that and subset the rest of your string just like what Highman is doing. In other words:

aa = 'SAN/MOST/MOD10A1.005/2000.02.26/MOD10A1.A2000057.h01v10.005.2007163034521.hdf.xml';
idx = regexp(aa, '\/');
aaSubset = aa(idx(end)+1 : end);

Take note that I had to use the \ character and place it before the / character as / is a reserved symbol when looking at regular expressions. aaSubset contains the string that you're looking for. I get:

aaSubset =

MOD10A1.A2000057.h01v10.005.2007163034521.hdf.xml
rayryeng
  • 102,964
  • 22
  • 184
  • 193
2

Another approach using regular expressions (specifically regexprep):

s = regexprep(aa, '^.*\/', '');

This greedily looks for any sequence of characters beginning at the start of the string and ending in /, and removes that (replaces it by an empty string).


You could also use fileparts:

[folder, name, ext] = fileparts(aa);
s = [name ext];
Community
  • 1
  • 1
Luis Mendo
  • 110,752
  • 13
  • 76
  • 147