design a function that runs a every digit from a matrox in matlab

Assuming that the matrix contains integers or floating point numbers, the following function will loop through each element of the matrix, extract each digit from the element and display it:

main.m
function display_digits(matrix)
% This function loops through each element of the matrix, extracts each digit 
% from the element and displays it.

    % Loop through each row of the matrix
    for i = 1:size(matrix, 1)
        
        % Loop through each column of the matrix
        for j = 1:size(matrix, 2)
            
            % Extract the element from the matrix
            curr_element = matrix(i,j);
            
            % Convert the element to a string
            element_str = num2str(curr_element);
            
            % Loop through each character in the string
            for k = 1:length(element_str)
                
                % Display the digit
                curr_digit = str2num(element_str(k));
                disp(curr_digit);
                
            end
            
        end
        
    end
    
end
859 chars
31 lines

To use this function, simply supply a matrix to it, like so:

main.m
my_matrix = [123, 456; 789, 432];
display_digits(my_matrix);
61 chars
3 lines

This will output each digit from each element in my_matrix line by line.

gistlibby LogSnag