Use the MATLAB Help Browser to learn about the Current Point property of an axes object. Use this property to create a program that creates an axes object and plots a line connecting the locations of successive mouse elicks within the axes. Use the function wait forbuttonpress to wait for mouse clicks, and update the plot after each click. Terminate the program when a keyboard press occurs.

Short Answer

Expert verified
To create a MATLAB program that plots a line connecting successive mouse clicks within an axes object, follow these steps: 1. Create a figure and axes using `fig = figure;` and `ax = axes;`. 2. Initialize variables for previous and current points, and an empty line plot using `prev_point = [NaN, NaN];`, `current_point = [NaN, NaN];`, and `line_plot = plot(ax, NaN, NaN);`. 3. Create a loop waiting for mouse clicks or keyboard presses using `while true`. Inside the loop, use `w = waitforbuttonpress;` to wait for a button press. 4. If it's a mouse click (`w == 0`), update the current point with `current_point = get(ax, 'CurrentPoint');` and `current_point = current_point(1, 1:2);`. 5. Update the line plot's x and y data with the new points using `set(line_plot, 'XData', xdata, 'YData', ydata);`. 6. If it's a keyboard press, break the loop using `else` and `break;`.

Step by step solution

01

Create an axes object

First, we need to create a figure and axes object in MATLAB. To do this, we will use the `figure` and `axes` commands: ```matlab fig = figure; ax = axes; ```
02

Initialize variables and the plot

Next, we need to initialize variables to store the previous and current mouse click locations. We will also create an empty line object to store our plot. ```matlab prev_point = [NaN, NaN]; current_point = [NaN, NaN]; line_plot = plot(ax, NaN, NaN); ```
03

Create a loop to wait for mouse clicks

We will create a loop that will continuously wait for a button press (either mouse or keyboard). If it was a mouse click, the loop will get the current point in the axes and update the plot. If it was a keyboard press, the loop will break, and the program will terminate. ```matlab while true w = waitforbuttonpress; if w == 0 % It was a mouse click current_point = get(ax, 'CurrentPoint'); current_point = current_point(1, 1:2); % Get only the first row and the first two columns % Update the line plot xdata = get(line_plot, 'XData'); ydata = get(line_plot, 'YData'); xdata = [xdata, prev_point(1), current_point(1)]; ydata = [ydata, prev_point(2), current_point(2)]; set(line_plot, 'XData', xdata, 'YData', ydata); prev_point = current_point; else % It was a keyboard press break; end end ``` The complete code should look like this: ```matlab % Create a figure and axes fig = figure; ax = axes; % Initialize variables and plot prev_point = [NaN, NaN]; current_point = [NaN, NaN]; line_plot = plot(ax, NaN, NaN); % Loop to wait for mouse clicks and update the plot while true w = waitforbuttonpress; if w == 0 % It was a mouse click current_point = get(ax, 'CurrentPoint'); current_point = current_point(1, 1:2); % Get only the first row and the first two columns % Update the line plot xdata = get(line_plot, 'XData'); ydata = get(line_plot, 'YData'); xdata = [xdata, prev_point(1), current_point(1)]; ydata = [ydata, prev_point(2), current_point(2)]; set(line_plot, 'XData', xdata, 'YData', ydata); prev_point = current_point; else % It was a keyboard press break; end end ``` This program will create an axes object in MATLAB, and as the user clicks within the axes, the program will plot a line connecting successive mouse click locations. The program will terminate when the user presses a keyboard key.

Unlock Step-by-Step Solutions & Ace Your Exams!

  • Full Textbook Solutions

    Get detailed explanations and key concepts

  • Unlimited Al creation

    Al flashcards, explanations, exams and more...

  • Ads-free access

    To over 500 millions flashcards

  • Money-back guarantee

    We refund you if you fail your exam.

Over 30 million students worldwide already upgrade their learning with Vaia!

One App. One Place for Learning.

All the tools & learning materials you need for study success - in one app.

Get started for free

Most popular questions from this chapter

See all solutions

Recommended explanations on Psychology Textbooks

View all explanations

What do you think about this solution?

We value your feedback to improve our textbook solutions.

Study anywhere. Anytime. Across all devices.

Sign-up for free