user34484
user34484

Reputation: 11

Changing a string to smaller one

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??

Upvotes: 0

Views: 65

Answers (3)

Luis Mendo
Luis Mendo

Reputation: 112749

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];

Upvotes: 2

rayryeng
rayryeng

Reputation: 104545

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

Upvotes: 2

Highman
Highman

Reputation: 145

You can use the strfind function instead:

idx = strfind(aa,'/');
s = aa(idx(end)+1:end)

Upvotes: 2

Related Questions