MATLAB: After giving row and column names to a matrix, how to access the particular elements in the matrix using the (rowname, column name)

access data using row and column namesrowname column name

S= [2 3 4 1;3 1 8 2;1 4 1 2;5 3 2 9;3 1 1 4;7 6 3 5;4 5 6 2];
printmat(S,'','A11 A12 A21 A22 A23 A31 A32','M1 M2 M3 M4')
Above code i used to generate the names of rows and columns shown below. Now I want to access the elements of matrix 'S' with my given row and column names. Example (A12, M3) =8. To this output I need the code. Thanks in advance
ans:
M1 M2 M3 M4
A11 2.00000 3.00000 4.00000 1.00000
A12 3.00000 1.00000 8.00000 2.00000
A21 1.00000 4.00000 1.00000 2.00000
A22 5.00000 3.00000 2.00000 9.00000
A23 3.00000 1.00000 1.00000 4.00000
A31 7.00000 6.00000 3.00000 5.00000
A32 4.00000 5.00000 6.00000 2.00000

Best Answer

PRINTMAT does not make any changes to your matrix at all. It does not "generate the names of rows and columns". As the name implies (it includes the word "print") PRINTMAT simply displays the matrix differently. In order to actually create a data object with row and column names, convert your matrix into a table and then you can use the names of the rows and variables in the table to index into it.