string match and replace string with matching number from another matrix

조회 수: 2 (최근 30일)
Phillip
Phillip 2023년 11월 27일
댓글: Phillip 2023년 11월 27일
Hi All,
I'm stuck. I have a file called StimNo.mat that contains the filenames of over 2000 soundfiles (e.g. 'air.wav', 'air_2.wav', 'chair.wav' etc) in the first column. The second column has the associated number (e.g. 1, 2, 3 etc).
I have another file like it with a small subset of the .wav names. I'd like to replace the names of the .wav file with the number from Column 2 in StimNo.mat. How might I do that please? The .wav files are not consistently labeled so need to match the full string.
Kind regards
Phil

채택된 답변

Paul
Paul 2023년 11월 27일
편집: Paul 2023년 11월 27일
From the desription, it sounds like the StimNo file contains a cell array (assuming all file names are unique)
stimno = {'air.wav' 1;'air2.wav' 2;'chair.wav' 3;'chair2.wav' 4}
stimno = 4×2 cell array
{'air.wav' } {[1]} {'air2.wav' } {[2]} {'chair.wav' } {[3]} {'chair2.wav'} {[4]}
and the second file is something like
secondfile ={'chair2.wav'; 'air2.wav'}
secondfile = 2×1 cell array
{'chair2.wav'} {'air2.wav' }
And the result would be
[~,locb] = ismember(secondfile,stimno(:,1));
locb
locb = 2×1
4 2
result = stimno(locb,2)
result = 2×1 cell array
{[4]} {[2]}
  댓글 수: 1
Phillip
Phillip 2023년 11월 27일
OH...ismember!! (I was messing around with strcmp etc.) Thank you very much!!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by