Say if we have this string
a = 'C:/my_folder/folder/mac/data/';
How can I use regexprep to reduce the string to:
'C:/my_folder/folder/mac/';
Actually, I found a way to do it.
[pathstr] = fileparts(a);
regexprep(pathstr, '(?<=/)[^/]*$', '')
You can try this method to cut 5 char at end of string
a = a(1:end-5)
Related
How to sort String ArrayList divided by "," separator?
In arraylist, string is dataType, and each index is stored as below.
someList[0] = "abc,xxx,1"
someList[1] = "abc,xxx,3"
someList[2] = "abc,xxx,2"
someList[3] = "abc,xxx,5"
someList[4] = "abc,xxx,4"
The problem is I want to split and rearrange based on the last number(1,3,2,5,4 -> 1,2,3,4,5). How could I achieve this? I would really appreciate for the answer
You can try this:
someList.sort((a, b)=>a.split(',').last.compareTo(b.split(',').last));
this question about matlab:
i'm running a loop and each iteration a new set of data is produced, and I want it to be saved in a new file each time. I also overwrite old files by changing the name. Looks like this:
name_each_iter = strrep(some_source,'.string.mat','string_new.(j).mat')
and what I#m struggling here is the iteration so that I obtain files:
...string_new.1.mat
...string_new.2.mat
etc.
I was trying with various combination of () [] {} as well as 'string_new.'j'.mat' (which gave syntax error)
How can it be done?
Strings are just vectors of characters. So if you want to iteratively create filenames here's an example of how you would do it:
for j = 1:10,
filename = ['string_new.' num2str(j) '.mat'];
disp(filename)
end
The above code will create the following output:
string_new.1.mat
string_new.2.mat
string_new.3.mat
string_new.4.mat
string_new.5.mat
string_new.6.mat
string_new.7.mat
string_new.8.mat
string_new.9.mat
string_new.10.mat
You could also generate all file names in advance using NUM2STR:
>> filenames = cellstr(num2str((1:10)','string_new.%02d.mat'))
filenames =
'string_new.01.mat'
'string_new.02.mat'
'string_new.03.mat'
'string_new.04.mat'
'string_new.05.mat'
'string_new.06.mat'
'string_new.07.mat'
'string_new.08.mat'
'string_new.09.mat'
'string_new.10.mat'
Now access the cell array contents as filenames{i} in each iteration
sprintf is very useful for this:
for ii=5:12
filename = sprintf('data_%02d.mat',ii)
end
this assigns the following strings to filename:
data_05.mat
data_06.mat
data_07.mat
data_08.mat
data_09.mat
data_10.mat
data_11.mat
data_12.mat
notice the zero padding. sprintf in general is useful if you want parameterized formatted strings.
For creating a name based of an already existing file, you can use regexp to detect the '_new.(number).mat' and change the string depending on what regexp finds:
original_filename = 'data.string.mat';
im = regexp(original_filename,'_new.\d+.mat')
if isempty(im) % original file, no _new.(j) detected
newname = [original_filename(1:end-4) '_new.1.mat'];
else
num = str2double(original_filename(im(end)+5:end-4));
newname = sprintf('%s_new.%d.mat',original_filename(1:im(end)-1),num+1);
end
This does exactly that, and produces:
data.string_new.1.mat
data.string_new.2.mat
data.string_new.3.mat
...
data.string_new.9.mat
data.string_new.10.mat
data.string_new.11.mat
when iterating the above function, starting with 'data.string.mat'
I am attempting to replace vowels in a string with another character using the function regexprep. For example,
content = regexprep( 'refrigerator', '[aeiou]', '!' )
content = r!fr!g!r!t!r
However, when I attempt to use a string variable in place of the character array, it does not seem to work:
allowedChar = 'aeiou';
content = regexprep( 'refrigerator', allowedChar, '!' )
content = refrigerator
How do I fix this problem?
You forgot the [] characters in allowedChar. It should be:
allowedChar = '[aeiou]';
content = regexprep( 'refrigerator', allowedChar, '!' )
For such a simple substitution you could use ismember instead of regexprep:
content(ismember(content, 'aeiou')) = '!';
I need to split a string into two components. As an example I have the string:
s = 'Hello1_1000_10_1_data'
and I want to split it into the two strings
str1 = 'Hello1_1000_10_1'
and
str2 = '_data'
the important point is that I can't be too sure of the format of the first string, the only thing that is sure is that the 'suffix' which is to be read into the second string always reads '_data'. What is the best way to do this? I looked up the documentation on strtok and regexp but they do not seem to offer me what I want.
If you always know the length of the suffix, you could just use that:
s = 'Hello1_1000_10_1_data'
str1 = s(1:end-5)
Or otherwise:
s = 'Hello1_1000_10_1_data'
suffix = length('_data')
str1 = s(1:end-suffix)
You can use:
s = 'Hello1_1000_10_1_data';
str = regexp(s, '(.*)(_data)', 'tokens'){1};
str{1} %// Hello1_1000_10_1
str{2} %// _data
If _data occurs several times in the file name, this will still work.
You can also use strsplit() as follow:
s = 'Hello1_1000_10_1_data';
suffix = '_data';
str = strsplit(s,suffix);
str1 = str{1}
In addition, you can use strsplit() with multiple delimiters.
You can use strfind():
s = 'Hello1_1000_10_1_data';
suffix = '_data';
i = strfind(s,suffix);
if any(i)
i = i(end);
prefix = s(1:i-1);
end
I have the following string in MATLAB, for example
##%%F1_USA(40)_u
and I want
F1_USA_40__u
Does it has any function for this?
Your best bet is probably regexprep which allows you to replace parts of a string using regular expressions:
s_new = regexprep(regexprep(s, '[()]', '_'), '[^A-Za-z0-9_]', '')
Update: based on your updated comment, this is probably what you want:
s_new = regexprep(regexprep(s, '^[^A-Za-z0-9_]*', ''), '[^A-Za-z0-9_]', '')
or:
s_new = regexprep(regexprep(s, '[^A-Za-z0-9_]', '_'), '^_*', '')
One way to do this is to use the function ISSTRPROP to find the indices of alphanumeric characters and replace or remove the others accordingly:
>> str = '##%%F1_USA(40)_u'; %# Sample string
>> index = isstrprop(str,'alphanum'); %# Find indices of alphanumeric characters
>> str(~index) = '_'; %# Set non-alphanumeric characters to '_'
>> str = str(find(index,1):end) %# Remove any leading '_'
str =
F1_USA_40__u %# Result
If you want to use regular expressions (which can get a little more complicated) then the last suggestion from Tamas will work. However, it can be greatly simplified to the following:
str = regexprep(str,{'\W','^_*'},{'_',''});