MATLAB Copilot Users Guide R2025a - MathWorks
MATLAB Copilot Users Guide R2025a - MathWorks
R2025a
How to Contact MathWorks
Phone: 508-647-7000
MATLAB Copilot
1
MATLAB Copilot Product Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-2
iii
1
MATLAB Copilot
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
• 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.
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.
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:
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
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
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
Provide Feedback
MathWorks uses your feedback to improve MathWorks products and services.
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
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
After Copilot generates an explanation, you can ask follow-up prompts, such as:
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
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.
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.
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
Copilot returns generated code in code boxes. Every code box includes these three buttons:
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:
1-16
Generate and Modify Code Using MATLAB Copilot
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.
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.
1-17
1 MATLAB Copilot
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
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:
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:
Be Specific
When you write a prompt, clearly specify what you want Copilot to do. Clarify vague prompts by
providing detailed instructions.
Create a plot
Create a sine wave plot from 0 to 2*pi with a title and labels
For example:
Generate a function that takes a numeric array and returns its mean and standard deviation
1-22
Write Effective Prompts for MATLAB Copilot
For example:
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.
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
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.
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
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.
1-25
1 MATLAB Copilot
• 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.
• 0 — Disabled
• 1 — Enabled
• 2 — Paused
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