Getting error when i use a self define function in a for loop
8 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
chia ching lin
am 16 Apr. 2023
Kommentiert: VBBV
am 16 Apr. 2023
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 Kommentare
Bhanu Prakash
am 16 Apr. 2023
Hi Chia,
Can you provide the "header_array", so that the error can be reproduced at my end?
Akzeptierte Antwort
VBBV
am 16 Apr. 2023
Bearbeitet: VBBV
am 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"
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 Kommentare
VBBV
am 16 Apr. 2023
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)
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Whos finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!