Array indices must be positive integers or logical values.
8 ビュー (過去 30 日間)
古いコメントを表示
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
0 件のコメント
採用された回答
Star Strider
2021 年 3 月 30 日
The code runs for me without error and creates a neat Lissajou figure with red and blue circles traversing the path!
The problem is this statement:
set(s,'XData', cos(t(100-i)),'YData',sin(2*t(100-i)));
and since ‘i’ goes from 1 to 100, the index into ‘t’ will be negative or 0 at the end of the loop, and negative or 0 subscripts are not permitted in MATLAB.
One possible solution that also automatically scales for different lengths of ‘t’:
set(s,'XData', cos(t(end-i+1)),'YData',sin(2*t(end-i+1)));
Otherwise, since I am not certain what you want to do, I cannot suggest any other correction for it.
0 件のコメント
その他の回答 (3 件)
Alan Stevens
2021 年 3 月 30 日
Look at cos(t(100-i)). When i is 100 this wants t(0). However, indexing starts at 1 in Matlab, not 0.
2 件のコメント
Alan Stevens
2021 年 3 月 30 日
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
2021 年 3 月 30 日
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 件のコメント
Image Analyst
2021 年 3 月 30 日
編集済み: Image Analyst
2021 年 3 月 30 日
@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
2021 年 3 月 30 日
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).
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Specifying Target for Graphics Output についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!