The code works but it gives this warning. How to remove this warning?

3 次查看(过去 30 天)
In the attachment, run the "main.m" file. It works but it continuiusly gives the following warning types:
Warning: Integer operands are required for colon operator when used as index.
> In L2_Array (line 14)
In monoWithArrayByAskic1 (line 9)
In main>@(b)monoWithArrayByAskic1(b,u) (line 16)
In fpa1 (line 36)
In main (line 16)
How to remove these warnings?

采纳的回答

Steven Lord
Steven Lord 2023-3-3
Looking at the contents of the attached L2_Array1.m:
textOfFile = readlines('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1313370/L2_Array1.m')
textOfFile = 10×1 string array
"function r=L2_Array1(N)" "d = 0.5;" "N_on_each_axis = (N-1)/3;" "r = zeros(3*N_on_each_axis+1,3);" "v = (N_on_each_axis*d:-d:d).';" "r(2:N_on_each_axis+1,1) = v;" "r(N_on_each_axis+2:2*N_on_each_axis+1,2) = v;" "r(2*N_on_each_axis+2:3*N_on_each_axis+1,3) = v;" "r;" "end"
If N is not one more than a multiple of 3, N_on_each_axis is not an integer value. When you use it in constructing the indices into r, those indices aren't integer values and will cause that warning to be issued, the same way the code below does.
x = 1:10;
y = x(2.5:2.5:10)
Warning: Integer operands are required for colon operator when used as index.
y = 1×4
3 5 8 10
You probably want to round the indices.
y = x(round(2.5:2.5:10))
y = 1×4
3 5 8 10
  1 个评论
Sadiq Akbar
Sadiq Akbar 2023-3-3
Thanks a lot for your kind resppnse dear Steven Lord. I replaed N by 7 which is one more than multiple of 3. Likewise I replaced M by 10 which again is one more than multiple of 3. But still it gives the same warnings.

请先登录,再进行评论。

更多回答(1 个)

Jan
Jan 2023-3-3
You call the function L2_Array in ByAskic.m with the arguments 6 and 10.
Inside L2_Array you calculate
N_on_each_axis = (N-1)/3;
r = zeros(3*N_on_each_axis+1,3);
v = (N_on_each_axis*d:-d:d).';
r(2:N_on_each_axis+1,1) = v;
r(N_on_each_axis+2:2*N_on_each_axis+1,2) = v;
For N=6 and 10, N_on_each_axis is 5/3 and 3. In the first case, this is not an integer and as the error message tells you, this is not valid for indexing. Indices must be positive integers.
  2 个评论
Sadiq Akbar
Sadiq Akbar 2023-3-4
Thank you very much dear Jan for your kind response. You are right. But when I replaced N=6 by N=7, and ran it, still it gives the same warnings.
Sadiq Akbar
Sadiq Akbar 2023-3-4
Sorry it is my mistake. Yes, you both are right. Actually I have saved the ByAsic.m with two different names. So I did changes in ByAskic.m but not in the other and inside the main, I was calling the other which I forgot to replace with ByAskic.m. I want to accept the answers of you both. But due to Mathworks restrictions, I have to accept only one. As Steven Lord was the first who has given me the answer. So, I am going to give vote to Jan and accept the answer of Steven Lord. Thanks to you both once again.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Simulink 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by