Getting error when i use a self define function in a for loop
1 次查看(过去 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!