How to 3D Surface Plot in Matlab R2024a 2024| Customizable Code and Plot| Skill Development #matlab

preview_player
Показать описание
Code for Intereactive 3D Surface Plot
function interactive_3D_plot
% Create the figure
fig = figure('Name', 'Interactive 3D Plot', 'NumberTitle', 'off', ...
'Position', [100, 100, 800, 600]);

% Create axes for the 3D plot
ax = axes('Parent', fig, 'Position', [0.3, 0.3, 0.65, 0.65]);

% Plot the initial 3D surface
[X, Y] = meshgrid(-5:0.1:5, -5:0.1:5);
Z = sin(sqrt(X.^2 + Y.^2));
surf_plot = surf(X, Y, Z);
xlabel('X-axis');
ylabel('Y-axis');
zlabel('Z-axis');
title('Interactive 3D Surface Plot');

% Create sliders for controlling rotation
uicontrol('Style', 'text', 'Position', [50, 500, 100, 20], ...
'String', 'Rotation (Azimuth)');
azimuth_slider = uicontrol('Style', 'slider', 'Min', 0, 'Max', 360, ...
'Value', 45, 'Position', [50, 480, 100, 20], ...
'Callback', @rotate_plot);

uicontrol('Style', 'text', 'Position', [50, 450, 100, 20], ...
'String', 'Rotation (Elevation)');
elevation_slider = uicontrol('Style', 'slider', 'Min', -90, 'Max', 90, ...
'Value', 30, 'Position', [50, 430, 100, 20], ...
'Callback', @rotate_plot);

% Create buttons for zooming in and out
uicontrol('Style', 'pushbutton', 'String', 'Zoom In', ...
'Position', [50, 390, 100, 30], 'Callback', @zoom_in);
uicontrol('Style', 'pushbutton', 'String', 'Zoom Out', ...
'Position', [50, 350, 100, 30], 'Callback', @zoom_out);

% Callback functions
function rotate_plot(~, ~)
az = get(azimuth_slider, 'Value');
el = get(elevation_slider, 'Value');
view(ax, [az, el]);
end

function zoom_in(~, ~)
camzoom(ax, 1.2);
end

function zoom_out(~, ~)
camzoom(ax, 0.8);
end
end
.............................................
This MATLAB code creates an interactive 3D plot with user controls for rotation and zooming.

This MATLAB code is designed to create an interactive 3D plot where users can control the plot’s view and zoom level using sliders and buttons.

Creating the Figure Window: The code starts by opening a new figure window using the figure function. This window is titled "Interactive 3D Plot," with the numbering feature turned off. The window’s position and size are specified by [100, 100, 800, 600], meaning it is positioned 100 pixels from the left and top edges of the screen and is 800x600 pixels in size.

Defining the Axes: The axes function creates a set of 3D axes inside the figure where the plot will appear. The position [0.3, 0.3, 0.65, 0.65] defines the axes relative to the figure's size, placing the plot towards the center of the window and scaling it to take up about 65% of the figure.

Generating a 3D Surface Plot: Using meshgrid, the code generates a 2D grid of X and Y coordinates ranging from -5 to 5 with a step size of 0.1. The height values (Z) are calculated using the sine of the distance from the origin (sqrt(X.^2 + Y.^2)), creating a wave-like radial pattern. The surf function is then used to plot this 3D surface based on the X, Y, and Z values. Labels for the axes (X, Y, Z) and a title for the plot are added to enhance the visualization.

Adding Sliders for Interactive Rotation: Two sliders are created using uicontrol—one for azimuth (horizontal rotation) and one for elevation (vertical rotation). The azimuth slider allows rotation between 0 and 360 degrees, with an initial value set at 45 degrees, while the elevation slider allows rotation between -90 and 90 degrees, starting at 30 degrees. The position [50, 480, 100, 20] defines where the sliders are placed in the figure. When the user adjusts a slider, the callback function rotate_plot is triggered.

Rotation Callback Function: The rotate_plot function updates the viewing angle of the 3D plot. It retrieves the current values of both sliders using the get function. The view function is then used to apply the new azimuth and elevation angles to the axes, updating the plot’s perspective in real-time.

Zoom In/Out Buttons: Two buttons labeled "Zoom In" and "Zoom Out" are created using uicontrol. When clicked, these buttons trigger the zoom_in and zoom_out callback functions, respectively. The camzoom function is used to adjust the zoom level of the 3D plot, where a factor of 1.2 zooms in by 20%, and 0.8 zooms out by 20%, giving the user control over the plot’s zoom level.

Together, these elements allow users to interact with the 3D plot dynamically, rotating and zooming it to explore different perspectives of the wave-like surface. This interactive setup makes the visualization more engaging and useful for detailed analysis.
Рекомендации по теме
visit shbcf.ru