MATLAB 2D and 3D Plotting 4 — Questions and Answers
Question 1: Which MATLAB function creates a stem plot (discrete sequence plot)?
- bar
- stairs
- stem (Correct answer)
- discrete
Correct answer: stem
stem(x,y) plots data as vertical lines (stems) topped with markers, commonly used to visualize discrete-time signals.
Question 2: How do you rotate a 3D plot interactively in MATLAB's figure window?
- Use the 'rotate3d on' command or the Rotate 3D toolbar button (Correct answer)
- Press R while hovering over the plot
- Call rotate(gca, [0 1 0], 45)
- Use the 'spin' command
Correct answer: Use the 'rotate3d on' command or the Rotate 3D toolbar button
'rotate3d on' enables interactive 3D rotation so you can click and drag the plot to view it from different angles.
Question 3: What does 'caxis([0 100])' do in MATLAB?
- Sets the c-axis (depth) range for 3D plots
- Sets the color data limits used by the colormap to map values 0–100 (Correct answer)
- Creates a secondary axis scaled from 0 to 100
- Changes the axis font size to between 0 and 100 points
Correct answer: Sets the color data limits used by the colormap to map values 0–100
caxis([cmin cmax]) sets the color axis scaling so that the colormap spans the data range from cmin to cmax.
Question 4: Which MATLAB function is used to add a secondary y-axis on the right side of a plot?
- twinx
- yyaxis right (Correct answer)
- addaxis
- yaxis(2)
Correct answer: yyaxis right
yyaxis right activates the right y-axis so subsequent plots are drawn against it while sharing the same x-axis.
Question 5: In MATLAB, what is the difference between 'plot(x,y)' and 'fplot(@(x) sin(x), [0 pi])'?
- There is no difference; both produce identical results
- fplot adaptively samples the function for accuracy while plot uses fixed x-spacing (Correct answer)
- fplot only works for linear functions
- plot can handle complex numbers but fplot cannot
Correct answer: fplot adaptively samples the function for accuracy while plot uses fixed x-spacing
fplot adaptively samples the function at more points in rapidly changing regions to produce a smoother and more accurate curve, unlike plot which uses fixed spacing.
Question 6: Which MATLAB command saves the current figure as a high-resolution PNG file?
- saveas(gcf,'fig.png')
- print('-dpng','-r300','fig.png') (Correct answer)
- export(gcf,'fig','png')
- save('fig.png','png')
Correct answer: print('-dpng','-r300','fig.png')
print('-dpng','-r300','fig.png') exports the current figure as a PNG with 300 DPI resolution, suitable for publication.
Question 7: What does the MATLAB function 'polarplot(theta, rho)' create?
- A 3D cylindrical surface plot
- A 2D plot in polar coordinates with angle theta and radius rho (Correct answer)
- A polar grid without data
- A plot where theta is converted to Cartesian coordinates automatically
Correct answer: A 2D plot in polar coordinates with angle theta and radius rho
polarplot(theta, rho) draws a line in polar coordinates, where theta is the angle in radians and rho is the radius.
Which MATLAB function creates a stem plot (discrete sequence plot)?