Getting error when i use a self define function in a for loop

2 views (last 30 days)
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?

Accepted Answer

VBBV
VBBV on 16 Apr 2023
Edited: VBBV on 16 Apr 2023
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"
InputStr = "fRC Ll=3 Ls=2 TLl=9 TLs=4"
s=num2cell(char(InputStr))
s = 1×25 cell array
Columns 1 through 22 {'f'} {'R'} {'C'} {' '} {'L'} {'l'} {'='} {'3'} {' '} {'L'} {'s'} {'='} {'2'} {' '} {'T'} {'L'} {'l'} {'='} {'9'} {' '} {'T'} {'L'} Columns 23 through 25 {'s'} {'='} {'4'}
% length of InputStr
length(s)
ans = 25
% length of Target
LengthT=length(char("TLs"))
LengthT = 3
% for loop limit
length(s)+LengthT-1
ans = 27
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 Comments
chia ching lin
chia ching lin on 16 Apr 2023
Hi @VBBV, thanks for answering, but the function success when InputStr is one at a time. It only fail when I call the function inside the for loop of my script.
VBBV
VBBV on 16 Apr 2023
The code seems to work when you modify the for loop as below
data = load('header_array.mat')
data = struct with fields:
header_array: ["fRC Ll=2 Ls=1 TLl=0 TLs=0" "cRC Ll=2 Ls=1 TLl=0 TLs=0" "fRC Ll=2 Ls=1 TLl=0 TLs=1" "cRC Ll=2 Ls=1 TLl=0 TLs=1" "fRC Ll=2 Ls=1 TLl=0 TLs=2" … ]
data.header_array(:)
ans = 704×1 string array
"fRC Ll=2 Ls=1 TLl=0 TLs=0" "cRC Ll=2 Ls=1 TLl=0 TLs=0" "fRC Ll=2 Ls=1 TLl=0 TLs=1" "cRC Ll=2 Ls=1 TLl=0 TLs=1" "fRC Ll=2 Ls=1 TLl=0 TLs=2" "cRC Ll=2 Ls=1 TLl=0 TLs=2" "fRC Ll=2 Ls=1 TLl=0 TLs=3" "cRC Ll=2 Ls=1 TLl=0 TLs=3" "fRC Ll=2 Ls=1 TLl=0 TLs=4" "cRC Ll=2 Ls=1 TLl=0 TLs=4" "fRC Ll=2 Ls=1 TLl=0 TLs=5" "cRC Ll=2 Ls=1 TLl=0 TLs=5" "fRC Ll=2 Ls=1 TLl=0 TLs=6" "cRC Ll=2 Ls=1 TLl=0 TLs=6" "fRC Ll=2 Ls=1 TLl=0 TLs=7" "cRC Ll=2 Ls=1 TLl=0 TLs=7" "fRC Ll=2 Ls=1 TLl=1 TLs=0" "cRC Ll=2 Ls=1 TLl=1 TLs=0" "fRC Ll=2 Ls=1 TLl=1 TLs=1" "cRC Ll=2 Ls=1 TLl=1 TLs=1" "fRC Ll=2 Ls=1 TLl=1 TLs=2" "cRC Ll=2 Ls=1 TLl=1 TLs=2" "fRC Ll=2 Ls=1 TLl=1 TLs=3" "cRC Ll=2 Ls=1 TLl=1 TLs=3" "fRC Ll=2 Ls=1 TLl=1 TLs=4" "cRC Ll=2 Ls=1 TLl=1 TLs=4" "fRC Ll=2 Ls=1 TLl=1 TLs=5" "cRC Ll=2 Ls=1 TLl=1 TLs=5" "fRC Ll=2 Ls=1 TLl=1 TLs=6" "cRC Ll=2 Ls=1 TLl=1 TLs=6"
H = length(data.header_array)
ans = "fRC Ll=3 Ls=2 TLl=9 TLs=4"
H = 704
k=681;
InputStr="fRC Ll=3 Ls=2 TLl=9 TLs=4"
InputStr = "fRC Ll=3 Ls=2 TLl=9 TLs=4"
Target = "TLs"
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.'
ans = 704×1
0 0 1 1 2 2 3 3 4 4
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)

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!