0% found this document useful (0 votes)
31 views32 pages

MATLAB Copilot Users Guide R2025a - MathWorks

The MATLAB® Copilot User’s Guide provides comprehensive instructions on using MATLAB Copilot, a generative AI tool designed to assist users in creating, refining, and debugging MATLAB code. It covers setup, features such as Copilot Chat, code generation, and effective prompt writing, as well as guidance on validating responses and providing feedback. The document also includes contact information for MathWorks and legal disclaimers regarding software use.

Uploaded by

zzzz1898
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views32 pages

MATLAB Copilot Users Guide R2025a - MathWorks

The MATLAB® Copilot User’s Guide provides comprehensive instructions on using MATLAB Copilot, a generative AI tool designed to assist users in creating, refining, and debugging MATLAB code. It covers setup, features such as Copilot Chat, code generation, and effective prompt writing, as well as guidance on validating responses and providing feedback. The document also includes contact information for MathWorks and legal disclaimers regarding software use.

Uploaded by

zzzz1898
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

MATLAB® Copilot

MATLAB® Copilot User’s Guide

R2025a
How to Contact MathWorks

Latest news: www.mathworks.com

Sales and services: www.mathworks.com/sales_and_services

User community: www.mathworks.com/matlabcentral

Technical support: www.mathworks.com/support/contact_us

Phone: 508-647-7000

The MathWorks, Inc.


1 Apple Hill Drive
Natick, MA 01760-2098
MATLAB® Copilot User’s Guide
© COPYRIGHT 2025 by The MathWorks, Inc.
The software described in this document is furnished under a license agreement. The software may be used or copied
only under the terms of the license agreement. No part of this manual may be photocopied or reproduced in any form
without prior written consent from The MathWorks, Inc.
FEDERAL ACQUISITION: This provision applies to all acquisitions of the Program and Documentation by, for, or through
the federal government of the United States. By accepting delivery of the Program or Documentation, the government
hereby agrees that this software or documentation qualifies as commercial computer software or commercial computer
software documentation as such terms are used or defined in FAR 12.212, DFARS Part 227.72, and DFARS 252.227-7014.
Accordingly, the terms and conditions of this Agreement and only those rights specified in this Agreement, shall pertain
to and govern the use, modification, reproduction, release, performance, display, and disclosure of the Program and
Documentation by the federal government (or other entity acquiring for or through the federal government) and shall
supersede any conflicting contractual terms or conditions. If this License fails to meet the government's needs or is
inconsistent in any respect with federal procurement law, the government agrees to return the Program and
Documentation, unused, to The MathWorks, Inc.
Trademarks
MATLAB and Simulink are registered trademarks of The MathWorks, Inc. See
www.mathworks.com/trademarks for a list of additional trademarks. Other product or brand names may be
trademarks or registered trademarks of their respective holders.
Patents
MathWorks products are protected by one or more U.S. patents. Please see www.mathworks.com/patents for
more information.
Revision History
March 2025 First printing New for Version 25.1 (R2025a)
Contents

MATLAB Copilot
1
MATLAB Copilot Product Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-2

Set Up MATLAB Copilot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-3


Sign In as Licensed User . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-3
Start and Chat with Copilot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-3
Use Copilot Actions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-6
Generate Code from Your Prompts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-6
Generate Code as You Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-7
Check Copilot Responses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-8
Provide Feedback . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-8

Explain Code Using MATLAB Copilot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-9


Explain Code in Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-9
Explain Code in Command Window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-9
Explain Error . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-10
Chat with Copilot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-11

Generate and Modify Code Using MATLAB Copilot . . . . . . . . . . . . . . . . . 1-12


Chat with Copilot to Generate Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-12
Insert or Run Code from Chat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-15
Generate Code Inline in Editor or Command Window . . . . . . . . . . . . . . . 1-15
Modify Generated Code with Copilot . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-17
Generate Code with Autocompletions . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-17
Generate Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-19

Write Effective Prompts for MATLAB Copilot . . . . . . . . . . . . . . . . . . . . . . 1-22


Be Specific . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-22
Describe Inputs and Outputs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-22
Break Down Complex Tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-22
Give Feedback and Describe Mistakes . . . . . . . . . . . . . . . . . . . . . . . . . . 1-23
Iterate, Refine, and Validate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-23
Describe Presentation of Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-23
Clear Chat History . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-24
Try Example Prompts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-24

Change MATLAB Copilot Settings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-25


Disable MATLAB Copilot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-25
Disable or Pause Autocompletions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-26

iii
1

MATLAB Copilot

• “MATLAB Copilot Product Description” on page 1-2


• “Set Up MATLAB Copilot” on page 1-3
• “Explain Code Using MATLAB Copilot” on page 1-9
• “Generate and Modify Code Using MATLAB Copilot” on page 1-12
• “Write Effective Prompts for MATLAB Copilot” on page 1-22
• “Change MATLAB Copilot Settings” on page 1-25
1 MATLAB Copilot

MATLAB Copilot Product Description


Create, refine, and debug MATLAB code with generative AI

MATLAB Copilot provides generative AI–powered capabilities specifically for the MATLAB desktop
environment. It assists with MATLAB workflows and provides responses based on MATLAB specific
information. You can use MATLAB Copilot to learn techniques, develop ideas, and improve
productivity.

With MATLAB Copilot, you can ask questions in the Copilot Chat and get responses based on relevant
MathWorks® documentation and code examples. As you type in the MATLAB Editor, autocompletions
provide code predictions. Alternatively, describe the code you want, and MATLAB Copilot will create
or modify code for you.

MATLAB Copilot helps with programming tasks by explaining code and error messages and adding
comments. You can also automatically generate test cases to verify expected code behavior with
MATLAB Test™.

1-2
Set Up MATLAB Copilot

Set Up MATLAB Copilot


MATLAB Copilot provides generative AI–powered capabilities specifically for the MATLAB desktop
environment. With MATLAB Copilot, you can ask questions, generate code, and get explanations for
selected code and error messages. Responses in the Copilot Chat panel are grounded by relevant
MathWorks documentation and code examples.

There are several ways to use MATLAB Copilot capabilities:

• To chat with MATLAB Copilot, use the Copilot Chat panel. Copilot Chat is an interactive assistant
that can help you explore ideas and express them in code. Its responses can include both
conversational text and generated code.
• To perform common tasks such as explaining code and errors or generating comments, use
Copilot actions. You can access Copilot actions from menus in the Editor, Live Editor, and
Command Window.
• To generate or modify code in the Editor, Live Editor, or Command Window, open an Ask
Copilot text box. Describe the code you want. Then accept or discard the generated code
directly in your code file or in the Command Window.
• To get code suggestions from MATLAB Copilot as you type in the Editor or Live Editor, use Copilot
autocompletions. Instead of responding to prompts, MATLAB Copilot generates code based on the
code and comments in your file.

Sign In as Licensed User


To use MATLAB Copilot, you must have a MATLAB Copilot license. Unlike other MathWorks products,
there is no toolbox or app to download and install for MATLAB Copilot. Instead, you gain access to
MATLAB Copilot as a licensed user through your MathWorks Account.

First, sign in to your MathWorks Account using the Sign In button at the upper right corner of
MATLAB.

• If you already have a license for MATLAB Copilot, then several MATLAB Copilot features appear at
startup. The symbol indicates ways that you can interact with MATLAB Copilot.
• If you do not have a license for MATLAB Copilot, then contact your license administrator. Or, if you
do not have a license for MathWorks products through an organization, you can buy a license or
request a trial from the MathWorks Store.

Start and Chat with Copilot


When you start MATLAB, several MATLAB Copilot features appear:

• Copilot Chat panel on the right.


• Copilot Chat button on the MATLAB Toolstrip. To open the Copilot Chat panel, click the button.
• Copilot Chat icon on the right sidebar. To open and close the Copilot Chat panel, click the icon.
• Ctrl+Shift+P help text in the Command Window.
• Copilot status indicator on the lower left corner. It indicates if Copilot is enabled or busy with a
request. It also provides access to Copilot Settings.

1-3
1 MATLAB Copilot

The Copilot Chat panel has an Ask Copilot text box where you can enter prompts—statements or
questions that you write using natural language. For example, enter this prompt:

Create an array of 10 random numbers

Copilot returns a response that can include generated code, references to documentation, and follow-
up prompts. Copilot responses can contain errors and inaccuracies. Validate any generated code and
text before using it.

1-4
Set Up MATLAB Copilot

1-5
1 MATLAB Copilot

Use Copilot Actions


Copilot provides a set of actions in the Editor, Live Editor, and Command Window. You can use these
actions to explain highlighted code or generate new code or comments. For example, highlight some
code in a code file. To get an explanation of the code, select Copilot > Explain Code on the Editor
or Live Editor tab of the MATLAB Toolstrip. Or, right-click and select Copilot > Explain Code from
the context menu.

Generate Code from Your Prompts


You can generate code inline using Copilot in the Command Window, Editor or Live Editor. If you
press Ctrl+Shift+P, or right-click and select Copilot > Ask Copilot from the context menu, then an
Ask Copilot text box appears at your cursor location. Copilot generates code in response to
prompts that you type in the text box.

For example, write a script that calls the sin function. Then open an Ask Copilot text box. Ask
Copilot to plot a sine wave. In response, Copilot generates code to plot the sine wave.

1-6
Set Up MATLAB Copilot

Generate Code as You Type


Copilot autocompletion provides code predictions as you work in the Editor or Live Editor. With
autocompletion you do not need to provide prompts. Instead, Copilot generates suggestions based on
the code and comments that you have just written.

For example, write a script that calls the sin function and then the plot function. Then press Enter.
Copilot autocompletion generates code for a next possible step. To accept all or part of the suggested
code, press Tab or the Right arrow key . To discard the suggestion, press Esc.

1-7
1 MATLAB Copilot

Check Copilot Responses


Because generative AI responses can contain errors and inaccuracies, validate any generated code
and text before using it.

You can use these strategies to validate Copilot responses:

• Review generated code before using it.


• Run generated code and review its output. Confirm that the output is what you expected.
• Check that the generated response aligns with the documentation.
• In the Copilot Chat panel, ask Copilot to explain the code it just generated.
• Highlight code, right-click and select Copilot > Explain Code. If you highlight several lines,
Copilot can return a line-by-line explanation in the Copilot Chat panel.

Provide Feedback
MathWorks uses your feedback to improve MathWorks products and services.

There are three buttons at the end of every Copilot response:

• — Send positive feedback that the answer is helpful.


• — Provide negative feedback that the answer is unhelpful. This button opens a dialog box
where you can provide more details. You can choose to send your chat history as part of your
feedback.
• — Copy the entire response to the clipboard.

See Also

Related Examples
• “Write Effective Prompts for MATLAB Copilot” on page 1-22
• “Explain Code Using MATLAB Copilot” on page 1-9
• “Generate and Modify Code Using MATLAB Copilot” on page 1-12
• “Change MATLAB Copilot Settings” on page 1-25

1-8
Explain Code Using MATLAB Copilot

Explain Code Using MATLAB Copilot


MATLAB Copilot provides generative AI–powered capabilities specifically for the MATLAB desktop
environment. You can use MATLAB Copilot to help you better understand your code. For example, you
can ask Copilot to explain code, explain error messages, or chat with Copilot to get explanations.
Copilot responses are augmented with domain-specific material from MathWorks documentation.

Explain Code in Editor


You can ask Copilot to explain a selection of code in the Editor or Live Editor. Highlight some code in
your file and select Copilot > Explain Code on the Editor or Live Editor tab of the MATLAB
Toolstrip. Or, right-click on the highlighted code and select Copilot > Explain Code from the context
menu. In the Copilot Chat panel, Copilot explains the highlighted code.

Explain Code in Command Window


You can also ask Copilot to explain a selection of code in the Command Window. Highlight lines of
code in the Command Window. You can highlight lines of code that you have already executed. Then
right-click on the highlighted code and select Copilot > Explain Code from the context menu. In the
Copilot Chat panel, Copilot explains the highlighted code.

1-9
1 MATLAB Copilot

Explain Error
When writing code in MATLAB, you might encounter errors in the Editor, Live Editor, or Command
Window. You can ask Copilot to explain these errors.

To get an explanation of an error, click the Explain Error button that appears when you encounter an
error. Copilot explains the error in the Copilot Chat panel. Typically, the response provides an
explanation and troubleshooting recommendation. The exact response depends on the kind of error
that you encounter.

When you work with code in the Editor or Command Window, the Explain Error button appears in
the Command Window. When you work in the Live Editor, Explain Error appears in the Live Editor.

1-10
Explain Code Using MATLAB Copilot

Chat with Copilot


Alternatively, you can chat directly with Copilot to ask questions to better understand code or error
messages. For example, you can copy MATLAB code from another source, such as a webpage that
displays a block of code. Then you can paste that code directly into the Ask Copilot text box in the
Copilot Chat panel and ask Copilot to explain it.

After Copilot generates an explanation, you can ask follow-up prompts, such as:

• Help me understand the code in the previous prompt


• Add comments to the code to make it more readable

See Also

Related Examples
• “Set Up MATLAB Copilot” on page 1-3
• “Write Effective Prompts for MATLAB Copilot” on page 1-22
• “Generate and Modify Code Using MATLAB Copilot” on page 1-12
• “Change MATLAB Copilot Settings” on page 1-25

1-11
1 MATLAB Copilot

Generate and Modify Code Using MATLAB Copilot


MATLAB Copilot provides generative AI–powered capabilities specifically for the MATLAB desktop
environment. You can ask questions, generate code, and use follow-up prompts and links to
documentation. In the Copilot Chat panel, you can effectively have a conversation with Copilot that
helps you solve a problem using MATLAB. Copilot Chat responses are grounded by relevant
MathWorks documentation and code examples.

You can ask MATLAB Copilot to generate and modify code in several ways:

• Copilot Chat — Use the Copilot Chat panel to explore different solutions to a problem or question.
Copilot provides responses that include explanations, generated code, references, and follow-up
prompts. You can insert code from the chat into your code in the Editor or Live Editor, or you can
run it in the Command Window.
• Ask Copilot — Use the Ask Copilot text box to generate code inline as you work in the Editor,
Live Editor, or Command Window. Copilot provides responses as formatted code and comments
directly in your code area, which you can accept or discard. If you want to have a conversation
with Copilot instead, use the Copilot Chat panel.
• Autocompletions — Use autocompletions to generate code predictions based on your code and
comments as you work in the Editor or Live Editor.

In general, use Copilot Chat for exploration. Its responses can include both conversational text and
generated code. Use the Ask Copilot text box and autocompletions to generate code and comments
that you can accept or discard.

Chat with Copilot to Generate Code


When you start MATLAB, the Copilot Chat panel appears on the right.

1-12
Generate and Modify Code Using MATLAB Copilot

You can enter prompts—statements or questions that you write using natural language—in the Ask
Copilot box.

For example, enter this prompt:

Create an array of 10 random numbers

Copilot returns a response that can include generated code, references to documentation, and follow-
up prompts.

1-13
1 MATLAB Copilot

1-14
Generate and Modify Code Using MATLAB Copilot

Insert or Run Code from Chat


When Copilot generates code, you can insert it into your code file or run it in the Command Window.
You can also copy it to the clipboard to manually paste the generated code where you want.

Copilot returns generated code in code boxes. Every code box includes these three buttons:

• — Copy code to the clipboard.


• — Insert code in the Editor or Live Editor. Copilot inserts the generated code at your cursor
location in the open file. If no file is open, then this action opens an untitled file in the Editor and
inserts the code.
• — Run code in the Command Window.

Generate Code Inline in Editor or Command Window


You do not need to use the Copilot Chat panel to generate code. You can generate code inline as you
work in the Editor, Live Editor, or Command Window. When you first start MATLAB or open a new
code file, you see this keyboard shortcut hint:

Press Ctrl + Shift + P to generate code with Copilot

You can perform this action at any time. When you press Ctrl+Shift+P, or right-click and select
Copilot > Ask Copilot from the context menu, an Ask Copilot text box opens at your current
cursor location in the Editor, Live Editor, or Command Window. Alternatively, in the Editor or Live
Editor, select Copilot > Ask Copilot on the Editor or Live Editor tab of the MATLAB Toolstrip.

1-15
1 MATLAB Copilot

For example, in a code file, create a vector x of values from 1 to 10 and a vector y of sine values.

x = 1:10;
y = sin(x);

Then ask Copilot to plot a sine wave by pressing Ctrl+Shift+P and typing plot a sine wave in
the text box that appears. Copilot generates the requested code under the text box. The generated
code is highlighted, and you can choose to:

• Accept the generated code.


• Discard the generated code.
• Revise the prompt you typed to improve the generated code.
• Provide feedback using the and buttons.

1-16
Generate and Modify Code Using MATLAB Copilot

Modify Generated Code with Copilot


Copilot might generate code that does not precisely match your expectations. In that case, you can
ask Copilot to modify the code it just generated. You can highlight the generated code and provide
Copilot with another prompt that provides more details.

For example, ask Copilot to modify the plotting code it generated in the previous example, by adding
square markers and a legend. If you already accepted the generated code, highlight it, and press Ctrl
+Shift+P or select Copilot > Ask Copilot. In the text box, write a new prompt with more details
(for example, plot a sine wave with squares, legend) and press Enter. You can accept the
modified code, discard it, or view the differences between it and the previously accepted code.

Generate Code with Autocompletions


In the Editor and Live Editor, you can also use Copilot autocompletion to generate code predictions.
While MATLAB tab completion can complete function names or arguments, Copilot autocompletion
can generate code based on the code and comments you have written. To use Copilot autocompletion:

1 In the Editor or Live Editor, type a comment, a function name, or some lines of code.
2 To generate code predictions using Copilot autocompletion, press the Enter key. The code
appears as a suggestion in dimmed text at your cursor location.

• To accept the suggestion, press Tab.


• To accept the next word of a suggestion composed of many words, press the Right arrow key.

1-17
1 MATLAB Copilot

• To discard the suggestion, press Esc.


3
To see the context menu of autocompletion options, click the button.

For example, write a script that calls the sin function and then the plot function. Then press Enter.
Copilot autocompletion generates code for a next possible step. The generated code includes a
comment that describes the suggestion.

Now write a script that calls the sin function. Instead of calling plot, add a comment that says,
"Make a scatter plot." Then press Enter. Copilot autocompletion treats that comment as a prompt
and generates code accordingly.

If you find autocompletions too obtrusive, then you can pause autocompletions and resume them
later. When you pause autocompletions, Copilot does not generate code but you still have access to
the autocompletion context menu. When you resume autocompletions after you had paused them,
Copilot can generate code again.

To pause autocompletions, open the autocompletion context menu by clicking the button to the left
of the suggestion and select Pause Autocompletions. The menu includes options to accept or
dismiss suggestions, pause autocompletions, or go to Copilot settings.

1-18
Generate and Modify Code Using MATLAB Copilot

To resume autocompletions after they have been paused, click the button and select Resume
Autocompletions. (When autocompletions are paused, the stars are unfilled and the context menu
changes to offer the Resume Autocompletions option.)

You can also pause autocompletions or disable them entirely by changing Copilot settings in the
Settings window. For more information, see “Change MATLAB Copilot Settings” on page 1-25.

Generate Comments
You can ask Copilot to generate comments for your code. To generate comments, highlight code in
your file and select Copilot > Generate Comments on the Editor or Live Editor tab. Or, right-click
and select Copilot > Generate Comments from the context menu.

1-19
1 MATLAB Copilot

You can accept or discard the generated comments, or view the differences between your code and
the commented code. You can also send feedback on the generated comments by clicking or .

1-20
Generate and Modify Code Using MATLAB Copilot

See Also

Related Examples
• “Set Up MATLAB Copilot” on page 1-3
• “Write Effective Prompts for MATLAB Copilot” on page 1-22
• “Explain Code Using MATLAB Copilot” on page 1-9

1-21
1 MATLAB Copilot

Write Effective Prompts for MATLAB Copilot


A prompt is text that you submit to a generative AI tool as a request to perform a task. Typically, a
prompt is a question or statement that you write using natural language. In MATLAB Copilot, you can
submit prompts using the Ask Copilot text box in the Copilot Chat panel, the Editor, the Live
Editor, and the Command Window.

Prompt engineering is the process of crafting effective prompts and guiding an AI tool to generate a
solution. To maximize the effectiveness of MATLAB Copilot, it is important to write clear and detailed
prompts. You might need to iterate through several prompts and responses to generate the expected
solution.

In the Copilot Chat panel, Copilot Chat returns conversational responses that can include both
natural language descriptions and formatted code (with comments). While you can submit any kind of
prompt, Copilot Chat is well suited to respond to prompts that ask for explanations, such as:

Explain why some matrices cannot be inverted

In the Editor, the Live Editor, and the Command Window, Copilot returns only formatted code (with
comments). When using Copilot in these panels, it is a good practice to write prompts that are
focused on writing code, such as:

Check if matrix can be inverted and invert it

Consider following these best practices for writing effective prompts.

Be Specific
When you write a prompt, clearly specify what you want Copilot to do. Clarify vague prompts by
providing detailed instructions.

For example, here is a vague prompt:

Create a plot

Instead, ask Copilot to create a specific kind of plot:

Create a sine wave plot from 0 to 2*pi with a title and labels

Describe Inputs and Outputs


Provide descriptions of the inputs and outputs that you expect. You can specify data types and sizes.
You can also just describe the inputs and outputs in words. Such details help Copilot understand your
requirements.

For example:

Generate a function that takes a numeric array and returns its mean and standard deviation

Break Down Complex Tasks


If your request involves multiple steps, break it down into simpler parts. You can submit the parts as
separate prompts or as a paragraph where you describe each part in a separate sentence.

1-22
Write Effective Prompts for MATLAB Copilot

For example:

First, generate code to read data from a CSV file.


Then, plot the data. Add a title, labels, and a legend.

Give Feedback and Describe Mistakes


Chat with Copilot in the Copilot Chat panel as though it were a colleague helping you with a project.
When Copilot Chat generates a response, you can follow up with a prompt that gives feedback. Tell
Copilot which parts of the response helped you and which parts did not. If Copilot made a mistake,
describe it in your follow-up prompt.

For example, ask Copilot to show you how to transpose a table.

Show me how to transpose a table

The response might include code to generate a sample table followed by a call to the transpose
function or operator. However, transpose operates on vectors and matrices only.

In this case, when you run the generated code, the result is an error message that suggests using the
rows2vars function instead. So, you can write a follow-up prompt that describes the mistake. In your
prompt, you can ask Copilot to work with the code or data it generated in its previous response.

transpose does not support tables. Show me how to use rows2vars


on the sample table from the previous response.

Iterate, Refine, and Validate


If the generated response is not what you expect, refine your prompt and try again. Verify that the
output is a valid result.

• Rewrite your prompt, adding more details as needed.


• Run generated code and review its output.
• Check that the generated code or text aligns with the documentation.
• If Copilot generated code that you do not understand, ask Copilot to explain it in more detail. If
Copilot has not already provided a line-by-line explanation, ask for one.
• Highlight the code, right-click and select Copilot > Explain Code for an explanation from
Copilot.

Describe Presentation of Results


Tell Copilot how you want it to present results. For instance, you can tell Copilot to present results as
a plot, an array, a table, or some other data structure. You can also tell Copilot that you want the
results written to a file.

For example, suppose that a previous Copilot response in the Copilot Chat panel returned results as a
table. You can request a different presentation of the results:

Return the results from the previous response as a timetable with variables
named "Signal" and "Status", sampled at 50 Hz.
Then write the timetable to a CSV file named mysignals.csv.

1-23
1 MATLAB Copilot

Clear Chat History


There are two situations where it is a good practice to clear the chat history:

• Before you start a conversation on a new topic.


• If the quality of the responses declines. Such a decline might happen when the chat history gets
long.

First, save anything of value from your current chat session. Save generated code that you want to
preserve. You can also save the entire chat history by selecting the Copy chat history option from
the Copilot Chat actions menu at the upper right corner of the Chat panel. Then, to clear the
chat history, click the Clear chat history button, also near the upper right corner of the panel.

The chat history does not persist between chat sessions.

Try Example Prompts


Copilot provides example prompts at the start of a chat session. Here are some example prompts to
help you to explore Copilot capabilities:

• Create a line plot that displays multiple sets of x and y data


• Roll two six-sided dice 1000 times and plot the sum of each roll
• Create a 3D bar graph of data from the magic function
• Generate noisy data from a cubic polynomial, preprocess the data, and plot
the results
• Solve the linear equations with coefficients A = [2, 4; 1, 3] and constants
B = [8; 5] and display the results

See Also

Related Examples
• “Set Up MATLAB Copilot” on page 1-3
• “Explain Code Using MATLAB Copilot” on page 1-9
• “Change MATLAB Copilot Settings” on page 1-25

1-24
Change MATLAB Copilot Settings

Change MATLAB Copilot Settings


You can change settings to configure MATLAB Copilot. Use the Settings window to interactively
access and modify MATLAB Copilot settings.

To open the Settings window for MATLAB Copilot settings, use one of these options:

• MATLAB Toolstrip: On the Home tab, in the Environment section, click Settings. Then, in the
left pane of the Settings window, select MATLAB Copilot.
• Copilot status indicator: In the lower left corner of the MATLAB desktop, click the Copilot status
indicator and select Copilot Settings.

• MATLAB command prompt: Enter preferences 'MATLAB Copilot'.

Disable MATLAB Copilot


If you have a license for MATLAB Copilot, then it is enabled by default. You can disable Copilot and
hide its features in MATLAB by clearing the Enable MATLAB Copilot check box in the MATLAB
Copilot Settings window.

1-25
1 MATLAB Copilot

Disable or Pause Autocompletions


In the Editor and Live Editor, you can use Copilot autocompletion to generate code. You can manage
autocompletion behavior by using the MATLAB Copilot Settings window.

The Copilot autocompletions mode list has three options:

• Enabled — In the Editor, Copilot autocompletion generates code after you press the Enter key.
The generated code appears next to the button at the cursor location. This behavior is the
default behavior.
• Disabled — Copilot autocompletion does not generate code. Pressing Enter results in a new
line.
• Paused — Copilot autocompletion does not generate code after you press Enter. The button
appears at the cursor location, but without generated code.

You also can disable autocompletions programmatically using the


matlab.editor.autocompletions.EnableAutocompletions.TemporaryValue setting. You
can set its value as:

• 0 — Disabled
• 1 — Enabled
• 2 — Paused

For example, disable autocompletions.

1-26
Change MATLAB Copilot Settings

s = settings;
s.matlab.editor.autocompletions.EnableAutocompletions.TemporaryValue = 0;

To enable autocompletions, select Enabled from the Copilot autocompletions mode list in the
Settings window or programmatically set
matlab.editor.autocompletions.EnableAutocompletions.TemporaryValue to 1.

See Also

Related Examples
• “Set Up MATLAB Copilot” on page 1-3
• “Explain Code Using MATLAB Copilot” on page 1-9
• “Generate and Modify Code Using MATLAB Copilot” on page 1-12

1-27

You might also like