function editPhaseDiagram(M, SCATTERING_LENGTH_RANGE, NUM_ATOMS_LIST)
    % Validate dimensions
    [ylen, xlen] = size(M);
    if length(NUM_ATOMS_LIST) ~= xlen || length(SCATTERING_LENGTH_RANGE) ~= ylen
        error('Matrix dimensions do not match axis lengths.');
    end

    % Create figure
    fig = figure('Name', 'Manual Phase Diagram Editor', ...
                 'NumberTitle', 'off', ...
                 'KeyPressFcn', @keyCallback);
    fig.WindowState = 'maximized';
    clf
    set(gcf,'Position', [100, 100, 1600, 900])
    
    hImg = imagesc(M);
    set(gca, 'YDir', 'normal');
    colormap(parula);
    colorbar;
    axis equal tight;
    xticks(1:xlen);
    yticks(1:ylen);
    xticklabels(string(NUM_ATOMS_LIST));
    yticklabels(string(SCATTERING_LENGTH_RANGE));
    xlabel('Number of Atoms', 'Interpreter', 'tex', 'FontSize', 16);
    ylabel('Scattering Length (\times a_o)', 'Interpreter', 'tex', 'FontSize', 16);
    grid on;
    title('Click a cell to modify value. Press "q" to quit.');

    set(fig, 'WindowButtonDownFcn', @clickCallback);
    uiwait(fig);  % Wait until user quits

    % Prompt for saving
    [filename, pathname] = uiputfile('edited_phase_diagram.mat', 'Save Edited Matrix As');
    if ischar(filename)
        save(fullfile(pathname, filename), 'M', 'SCATTERING_LENGTH_RANGE', 'NUM_ATOMS_LIST');
        disp(['Matrix saved to ' fullfile(pathname, filename)]);
    end

    % Callback: Mouse click to edit value
    function clickCallback(~, ~)
        C = get(gca, 'CurrentPoint');
        col = round(C(1,1));
        row = round(C(1,2));
        if row >= 1 && row <= ylen && col >= 1 && col <= xlen
            prompt = sprintf('Enter new value for a = %.2f, N = %d:', ...
                             SCATTERING_LENGTH_RANGE(row), NUM_ATOMS_LIST(col));
            answer = inputdlg(prompt, 'Edit Cell Value', 1, {num2str(M(row,col))});
            if ~isempty(answer)
                val = str2double(answer{1});
                if ~isnan(val)
                    M(row, col) = val;
                    refreshDisplay();
                else
                    warndlg('Invalid input. Please enter a numeric value.', 'Invalid Input');
                end
            end
        end
    end

    % Callback: Quit on 'q'
    function keyCallback(~, event)
        if strcmp(event.Key, 'q')
            uiresume(fig);
            close(fig);
        end
    end

    % Update image
    function refreshDisplay()
        set(hImg, 'CData', M);
        drawnow;
    end
end
