Azzera filtri
Azzera filtri

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

2 visualizzazioni (ultimi 30 giorni)
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

Risposte (1)

Brahmadev
Brahmadev il 4 Ott 2023
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!

Categorie

Scopri di più su Large Files and Big Data in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by