How can I fix this error Index in position 2 exceeds array bounds (must not exceed 1).

21 views (last 30 days)
leny = length(y);
n = floor(max(time)); % n is the number of different color segments in the plot (1-64)
palette = jet(n); % Create the color palette from the jet color map
% Plot the phase plot
figure
subplot(10,1,1:9)
for t = 1:n
hold on
% Plot part of the phase plot
title('Phase Plot with Colored Time Representation')
pplot1 = plot( y(floor(leny/n)*(t-1)+1 : floor(leny/n)*t,1), ...
y(floor(leny/n)*(t-1)+1 : floor(leny/n)*t,2), ...
'LineWidth',3,'Color', palette(t,:) );
xlabel('x_{1}');
ylabel('x_{2}');
end
hold off
grid on
% Plot the time scale
subplot(10,1,10)
hold on
for t = 1:n
x = [ t-1 t t t-1];
y = [ 0 0 1 1 ];
patch(x,y,palette(t,:))
xlabel('time');
ylabel('Color');
end
hold off
end
I got this message :
Index in position 2 exceeds array bounds (must not exceed 1).
Error in pptime (line 29)
y(floor(leny/n)*(t-1)+1 : floor(leny/n)*t,2), ...

Accepted Answer

Star Strider
Star Strider on 26 Jun 2021
Since ‘y’ is a (10001x1) column vector, there is no second column as referred to:
y(floor(leny/n)*(t-1)+1 : floor(leny/n)*t,2), ...
↑ ← HERE
Since I have no idea what you want to do, you will need to solve that.
.
  2 Comments
hala siddiq
hala siddiq on 26 Jun 2021
Thank you so much. I am try to plot the phase space of this signal but this code what i have but still i can not able to plot. is there any easy way to plot the phase space.
Star Strider
Star Strider on 26 Jun 2021
As always, my pleasure!
The only option that comes quickly to mind (since I am not certain what you are doing) is:
dydt = gradient(y) ./ gradient(time(:));
figure
plot(y, dydt)
grid
Plotting the derivative of the vector against the original vector is a relatively straightforward way of creating a phase plot. (Another way is to plot a delayed version of the original vector against the original vector.)
It would be best to force ‘time’ to also be a column vector, either by transposing it, or using the (:) subscript convention, as I do here. Otherwise, with ‘automatic implicit expansion’ (introduced in R2016b), you could end up inadvertently creating some relatively large matrices that could be confusing to deal with.
.

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 26 Jun 2021
Edited: Image Analyst on 26 Jun 2021
y is a 1-d vector. There is no second dimension. So you can't tell it to take the second column of y when there is no second column. When an array is either a row vector or a column vector, it treats the first index like a row index, and the second index as a column index. So you have a vector, or range, floor(leny/n)*(t-1)+1 : floor(leny/n)*t for the first index and it will use that as an index (row) into the 1-D y variable. So that part is fine. However you follow that with 2, so now it's expecting that y is a 2-D array, which it's not, and it's wanting to take some elements from the second column. But you declared y as a 1-D variable not a 2-D variable so there is no second dimension (no second column). Not sure what you were thinking but maybe just get rid of the 2 and have:
index1 =floor(leny/n)*(t-1)+1
index2 = floor(leny/n)*t
y(index1 : index2)
that will just extract that range of indexes from the row vector y. Or
pplot1 = plot(y(floor(leny/n)*(t-1)+1 : floor(leny/n)*t), '-', ...
'LineWidth',3, 'Color', palette(t,:) );
  3 Comments
hala siddiq
hala siddiq on 26 Jun 2021
I have another question
function phsprecon(yy)
% function phsprecon(yy)
% phsprecon(yy) plots the time series, yy, using phase space reconstruction
%
% This code creates a graphical user interphase, through which phase space
% reconstruction can be visualized. The user controls the embedding
% dimension and the time lag through sliders at the bottom of the gui.
% These values are used to create a phase space matrix, which is then
% plotted using my plotMD code, also available on the Matlab file exchange.
% For an explaination of how the data is plotted in multiple dimensions
% please see the plotMD code.
%
% Dec 2015 - Created by Ben Senderling, email bensenderling@gmail.com
%
% Example
% t=((0:4999)*0.01)'
% y=sin(2*pi*1*t)+sin(2*pi*0.5*t);
% phsprecon(y);
%
%% Gather inputs
[row,col]=size(yy);
if col~=1 && row==1;
yy=yy';
elseif row~=1 && col==1
else
error('input must be a 1xn or nx1 matrix')
end
%% Set up GUI
H=figure;
subplot(5,1,1), plot(yy); axis tight, grid on
% Create slider for embedding dimensions
sld1 = uicontrol('Style', 'slider',...
'Min',1,'Max',8,'Value',4,...
'Units','Normalized',...
'Position', [0.05 0.11 0.7 0.05],...
'Callback', @phasespace,...
'SliderStep', [1/7 1/7]);
% Create slider for lag
sld2 = uicontrol('Style', 'slider',...
'Min',1,'Max',200,'Value',4,...
'Units','Normalized',...
'Position', [0.05 0.05 0.7 0.05],...
'Callback', @phasespace,...
'SliderStep', [1/199 1/5]);
G=annotation('textbox',[0.8 0.11 0.2 0.05],'string',['dim = ' num2str(4)],'LineStyle','none');
H=annotation('textbox',[0.8 0.05 0.2 0.05],'string',['lag = ' num2str(4)],'LineStyle','none');
%% Initialize plot
phasespace
%% Perform phase space reconstruction and update plot
function phasespace(source,callbackdata)
dim=round(get(sld1,'Value'));
lag=round(get(sld2,'Value'));
delete(G)
G=annotation('textbox',[0.8 0.11 0.2 0.05],'string',['dim = ' num2str(dim)],'LineStyle','none');
delete(H)
H=annotation('textbox',[0.8 0.05 0.2 0.05],'string',['lag = ' num2str(lag)],'LineStyle','none');
ps=[];
for i=1:dim
ps(:,i)=yy(1+(i-1)*lag:end-(dim-(i-1))*lag);
end
subplot(5,1,2:4),plotMD(ps);
end
end
This is working wwith me for this case how i could I change from plot of 3D ro 2D x,y plot.
and how can i emergy the two code to get nice plot and correct because i got the correct phase space in this code and not in the first one but the problem with the second code that in 3D
Image Analyst
Image Analyst on 26 Jun 2021
I didn't run the code (I don't have plotMD and some of the other functions it calls) but you can change the view point that you're looking at a 3-D plot with with the view() function. Experiment around with that and see if you can get what you want.

Sign in to comment.

Categories

Find more on Data Distribution Plots in Help Center and File Exchange

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!