Array indices must be positive integers or logical values.
Mostra commenti meno recenti
Why im keeping get this massage all the time? what do i do wrong?
t=linspace(0,2*pi,100);
plot(cos(t),sin(2*t));
hold on;
grid on;
p=plot(cos(t(1)),sin(2*t(1)),'ro');
s=plot(cos(t(100)),sin(2*t(100)),'bo');
set(p, 'markerfacecolor','r','markersize',18);
set(s, 'markerfacecolor','b','markersize',18);
for i=1: 100
set(p,'XData', cos(t(i)),'YData',sin(2*t(i)));
set(s,'XData', cos(t(100-i)),'YData',sin(2*t(100-i)));
title(['Current T=',num2str(t(i)/pi),'\pi']);
pause(0.1);
drawnow;
end
Risposta accettata
Più risposte (3)
Alan Stevens
il 30 Mar 2021
1 voto
Look at cos(t(100-i)). When i is 100 this wants t(0). However, indexing starts at 1 in Matlab, not 0.
2 Commenti
Naor t
il 30 Mar 2021
Alan Stevens
il 30 Mar 2021
t=linspace(0,2*pi,100);
plot(cos(t),sin(2*t));
hold on;
grid on;
set(gca,'color','k') %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
p=plot(cos(t(1)),sin(2*t(1)),'ro');
s=plot(cos(t(100)),sin(2*t(100)),'bo');
set(p, 'markerfacecolor','r','markersize',18);
set(s, 'markerfacecolor','b','markersize',18);
for i=1: 99 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
set(p,'XData', cos(t(i)),'YData',sin(2*t(i)));
set(s,'XData', cos(t(100-i)),'YData',sin(2*t(100-i)));
title(['Current T=',num2str(t(i)/pi),'\pi']);
pause(0.1);
drawnow;
end
Image Analyst
il 30 Mar 2021
Because 100-i is 0 at the last iteration and you can't have the zero'th element of an array. Have your loop go from 1 to 99.
t=linspace(0,2*pi,100);
plot(cos(t),sin(2*t));
hold on;
grid on;
p=plot(cos(t(1)),sin(2*t(1)),'ro');
s=plot(cos(t(100)),sin(2*t(100)),'bo');
set(p, 'markerfacecolor','r','markersize',18);
set(s, 'markerfacecolor','b','markersize',18);
for i=1: 99
set(p,'XData', cos(t(i)),'YData',sin(2*t(i)));
set(s,'XData', cos(t(100-i)),'YData',sin(2*t(100-i)));
title(['Current T=',num2str(t(i)/pi),'\pi']);
pause(0.1);
drawnow;
end
There is a thorough explanation in the FAQ:
1 Commento
Image Analyst
il 30 Mar 2021
Modificato: Image Analyst
il 30 Mar 2021
@Naor t, regarding your request to set the background and other colors, see my attached demo. You can change virtually anything on the graph that you want if you choose the correct property.

% Demo to make a black graph with red Y axis, green X axis, and yellow grid. Markers are magenta with green lines between them.
% Initialization steps:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clearvars;
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 24;
% Create sample data.
X = 1 : 20;
Y = rand(1, 20);
% Plot green lines between the markers.
plot(X, Y, 'g-', 'LineWidth', 2);
hold on;
% Plot magenta markers.
plot(X, Y, 'ms', 'LineWidth', 2, 'MarkerSize', 15);
grid on;
title('Y vs. X, Font Size 20', 'FontSize', 20, 'Color', 'b', 'FontWeight', 'bold');
% Make labels for the two axes.
xlabel('X Axis, Font Size 18');
ylabel('Y axis, Font Size 24');
yticks(0 : 0.2 : 1);
% Get handle to current axes.
ax = gca
% Now let's have fun changing all kinds of things!
% This sets background color to black.
ax.Color = 'k'
ax.YColor = 'r';
% Make the x axis dark green.
darkGreen = [0, 0.6, 0];
ax.XColor = darkGreen;
% Make the grid color yellow.
ax.GridColor = 'y';
ax.GridAlpha = 0.9; % Set's transparency of the grid.
% Set x and y font sizes.
ax.XAxis.FontSize = 18;
ax.YAxis.FontSize = 24;
% Make the axes tick marks and bounding box be really thick.
ax.LineWidth = 3;
% Let's have the tick marks go outside the graph instead of poking inwards
ax.TickDir = 'out';
% The below would set everything: title, x axis, y axis, and tick mark label font sizes.
% ax.FontSize = 34;
% Bold all labels.
ax.FontWeight = 'bold';
hold off
% Now do stuff with the figure, as opposed to the axes control that is ON the figure.
% Maximize the figure
g = gcf; % Get handle to the current figure.
g.WindowState = 'maximized'; % Make it full screen.
g.Name = 'Demo by Image Analyst'; % Put a custom string into the titlebar.
g.NumberTitle = 'off'; % Don't have it put "Figure 1" before the name.
g.MenuBar = 'figure'; % or 'none'
g.ToolBar = 'figure'; % or 'none'
Steven Lord
il 30 Mar 2021
for i=1: 100
set(p,'XData', cos(t(i)),'YData',sin(2*t(i)));
set(s,'XData', cos(t(100-i)),'YData',sin(2*t(100-i)));
When i is equal to 100, the second line attempts to compute the cosine and sine of t(0). Arrays in MATLAB don't have an element 0. Since I'm guessing you want to use t(100) when i is 1, you should work with t(101-i) instead of t(100-i).
Categorie
Scopri di più su Graphics Object Properties in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!