I have a character vector with repeated elements and want to extract each element once and at the sequence of appearance.

조회 수: 2 (최근 30일)
Hi all,
I have a character vector with repeated elements and want to extract each element once, at the sequence of appearance in the original file. I am attaching an example file. In fact, I need to get:
Group Frontal_L Ins_Cing_L etc..
I guess I will have to use regexp but not very experienced with this functionality. Any ideas very welcome.

채택된 답변

dpb
dpb 2018년 8월 17일
편집: dpb 2018년 8월 18일
[unames,ia,iu]=unique(strtrim(cellstr(Getcurvenames)),'stable');
unames will be the names that are unique in the overall list,
unames=Getcurvenames(ia); % ia is location vector in original list Getcurvenames
Getcurvenames=unames(iu); % rebuild the original from the indices in unique list
unames will be cellstr list of names, much more conveniently handled than are fixed-length char arrays.
ADDENDUM
I forgot about the request for initial order in the output; use the 'stable' option as shown above. To not return the group string, make a slight modification to eliminate it first --
cnames=strtrim(cellstr(Getcurvenames)); % convert to trimmed cellstr
cnames=cnames(~contains(cnames,'Group')); % keep only those ~='Group'
[unames,ia,iu]=unique(cnames)),'stable'); % and the unique list of those
  댓글 수: 3
GioPapas81
GioPapas81 2018년 8월 17일
Just for the history, I found a way to do that, slightly modifying your answer dpb:
[~,idx]=unique(strtrim(cellstr(Getcurvenames)));
% Sort by ascending order the indices idx=sort(idx);
% Convert to char array to a cell array NGetcurvenames=cellstr(Getcurvenames);
% Extract only the indexed arrays starting from zero to exclude the % unnecessary 'Group' curves. for i=2:length(idx);Extractcurvenames{i}=NGetcurvenames{idx(i)};end Extractcurvenames=Extractcurvenames(~cellfun('isempty',Extractcurvenames));
It now works! Thank you again!
dpb
dpb 2018년 8월 17일
Don't know what you did differently, works fine here--
>> [u,ia,iu]=unique(strtrim(cellstr(Getcurvenames)));
>> u
u =
15×1 cell array
{'Central_L' }
{'Central_R' }
{'Frontal_L' }
{'Frontal_R' }
{'Group' }
{'In Mask' }
{'Ins_Cing_L' }
{'Ins_Cing_R' }
{'Occipital_L' }
{'Occipital_R' }
{'Parietal_L' }
{'Parietal_R' }
{'Posterior_all'}
{'Temporal_L' }
{'Temporal_R' }
>>
Returning a single character is characteristic of still dealing with char() arrays that are just 2D arrays of bytes and not using the trailing : to address the full array; hence the conversion to cellstr() that addresses the full cell.
unique does return the unique values in sorted order by default, there is the 'stable' optional order input to return in discovered order if that's what's wanted--
>> [u,ia,iu]=unique(strtrim(cellstr(Getcurvenames)),'stable');
>> u
u =
15×1 cell array
{'Group' }
{'Frontal_L' }
{'Ins_Cing_L' }
{'Temporal_L' }
{'Occipital_L' }
{'Parietal_L' }
{'Central_L' }
{'Frontal_R' }
{'Ins_Cing_R' }
{'Temporal_R' }
{'Occipital_R' }
{'Parietal_R' }
{'Central_R' }
{'Posterior_all'}
{'In Mask' }
>>

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by