Effacer les filtres
Effacer les filtres

Info

Cette question est clôturée. Rouvrir pour modifier ou répondre.

How can I compare cells of 2 matrices to see if part of the string is the same?

1 vue (au cours des 30 derniers jours)
Anouk Heuvelmans
Anouk Heuvelmans le 28 Mar 2018
Clôturé : MATLAB Answer Bot le 20 Août 2021
I have 2 datasets for my subjects and due to blinding of the documents the order of the subjects in the 2 matrices I have does not match. Example:
cell array 1:
WTCdatanames_sham = {'WTC18_2_2_P16_whis_sham_rec' 'WTC18_2_1_P16_whis_sham_rec' 'WTC18_2_3_P16_whis_sham_rec'}
cell array 2:
WTCdatanames_stim = {'WTC18_2_2_P16_whis_stim_rec' 'WTC18_2_3_P16_whis_stim_rec' 'WTC18_2_1_P16_whis_stim_rec'}
Say, for every column in cell array 1, I want to use the first part (WTC18_2_2) to find the corresponding column in cell array 2. How would I be able to do this? Can I in some way write a loop in which it compares the first 10 letters from a cell in cell array 1 to all cells in cell array 2?
  2 commentaires
Greg
Greg le 28 Mar 2018

Did you read doc on strncmp?

Anouk Heuvelmans
Anouk Heuvelmans le 28 Mar 2018
I did, I tried the following: s1 = WTCshamrecnames(1,1); s2 = WTCstimrecnames(1,1); tf = strncmp(s1,s2,9) This should give me 1 because WTC18_2_2 is for both cell arrays in the first column but my output of tf is 0

Réponses (1)

Birdman
Birdman le 28 Mar 2018
This should do it:
loc='WTC18_2_2';
strncmp(loc,WTCdatanames_stim,numel(loc))
and this will return logical
1 0 0
which means loc is located in the first element of WTCdatanames_stim, not others.
  2 commentaires
Anouk Heuvelmans
Anouk Heuvelmans le 28 Mar 2018
Thanks for your help. However, this would only give me the location of the first one. instead of saying 'loc='WTC18_2_2'; I would like to have the program extracting that part for every column because my actual dataset is a lot bigger and I don't want to write out loc = ... for every single subject. Do you know of a way in which I can ask matlab to read out this first part from my cell? (I hope you understand my question)
Birdman
Birdman le 28 Mar 2018

Maybe this could be helpful:

 n=9; %corresponds to WTC_2_2, WTC_2_1 and WTC_2_3 respectively 
 for i=1:size(WTCdatanames_sham,2)
    idx(i,:)=strncmp(WTCdatanames_sham{i}(1:n),WTCdatanames_stim,n);
 end

Cette question est clôturée.

Community Treasure Hunt

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

Start Hunting!

Translated by