You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
How to solve "Attempted to access index 0 of an array with smaller dimension sizes.
14 views (last 30 days)
Show older comments
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
Answers (1)
Mehmed Saad
on 18 Apr 2020
Edited: Mehmed Saad
on 18 Apr 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 Comments
Kenneth Yee
on 18 Apr 2020
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
Mehmed Saad
on 18 Apr 2020
Edited: Mehmed Saad
on 18 Apr 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
on 18 Apr 2020
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
Mehmed Saad
on 18 Apr 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
on 18 Apr 2020
Based on the idea u give, the output is not what i wanted. Here is the simulink file.
Mehmed Saad
on 18 Apr 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
Kenneth Yee
on 18 Apr 2020
The variable x and t is not in my workspace after i followed what you mentioned up there.
Mehmed Saad
on 18 Apr 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
on 18 Apr 2020
I had run the script in matlab and it show
"Error in Untitled (line 1)
T=readmatrix('PvPower.xlsx');"
and i dont know why
Mehmed Saad
on 18 Apr 2020
Edited: Mehmed Saad
on 18 Apr 2020
filename = 'complete path of your excel file you want to read along with filename';
T=readmatrix(filename);
Kenneth Yee
on 18 Apr 2020
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
Mehmed Saad
on 18 Apr 2020
Edited: Mehmed Saad
on 18 Apr 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
on 18 Apr 2020
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?
Mehmed Saad
on 18 Apr 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
on 18 Apr 2020
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?
Kenneth Yee
on 19 Apr 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
on 19 Apr 2020
Edited: Mehmed Saad
on 19 Apr 2020
Choose sampling time, sorry i forgot to change that, try the new one
Kenneth Yee
on 19 Apr 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
on 19 Apr 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?
See Also
Categories
Find more on Programmatic Model Editing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)