Getting error when i use a self define function in a for loop
2 ビュー (過去 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!