Error with IF statement "Subscript indices must either be real positive integers or logicals."

2 ビュー (過去 30 日間)
Joseph Fortugno
Joseph Fortugno 2017 年 11 月 10 日
回答済み: Brahmadev 2023 年 10 月 4 日
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

回答 (1 件)

Brahmadev
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!

カテゴリ

Help Center および File ExchangeMultidimensional Arrays についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by