How to search a word both uppercase and lowercase given a specified string?

MIM Maestro puts volume data inside the table variable names:
Rectum (4)(Volume: 57.77) Bladder (5)(Volume: 139.40)
However, sometimes the name is lowercase, e.g. 'bladder'. If one wishes to pull the volume '139.40' from the CSV file, how can one search for both 'Bladder' and 'bladder' in a single query given an input string?
The following function only searches the entered string with regexp; if the input organ string is 'Bladder', doesn't regexp neglect 'bladder'?
function output = GetVolumesFromDVH(organ)
[omitting code to loop through multiple files...]
searchstring = [organ,'_'];
[...]
output(fileloop) = GetOrgVol(filepath,searchstring);
[...]
end
function volume = GetOrgVol(filepath,searchstring)
% get list of variable names
opts = detectImportOptions(filepath, 'NumHeaderLines', 1);
% we find the column containing the organ volume
OrganSearching = regexp(opts.VariableNames,searchstring);
for loop = 1:numel(opts.VariableNames)
if OrganSearching{loop} == 1
index = loop;
end
end
% extract volume from said string
volume = extractAfter(opts.VariableNames{index},'Volume_');
volume = sscanf(strrep(volume,'_','.'),'%f');
end

1 Kommentar

Daniel Bridges
Daniel Bridges am 15 Mär. 2018
Bearbeitet: Daniel Bridges am 15 Mär. 2018
I wonder if one can use the StringToUpper function (C library?) together with an 'or'-type command and lowercase input string to search both terms.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Star Strider
Star Strider am 15 Mär. 2018
The regexpi (link) function is the case-insensitive version of regexp.

3 Kommentare

Thank you! I suspected it was something "easy". I'm embarrassed to not have taken a closer look at the related functions in the documentation.
That increases my MATLAB R2017b runtime from 17 s to 25 s (searching 40 files, storing 40 volumes in a column vector), but I suppose it cannot be helped, that it's in fact searching multiple times for each variant. (Right? e.g. given 'rectum': 'Rectum', 'rEctum', 'reCTum', etc.)
I'm downloading R2018a now, wondering if it will be any faster.
As always, my pleasure!
Thanks for the info on R2018a. I didn’t know it was available. (It wasn’t when I checked a earlier this week.) I’ll follow your lead.
R2018a executed it in 6 s rather than 25 s, and clear and trying again it did so in 1 s. Hooray!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Just cast the string to lower and use a lower case template:
if contains(lower(yourString), 'bladder')
% yourString contains bladder somewhere in it
end
or you might use strcmpi() if you're sure there is no other characters in your string other than the template you're searching for:
if strcmpi(yourString, 'bladder')

Kategorien

Community Treasure Hunt

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

Start Hunting!

Translated by