File Exchange

image thumbnail


version (43.5 KB) by Stefan Bleeck
Sudoku for MATLAB.


Updated 20 Dec 2005

No License

Editor's Note: This file was a File Exchange Pick of the Week

Sudoku is a game that is very fashinable in Britain at the moment. Everyone seems to be captured.

The rules are very simple:
Fill in the grid so that every row, every column, and every 3x3 box contains the digits 1 through 9.

That's all there is to it. There's no math involved. The grid has numbers, but nothing has to add up to anything else. You solve the puzzle with reasoning and logic. It's fun. It's challenging. It's addictive!

I tried one of these puzzles last Sunday and spent the rest of the afternoon with the Times on my lap scribbling little numbers only to find out after hours that my first guess was wrong and I had to restart. How frustrating!

So I decided to solve this problem once for all. Why must I think when that can be done by my computer?

You can use the program for solving sudokus yourself (and get some help if you need it) or let the computer solve them for you (much more satisfactory :)

You can also generate your own puzzles. But please be aware that the generating option is not too sophisticated. It works by basically finding a solution to a random sudoku and then taking away random numbers.
A simple rule is applied to see if the puzzle is unique: the solution of the puzzle is compared to the solution of the transposed puzzle. If both are the same then the puzzle is very probable unique. For difficult puzzles this sometimes takes some time.

unzip (the zip contains also the gui-files, so its some files. The actual sudoku file is only one .m-file)

change into the created directory
type "sudoku"
A figure appears in the top right corner that gives you options:
"Emtpy puzzle" creates an empty field that you can fill with whatever you like
"file name" declares the name of the file that you can save or load (the latest one from the Times is in "sudoku_saved").
"load puzzle" loads the puzzle with the filename.
"save puzzle" saves the current puzzle under the filename.
"check possible" plots small pencilmarkers for all possible entries (that are all entries that are not conflicting with the existing entries).
"check neccessary" checks all fields if its neccessary to be filled with a number (checks each line, column and subsquare for fields in which only one number can go).
"solve" uses the two methods and finds a solution recursivly.

Start with loading the suggested puzzle (its a very tough one!) You can try to solve it yourselve by clicking into a square and insert a number between 1 and 9. If you type in 0 the square is cleared. If its not a valid number (contradicts the rules as stated above) the number is printed in red.

Pressing the "check possible" button shows you what numbers are possible at every given time.

Try to solve an empty puzzle or create some that have funny properties (e.g all magic squares...)

If you are looking for more puzzles try eg,,18209,00.html or

Q: When I start your program get the message
??? Undefined function or variable 'uipanel'.
A: Sorry, you need MATLAB version 7 for this program

Q:??? Error using ==> class
Class parameter is not a valid class name...
A: this error results from a wrong zip-file. Its solved now.

If for what ever reason the gui appears partly out of the screen, type in "movegui", to recenter it

Cite As

Stefan Bleeck (2021). Sudoku (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (20)

Andres .

Nice Job. It would be even better to have the suggested numbers in a diferent color so they don't are confused with the ones I guess.

Lucas Lopes

Very good...
I never see something like these in matlab progamation...
Tanks....and exuse-me for the bad english

Omer Demirkaya

I am also novice sudoku player. Last night it occurred to me that this could be programmed in Matlab. As usual, first thing in the morning I checked Matlab File Exchange not to rediscover the wheel. And here it is! Excellent job! Thanks.

Hoa Ngo

David Trumper

I really enjoyed using this, and have been giving my kids puzzles generated with your code. Thanks!

Torkil Deigaard

Remember to select "Include folder information" when unzipping. This creates the @parameter directory for storing the parameter class.

Matthias Isler

S Vora

Error results as follows : release R14 service pack 1
??? Error using ==> class
Class parameter is not a valid class name.

Error in ==> parameter at 90

Error in ==> sudoku at 34
params=parameter('sudoku controls');

J Mook

Release 14, I get the error:

??? Error using ==> class
Class parameter is not a valid class name.

Error in ==> parameter at 90

Error in ==> sudoku at 34
params=parameter('sudoku controls');

Maurice Hendrix


Georg Ogris

(1) the GUI has got some bugs
(2) bad programming style
(3) Due to the fact that the Sudokus are generated by deleting random entries there is no warranty that the sudokus are uniquely solvable and in fact all the "difficult" and most of the "mild" and most of the "easy" sudokus I tested had more than one solution.

Robert Adams

Handy for Sudoku addicts. One problem, on my system with 1600x1200 resolution the parameter gui is located in the upper left corner of the screen and is partially off of the screen.


Joe Lastname

Really liked the game. I found it interesting that I could alter the numbers given to me by the computer.

Sun Liantao

Fantastic!I thing it's more amused than "the game of go".Let's thank the author.

Brett Shoelson

Stefan...very nice! I'm getting hooked on these damned things. But I have one suggestion: "proper" sudoku puzzles should have unique solutions; yours often do not. A modification to ensure uniqueness would be nice.

wayne kiernan

Great Game

Luc Jonckheere

Great program!

Mat Labor

Great program, easy to use, Intuitive, in one word - excellent!

daniel miler

fantastic just brilliant

MATLAB Release Compatibility
Created with R14
Compatible with any release
Platform Compatibility
Windows macOS Linux

Inspired by: Create Interactive GUIs

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!