How to solve "Attempted to access index 0 of an array with smaller dimension sizes.
Afficher commentaires plus anciens
I'm trying to compare current data with previous data like data(i)-data(i-1) in simulink. 'b' is the input from workspace where i had transfer the data from excel. When i run the simulink the error as stated in the title occured. Anyone here know how to solve?
function [y,z]= fcn(b)
% b=current input PV power,
%y= power fluctuation, z=smoothed output
for i = 1:1440;
y = (b(i))-(b(i-1))/6240*100;
if (y>10)
z = b(i-1) - [(abs(y-10))*6240/100];
elseif (y<-10)
z=b(i-1) + [(abs(y+10))*6240/100];
else
z = b(i);
end
end
Réponses (1)
Mehmed Saad
le 18 Avr 2020
Modifié(e) : Mehmed Saad
le 18 Avr 2020
function [y,z]= fcn(b)
% b=current input PV power,
%y= power fluctuation, z=smoothed output
for i = 2:length(b);
y = (b(i))-(b(i-1))/6240*100;
if (y>10)
z = b(i-1) - [(abs(y-10))*6240/100];
elseif (y<-10)
z=b(i-1) + [(abs(y+10))*6240/100];
else
z = b(i);
end
end
25 commentaires
Kenneth Yee
le 18 Avr 2020
Mehmed Saad
le 18 Avr 2020
Modifié(e) : Mehmed Saad
le 18 Avr 2020
see the updae
for i = 2:length(b)
also it seems like b is not an array in your code
according to logic b should ve length of 1440
Kenneth Yee
le 18 Avr 2020
Mehmed Saad
le 18 Avr 2020
can you post the project
Mehmed Saad
le 18 Avr 2020
In case if b is a variable i..e only 1 value come out of it then the function should be
function [y,z]= fcn(b,b0)
% b=current input PV power,
y = (b-b0)/6240*100;
if (y>10)
z = b0 - [(abs(y-10))*6240/100];
elseif (y<-10)
z=b0 + [(abs(y+10))*6240/100];
else
z = b;
end
end
and the block should be like

i.e. add a delay
Kenneth Yee
le 18 Avr 2020
Mehmed Saad
le 18 Avr 2020
please attach the variabl input
Kenneth Yee
le 18 Avr 2020
Mehmed Saad
le 18 Avr 2020
Run this code before runing simulink model
T=readmatrix('PvPower.xlsx');
t = T(:,1);
x = T(:,2);
so that variable x and t are in your workspace
Changes i ve made are following
- time and data from workspace

- adding delay

- Changing the function for step by step process
function [y,z]= fcn(b,b0)
% b=current input PV power,
%y= power fluctuation, z=smoothed output
y = (b-b0)/6240*100;
if (y>10)
z = b0 - (abs(y-10))*6240/100;
elseif (y<-10)
z=b0 + (abs(y+10))*6240/100;
else
z = b;
end
end
Mehmed Saad
le 18 Avr 2020
or you can also use this
Kenneth Yee
le 18 Avr 2020
Kenneth Yee
le 18 Avr 2020
Mehmed Saad
le 18 Avr 2020
Run this script first in matlab, so that x and t are in your workspace
T=readmatrix('PvPower.xlsx');
t = T(:,1);
x = T(:,2);
R2016a version is attached
Kenneth Yee
le 18 Avr 2020
Mehmed Saad
le 18 Avr 2020
Modifié(e) : Mehmed Saad
le 18 Avr 2020
filename = 'complete path of your excel file you want to read along with filename';
T=readmatrix(filename);
Kenneth Yee
le 18 Avr 2020
Mehmed Saad
le 18 Avr 2020
Modifié(e) : Mehmed Saad
le 18 Avr 2020
Error was occuring maybe because readmatrix was not in R2016a, instead of that we use xlsread
step-1: Run this code in matlab script and select your excel file
[file_n,folder_n] = uigetfile('*.xlsx');
filename = strcat(folder_n,file_n);
T=xlsread(filename);
t = T(:,1);
x = T(:,2);
Kenneth Yee
le 18 Avr 2020
Mehmed Saad
le 18 Avr 2020
Run this only, donot edit anything, it will open a window explorer infront of you, Now select the excel file you want to open
[file_n,folder_n] = uigetfile('*.xlsx');
filename = strcat(folder_n,file_n);
T=xlsread(filename);
t = T(:,1);
x = T(:,2);
Kenneth Yee
le 18 Avr 2020
Mehmed Saad
le 18 Avr 2020
Modifié(e) : Mehmed Saad
le 18 Avr 2020
it is working perfectly in R2017a

Kenneth Yee
le 19 Avr 2020
I had tried it at 2020 version and the output is not i wanted. I will try to figure why. Thank you for your info.
Mehmed Saad
le 19 Avr 2020
Modifié(e) : Mehmed Saad
le 19 Avr 2020
Choose sampling time, sorry i forgot to change that, try the new one

Kenneth Yee
le 19 Avr 2020
Yes, I had insert the excel data before run the simulink. Just the data after going through delay block is not what i wanted then the whole output become incorrect. As I thought, the delay block will just delay the input data one time step but its not.
Kenneth Yee
le 19 Avr 2020
I had tried the latest one. What i want from the delay block is delay one time step compare to the original data, but what i get from the delay block is advance one time step. The data i want, for example, the original data at 694th is 4607.79, the data i want to get after the delay block at 693th is 4607.79.
You understand what i mean?
Catégories
En savoir plus sur String dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!