For loop in string array

조회 수: 11 (최근 30일)
Aakash Soni
Aakash Soni 2023년 1월 19일
댓글: Aakash Soni 2023년 1월 19일
Hello Matlab Community,
How can I loop though my string array to get indexes? Here is my code using for loop but it creates blank matix after running a for loop.
str = importfile("imageTimeline1.xlsx");
% individual data collection
Ind_1_start = find(contains(str,'Stimulus 1 Data'));
Ind_1_end = find(contains(str,'Questionnair 1 Data'));
Ind_2_start = find(contains(str,'Stimulus 2 Data'));
Ind_2_end = find(contains(str,'Questionnair 2 Data'));
% using for loop to collect index information
for ii = 1:20
Ind_start{:,ii} = find(contains(str,['Stimulus' num2str(ii) 'Data']));
Ind_end{:,ii} = find(contains(str,['Questionnair' num2str(ii) 'Data']));
end
Thank you,
Aakash.

채택된 답변

Walter Roberson
Walter Roberson 2023년 1월 19일
['Stimulus' num2str(ii) 'Data']
that does not generate spaces between the word and the number.
I suggest that you use sprintf()
  댓글 수: 1
Aakash Soni
Aakash Soni 2023년 1월 19일
Thank you for pointing out space issue in my array.
I found following code works fine for my application:
for ii = 1:20
Ind_start(ii,:) = find(contains(str,['Stimulus' ' ' num2str(ii) ' ' 'Data']));
Ind_end(ii,:) = find(contains(str,['Questionnair' ' ' num2str(ii) ' ' 'Data']));
end

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by