if (Serial.read()=='!')

void printMatrix() {
    for (int rowIndex=0; rowIndex < rowCount; rowIndex++) {
        if (rowIndex < 10)
            Serial.print(F("0"));
        Serial.print(rowIndex); Serial.print(F(": "));
 
        for (int colIndex=0; colIndex < colCount; colIndex++) {  
            Serial.print(keys[colIndex][rowIndex]);
            if (colIndex < colCount)
                Serial.print(F(", "));
        }   
        Serial.println("");
    }
    Serial.println("");
}
 
void loop() {
    readMatrix();
    if (Serial.read()=='!')
        printMatrix();
}

In this code it look like the if(Serial.read()==’!’) prevents the program from printing out a repeated matrix.
Why does it do that?

Serial.read() returns a -1 if nothing is available to read.

Consider calling Serial.available(), to determine if characters are, in fact, available, before attempting to read them.

I’m not sure what the question is.
There is no readmatrix(), and the if() logic should work as written…?

jacobmyers822: In this code it look like the if(Serial.read()=='!') prevents the program from printing out a repeated matrix. Why does it do that?

looks like is will invoke printMatrix() when it receives a '!' after repeatedly (in loop()) reading a matrix.

jacobmyers822: In this code it look like the if(Serial.read()=='!') prevents the program from printing out a repeated matrix. Why does it do that?

Probably because the author wanted it to do that. If you want it to do something different, you should change it.