Matching first n characters of string in array

조회 수: 3 (최근 30일)
Malicule
Malicule 2017년 7월 10일
답변: KSSV 2017년 7월 11일
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!

답변 (1개)

KSSV
KSSV 2017년 7월 11일
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)

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by