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!