Claudifying MATLAB

Claudifying MATLAB
Duncan Carlsmith
Department of Physics, University of Wisconsin-Madison
Decorative image illustrating application of AI in education
Introduction
AI mobile and desktop apps are convenient interfaces for exploration. For programmatic work not subject to variations in AI response, an AI can generate standalone code and web workflows. (See e.g. Web Automation with Claude, MATLAB, Chromium, and Playwright.) Intermediate is your own code that can batch process input data with AI via API.
Large Language Models (LLMs) with MATLAB permits a script to connect to OpenAI Chat Completions and Images, Azure OpenAI, Ollama, and other services that accept the OpenAI format. This submission introduces an educational Live Script Claude API from MATLAB for Coursework that illustrates how to access Anthropic models via API with MATLAB, using example tasks relevant to physics education, and might be adapted for other AI vendor APIs or emulated for other applications. This submission is essentially the Live Script introduction.
The script lists the available Claude models, sends text, images, and PDFs to a chosen model, holds a multi-turn conversation, extracts structured data from a document, lets Claude call MATLAB functions, and grades a set of short answers against a rubric. The examples show how an instructor or a student might use an AI model as a programmable assistant, for feedback on a figure, a check of a lab report, or a first pass at grading.
The Messages API is a web service at https://api.anthropic.com/v1/messages. A program sends it a request in JSON, the text format used for structured data on the web, and receives Claude's reply in JSON. A request names a model, sets a limit on the length of the reply, and carries a list of messages. An optional system prompt holds standing instructions that apply to every turn. The service keeps no memory between requests, so each request carries the whole conversation so far. MATLAB builds each request as a struct and jsonencode converts it to JSON.
Lengths and prices are counted in tokens. A token is a fragment of text, on average about four characters of English. Each response reports how many input and output tokens it used.
Helper functions in this folder handle the web requests and the bookkeeping. claudeRequest sends one request and reports the server's error message on failure. claudeListModels returns the models available to the key. claudeConversation and askClaude hold a multi-turn conversation and add up tokens and cost. claudeImageBlock and claudePdfBlock package a file for a message, claudeText extracts the reply text, and claudeCost converts token counts to dollars.
The example inputs sit in the inputs folder, which the Python version shares, and are fictional. example_flawed_figure.png is a plot with deliberate defects. example_lab_report.pdf is a student lab report with three planted errors, and example_lab_report_page1.png is an image of its page for display. student_answers.csv holds five short answers of varying quality. Output files go to the matlab_outputs folder and carry a timestamp in the name, so no run overwrites an earlier one, and the MATLAB and Python outputs stay apart.
"Try this" blocks mark the settings to change. The references list the API documentation. The appendix on privacy and security describes where requests go, how long the vendor keeps them, and precautions for student work and for the API key. A parallel Python version of the script and helper functions is in the Python folder. These functions could be called from MATLAB or invoked from a terminal.
Acknowledgments and disclaimer
The author has no financial interest in any company or product named here. Nothing in this post is endorsed by, sponsored by, or an official position of the University of Wisconsin-Madison.