Error with IF statement "Subscript indices must either be real positive integers or logicals."
조회 수: 2 (최근 30일)
이전 댓글 표시
So I'm trying to do manipulate some data from a csv file but its throwing an error for my IF statement
Subscript indices must either be real positive integers or logicals.
Error in data_manip (line 23) if (logical(data(n)) > logical(on_peak)
onpeak = 1.4; data = csvread(datafile); charge_rate = 100;
for n = 0:20
if (logical(data(n)) > logical(on_peak))
%capLost = data(n) - on_peak;
%cap = cap - capLost;
%Load(n) = on_peak;
%onoff(n) = 1;
end
end
댓글 수: 0
답변 (1개)
Brahmadev
2023년 10월 4일
Hi Joseph,
I understand that you would like to index through 'data' using the indexing variable 'n'. The error you're encountering is due to the incorrect usage of indices in the code. In MATLAB, indices start from 1, not 0. Therefore, when you use n = 0:20, the indices in the subsequent if statement are out of bounds. You can refer to the documentation link below to know more about array indexing.
To fix this issue, you can modify the loop to start from 1 instead of 0. Additionally, you can remove the logical function calls in the if statement since they are unnecessary. You can modify the for loop as shown below:
for n = 1:20
if (data(n) > on_peak)
% Calculation
end
end
Hope this helps in resolving your issue!
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Large Files and Big Data에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!