How to solve "Attempted to access index 0 of an array with smaller dimension sizes.

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)

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

After I changed the i=1:1440 to i=2:1440, error occured as "Attempted to access index 2 of an array with smaller dimension sizes." I had tried this before, no matter what value i change in 'i', it shows in the error again
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
An error shows like this "Simulink does not have enough information to determine output sizes for this block. If you think the errors below are inaccurate, try specifying types for the block inputs and/or sizes for the block outputs." after i tried for i = 2:length(b)
Yes, my data has two coloumn , 1st column is no. from 1 to 1440, 2nd coloumn is the value i want to compare
can you post the project
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
Based on the idea u give, the output is not what i wanted. Here is the simulink file.
please attach the variabl input
The 1st sheet named 'Curren', I inserted it through workspace
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
Sorry, i can't open your file. Can u export it as 2016 version?
The variable x and t is not in my workspace after i followed what you mentioned up there.
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
I had run the script in matlab and it show
"Error in Untitled (line 1)
T=readmatrix('PvPower.xlsx');"
and i dont know why
filename = 'complete path of your excel file you want to read along with filename';
T=readmatrix(filename);
Yes i understand, where you save your excel file in? I save in D disk and the script save in disk D also cannot , still got error
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);
after runing this code, run this simulink model
Hi, i run the code as
[file_n,folder_n] = uigetfile('PvPower.xlsx');
'PvPower.xlsx' = strcat(folder_n,file_n);
T=readmatrix('PvPower.xlsx');
t = T(:,1);
x = T(:,2);
There is an error,
Error: File: data.m Line: 2 Column: 16
The expression to the left of the equals sign is not a valid target for an assignment.
is my script wrong? The file and folder indicate?
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);
Yes, i did it.
After i run the simulink, the error occur as
The sample time propagated to 'PvPower2016b/Delay' is not discrete but [0, 1]. Specify a discrete sample time in the source of the incoming signal.
May i know how you setting the delay block?
Mehmed Saad
Mehmed Saad le 18 Avr 2020
Modifié(e) : Mehmed Saad le 18 Avr 2020
it is working perfectly in R2017a
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
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
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.
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?

Connectez-vous pour commenter.

Commenté :

le 19 Avr 2020

Community Treasure Hunt

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

Start Hunting!

Translated by