The MATLAB Blog

Practical Advice for People on the Leading Edge

Using AI to help write MATLAB code – The MATLAB AI Chat Playground

Along with almost everyone who works in software development, I’ve recently been exploring how to integrate AI systems in my day-to-day work. In a MATLAB context, my appetite was whetted by the unofficial MatGPT application developed by friend and colleague Toshi Takeuchi but it wasn’t long before I wanted more. Then, the deep learning team released an open-source LLMs to MATLAB project on GitHub.
For the last few months I’ve been an internal beta tester of the MATLAB AI Chat Playground that was released to the public last week. It has been an interesting experience and I thought that I’d share some of the mental stages I went through while working with it.

Stage 1: I hate it and I can’t trust it.

During a casual one on one with Hans Scharler, he introduced an early prototype to me with very little fanfare. “What do you think of this?”.
I was working on a project involving clustering at the time and started asking it in-depth questions about the work I was doing. It did OK on some things but was blatantly making up others. It produced code that didn’t work using MATLAB functions that didn’t exist and cited papers it made up. I was not impressed.
I hate it! How could I ever trust it? I can’t see how this would ever be useful to me.”
I spent the rest of the day pinging Hans with other mistakes the AI had made. Honestly, it’s a wonder we are still friends.

Stage 2: Well…this is kind of fun!

Development on the project continued and Hans didn’t bring it up with me again until he gave an internal demo at yet another casual drop-in meeting we have at MathWorks for anyone vaguely interested in generative AI. I wasn’t even in the meeting, but I was getting messages from the meeting chat and someone posted a code snippet that got my interest.
I jumped into the meeting and started playing with the latest version of the app. This time I approached it with a totally different mindset. Simple curiosity about what it could and couldn’t do and, this time, I was enchanted.
Here’s one of my early queries from back then. It's a screenshot so you won't be able to copy and paste the code I'm afraid
Picture1h.png
Run that code and you get exactly what I had in mind
Picture2.png
I then asked it to draw me a picture of a cat. The answer was a little too literal!
img = imread('cat.jpg')
imshow(img)
So, I tried again
% Draw the head
rectangle('Position', [0.5 1 2 2], 'Curvature', [1 1], 'FaceColor', 'k');
hold on;
 
% Draw the ears
rectangle('Position', [0.5 2.5 0.5 0.5], 'Curvature', [1 1], 'FaceColor', 'k');
rectangle('Position', [2 2.5 0.5 0.5], 'Curvature', [1 1], 'FaceColor', 'k');
 
% Draw the eyes
rectangle('Position', [1 1.5 0.5 0.5], 'Curvature', [1 1], 'FaceColor', 'w');
rectangle('Position', [2 1.5 0.5 0.5], 'Curvature', [1 1], 'FaceColor', 'w');
 
% Draw the pupils
rectangle('Position', [1.25 1.75 0.1 0.1], 'Curvature', [1 1], 'FaceColor', 'k');
rectangle('Position', [2.25 1.75 0.1 0.1], 'Curvature', [1 1], 'FaceColor', 'k');
 
% Draw the nose
line([1.75 2.25], [1.75 1.75], 'LineWidth', 2, 'Color', 'k');
line([1.75 1.75], [1.75 1.5], 'LineWidth', 2, 'Color', 'k');
 
% Draw the mouth
line([1.5 2], [1.25 1.25], 'LineWidth', 2, 'Color', 'k');
hold off
I see where it’s going but there are some obvious mistakes. The eyes are too low down and the mouth is in the wrong place. Let's ask the AI to fix it
figure
% Draw the head
rectangle('Position', [0.5 1 2 2], 'Curvature', [1 1], 'FaceColor', 'k');
hold on;
 
% Draw the ears
rectangle('Position', [0.5 2.5 0.5 0.5], 'Curvature', [1 1], 'FaceColor', 'k');
rectangle('Position', [2 2.5 0.5 0.5], 'Curvature', [1 1], 'FaceColor', 'k');
 
% Draw the eyes
rectangle('Position', [1 1.75 0.5 0.5], 'Curvature', [1 1], 'FaceColor', 'w');
rectangle('Position', [2 1.75 0.5 0.5], 'Curvature', [1 1], 'FaceColor', 'w');
 
% Draw the pupils
rectangle('Position', [1.25 1.95 0.1 0.1], 'Curvature', [1 1], 'FaceColor', 'k');
rectangle('Position', [2.25 1.95 0.1 0.1], 'Curvature', [1 1], 'FaceColor', 'k');
 
% Draw the nose
line([1.75 2.25], [1.75 1.75], 'LineWidth', 2, 'Color', 'k');
line([1.75 1.75], [1.75 1.5], 'LineWidth', 2, 'Color', 'k');
 
% Draw the mouth
line([1.5 2], [1.35 1.35], 'LineWidth', 2, 'Color', 'w');
hold off
It’s not going to win any awards but it is starting to head in the right direction! See what I did there.
This is a totally different way of programming and I’ve disappeared down several such rabbit-holes since my early cat-pictures. I have a half-finished MATLAB implementation of space-invaders somewhere that was written in collaboration with AI. Writing it was more fun than I like to admit to!

Stage 3: Getting practice by using AI in low-stakes situations

Becoming proficient in any technology takes practice and AI is no different. One place I’ve been using it this week is in the new MATLAB Flipbook Mini Hack Contest. Many of my entries were made with a little (sometimes a lot) of help from AI.
Picture1.gif
I fed Adam’s code to the AI and asked it to help me figure out how to make the triangles multicolored.
It didn’t actually manage to write the code for me in this case, but it did help me figure out what I needed to do to get where I wanted. It very much felt like a partnership. Here’s the result
Picture2.gif
I also took my cat obsession to the next level with this - ChatGPT Kitty.
Picture3.gif
Why I didn’t call it CatGPT is beyond me?!

Stage 4: Using AI for real work

I’ve still not fully made it to Stage 4 yet although I have started to leverage AI for real work here and there. I’ve used AI to help me vectorise loops, for example. I’ve also used it to suggest ways to analyze data, help me fix bugs in my own code, or suggest starting points for demonstrations I want to write.
As I continue to play around, I feel my confidence growing just as AI itself is improving. It’s worth bearing in mind that the AI we have today is the most rudimentary version of it we’ll ever see. It has improved quite a lot since ChatGPT was introduced late last year.

Over to you

Which stage in your AI journey are you? Have you played around with the MATLAB AI Chat Playground? Maybe you’ve used another AI system to help you write code? How have you used it? What do you think?
|
  • print

댓글

댓글을 남기려면 링크 를 클릭하여 MathWorks 계정에 로그인하거나 계정을 새로 만드십시오.