Effacer les filtres
Effacer les filtres

Error whenever I try to label axis

2 vues (au cours des 30 derniers jours)
Stanley Zhu
Stanley Zhu le 9 Juil 2021
Modifié(e) : Stanley Zhu le 9 Juil 2021
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')

Réponse acceptée

Chunru
Chunru le 9 Juil 2021
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 commentaires
Chunru
Chunru le 9 Juil 2021
Show your error message.
Stanley Zhu
Stanley Zhu le 9 Juil 2021
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.

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by