Getting error when i use a self define function in a for loop
조회 수: 8 (최근 30일)
이전 댓글 표시
I wrote a self define function to find a specific string in a given string, the function name is FindNumInStr.
function num=FindNumInStr(InputStr,Target)
s=num2cell(char(InputStr));
LengthT=length(char(Target));
t=0;
for i=1:length(s)
if [s{i:i+LengthT-1}]==Target
NumStartLocation=i+LengthT-1+2;
t=1;
end
if t && (s{i}==' ' || i==length(s))
if i==length(s)
NumEndLocation=i;
else
NumEndLocation=i-1;
end
break
end
end
num=str2double([s{NumStartLocation:NumEndLocation}]);
end
When InputStr="fRC Ll=3 Ls=2 TLl=9 TLs=4" and Target="TLs", I can get a num return by the function is 4.
I'm trying to use it in my script. I got a header_array which is 1x704 string, the function work perfect when i do
k=681;
T=FindNumInStr(header_array(k),"TLs");
i can get
T=4
but when i use the function as below
TLs_array=[];
for k=1:length(header_array)
TLs_array=[TLs_array,FindNumInStr(header_array(k),"TLs")];
end
I get error:
Index exceeds the number of array elements. Index must not exceed 25.
Error in FindNumInStr (line 6)
if [s{i:i+LengthT-1}]==Target
How can I fix this problem?
댓글 수: 2
Bhanu Prakash
2023년 4월 16일
Hi Chia,
Can you provide the "header_array", so that the error can be reproduced at my end?
채택된 답변
VBBV
2023년 4월 16일
편집: VBBV
2023년 4월 16일
As the error states, Index exceeds the number of array elements. Index must not exceed 25.
the length of InputStr is 25, you need to modify the for loop as shown below for the same reason when you try to access an element which is beyond the limit specified in array.
k=681;
InputStr="fRC Ll=3 Ls=2 TLl=9 TLs=4"
s=num2cell(char(InputStr))
% length of InputStr
length(s)
% length of Target
LengthT=length(char("TLs"))
% for loop limit
length(s)+LengthT-1
function num=FindNumInStr(InputStr,Target)
s=num2cell(char(InputStr));
LengthT=length(char(Target));
t=0;
% check by modifying for loop as below
for i=1:length(s)-2
if [s{i:i+LengthT-1}]==Target
NumStartLocation=i+LengthT-1+2;
t=1;
end
if t && (s{i}==' ' || i==length(s))
if i==length(s)
NumEndLocation=i;
else
NumEndLocation=i-1;
end
break
end
end
num=str2double([s{NumStartLocation:NumEndLocation}]);
end
댓글 수: 2
VBBV
2023년 4월 16일
The code seems to work when you modify the for loop as below
data = load('header_array.mat')
data.header_array(:)
H = length(data.header_array)
k=681;
InputStr="fRC Ll=3 Ls=2 TLl=9 TLs=4"
Target = "TLs"
% s=num2cell(char(InputStr))
% length of InputStr
% length(s)
% length of Target
% LengthT=length(char("TLs"))
% for loop limit
% length(s)+LengthT-1
k=681;
% T=FindNumInStr(data.header_array(k),"TLs")
TLs_array=[];
for k=1:H
TLs_array=[TLs_array,FindNumInStr(data.header_array(k),"TLs")];
end
TLs_array.'
function num=FindNumInStr(InputStr,Target)
s=num2cell(char(InputStr));
LengthT=length(char(Target));
t=0;
% check by modifying for loop as below
for i=1:length(s)-2
if [s{i:i+LengthT-1}]==Target
NumStartLocation=i+LengthT-1+2;
t=1;
end
% similarly this change
if t & (s{i}==' ' || i+2==length(s))
% this change
if i+2==length(s)
NumEndLocation=i+2;
else
NumEndLocation=i-1;
end
% break
end
end
num=str2double([s{NumStartLocation:NumEndLocation}]);
end
Please see the change done to these lines
for i=1:length(s)-2
if [s{i:i+LengthT-1}]==Target
NumStartLocation=i+LengthT-1+2;
t=1;
end
% similarly this change
if t & (s{i}==' ' || i+2==length(s))
% this change
if i+2==length(s)
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Whos에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!