Error whenever I try to label axis

조회 수: 30 (최근 30일)
Stanley Zhu
Stanley Zhu 2021년 7월 9일
편집: Stanley Zhu 2021년 7월 9일
I keep getting the error Index exceeds the number of array elements(1). The code essentially has been provided and I just need to add to it, so I'm not quite certain what I'm doing wrong.
Edit 1: Even if I try to use MatLab's example on labeling axis, it still gives me the error, code of the example is all the way at the bottom.
Edit 2: Issue was a missing toolbox
clc, close all
figure
% Choose POLAR or CARTESIAN coordinates
use_polar_coordinates = true % options: true (polar) or false (cartesian)
% Choose a colormap.
% try other colormaps such as cool, hot, hsv, prism, flag, jet, parula, lines
colormap flag
% choose preferred shading for surface plots
shading faceted % Shading methods are faceted, flat, and interp.
L = 5 % A default limit so we can easily control the axes.
% Set up a grid for each 3D plot.
x = -L : 1 : L; % x will range from -L to L.
y = -L : 1 : L; % y will range from -L to L.
[X,Y] = meshgrid(x,y);
% polar coordinates will be used instead, if you set use_polar_coordinates = true
if use_polar_coordinates
[rad,theta] = meshgrid(0:1:5, 0:2*pi/36: (2*pi) );
X = rad.*cos(theta);
Y = rad.*sin(theta);
Z = 0*X;
end
base = surf(X,Y,0*X + 0*Y); % Draw the xy plane. It's equation is z = 0.
hold on
grid on
% add some transparency to the base (xy-plane) for reference
base.set('facealpha', 0.4); base.set('edgealpha', 0.15);
base.set('facecolor', 'green');
darkgreen = [0 0.5 0 ];
% Adjust the axes limits for optimal view
L = 5; L = L + 1;
zmin = -L; zmax = +L; % these have to be set by trial and error.
axis([-L, L, -L, L, zmin, zmax]) % set all three axis limits - we are in three space
axis equal
% Plot the coordinate axis in the background as reference objects
t = [-1, 1] % used to compute endpoints along each axis
% plot the x-axis
x = L*t; y = 0*t; z = 0*t;
plot3( x, y, z, 'color', darkgreen, 'LineWidth', 3)
% plot the y-axis
x = 0*t; y = L*t; z = 0*t;
plot3( x, y, z, 'color', darkgreen, 'LineWidth', 3)
% plot the z-axis
x = 0*t; y = 0*t; z = [zmin, zmax]
plot3( x, y, z, 'color', darkgreen, 'LineWidth', 3)
set(gca, 'FontSize', 20)
% Add one reference marker at the tip of the positive x-axis.
plot3( L, 0, 0, 'bd', 'MarkerSize', 9, 'MarkerFaceColor', 'red')
% Add one reference marker at the tip of the positive y-axis.
plot3( 0, L, 0, 'bd', 'MarkerSize', 9, 'MarkerFaceColor', 'green')
% Add one reference marker at the tip of the positive z-axis.
plot3( 0, 0, zmax, 'bd', 'MarkerSize', 9, 'MarkerFaceColor', 'blue')
view([120, 20])
% Students will add more code here.
title('Visualization of a System of Equations');
xlabel('x');
ylabel('y');
zlabel('z');
%% MatLab's example
x = linspace(-2*pi,2*pi,100);
y1 = sin(x);
y2 = cos(x);
figure
plot(x,y1,x,y2)
title('Line Plot of Sine and Cosine Between -2\pi and 2\pi')
xlabel('-2\pi < x < 2\pi')
ylabel('Sine and Cosine Values')

채택된 답변

Chunru
Chunru 2021년 7월 9일
The code can be run as shown below. Try clear all before running the code.
clc, close all
figure
% Choose POLAR or CARTESIAN coordinates
use_polar_coordinates = true % options: true (polar) or false (cartesian)
use_polar_coordinates = logical
1
% Choose a colormap.
% try other colormaps such as cool, hot, hsv, prism, flag, jet, parula, lines
colormap flag
% choose preferred shading for surface plots
shading faceted % Shading methods are faceted, flat, and interp.
L = 5 % A default limit so we can easily control the axes.
L = 5
% Set up a grid for each 3D plot.
x = -L : 1 : L; % x will range from -L to L.
y = -L : 1 : L; % y will range from -L to L.
[X,Y] = meshgrid(x,y);
% polar coordinates will be used instead, if you set use_polar_coordinates = true
if use_polar_coordinates
[rad,theta] = meshgrid(0:1:5, 0:2*pi/36: (2*pi) );
X = rad.*cos(theta);
Y = rad.*sin(theta);
Z = 0*X;
end
base = surf(X,Y,0*X + 0*Y); % Draw the xy plane. It's equation is z = 0.
hold on
grid on
% add some transparency to the base (xy-plane) for reference
base.set('facealpha', 0.4); base.set('edgealpha', 0.15);
base.set('facecolor', 'green');
darkgreen = [0 0.5 0 ];
% Adjust the axes limits for optimal view
L = 5; L = L + 1;
zmin = -L; zmax = +L; % these have to be set by trial and error.
axis([-L, L, -L, L, zmin, zmax]) % set all three axis limits - we are in three space
axis equal
% Plot the coordinate axis in the background as reference objects
t = [-1, 1] % used to compute endpoints along each axis
t = 1×2
-1 1
% plot the x-axis
x = L*t; y = 0*t; z = 0*t;
plot3( x, y, z, 'color', darkgreen, 'LineWidth', 3)
% plot the y-axis
x = 0*t; y = L*t; z = 0*t;
plot3( x, y, z, 'color', darkgreen, 'LineWidth', 3)
% plot the z-axis
x = 0*t; y = 0*t; z = [zmin, zmax]
z = 1×2
-6 6
plot3( x, y, z, 'color', darkgreen, 'LineWidth', 3)
set(gca, 'FontSize', 20)
% Add one reference marker at the tip of the positive x-axis.
plot3( L, 0, 0, 'bd', 'MarkerSize', 9, 'MarkerFaceColor', 'red')
% Add one reference marker at the tip of the positive y-axis.
plot3( 0, L, 0, 'bd', 'MarkerSize', 9, 'MarkerFaceColor', 'green')
% Add one reference marker at the tip of the positive z-axis.
plot3( 0, 0, zmax, 'bd', 'MarkerSize', 9, 'MarkerFaceColor', 'blue')
view([120, 20])
% Students will add more code here.
title('Visualization of a System of Equations');
xlabel('x');
ylabel('y');
zlabel('z');
  댓글 수: 3
Chunru
Chunru 2021년 7월 9일
Show your error message.
Stanley Zhu
Stanley Zhu 2021년 7월 9일
Yeah sorry, it was an issue with me missing a toolbox, just decided to download all the toolboxes the version of matlab provided to me by the univeristy and it worked out, sorry for that.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by