Matching first n characters of string in array

3 views (last 30 days)
Malicule
Malicule on 10 Jul 2017
Answered: KSSV on 11 Jul 2017
Hi,
My data is organized in the following way:
image1_control
image1_drug
image1_drug2
image2_control
image2_drug2
image3_control
image4_drug
image4_drug2
etc
with each "image" file as a structure containing all information about the image.
I would like to write a function that allows me to group each image, with its various conditions, into a cell array.
I tried using strcmp but it would require pairwise comparison of each string.
Can you please help?
Thank you!
[Merged from duplicate]
Hi,
My data looks like this:
image1_control,
image1_drug,
image1_drug2,
image2_control,
image2_drug2,
image3_drug,
image4_control,
image4_drug2,
etc
These are all field names of a structure containing all the "images".
I would like to group each image and its various conditions based on the field name.
I have tried using strcmp, but I would have to create a for loop to conduct a pairwise comparison. I'm having trouble using regexp and indexing all "images" with the matching names.
Can someone help me find a more efficient way to group field names that contain matching first 7 letters?
Thanks!

Answers (1)

KSSV
KSSV on 11 Jul 2017
str = {'image1_control','image1_drug','image1_drug2','image2_control','image2_drug2',.......
'image3_drug','image4_control','image4_drug2'} ;
%%Seperate drug
idx1 = strfind(str, 'drug');
idx1 = find(not(cellfun('isempty', idx1)));
str1 = str(idx1)
%%Seperate control
idx2 = strfind(str, 'control');
idx2 = find(not(cellfun('isempty', idx2)));
str2 = str(idx2)

Categories

Find more on Structures in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!