无法使用按钮保存数据(MATLAB)

ilmyapht  于 2022-11-30  发布在  Matlab
关注(0)|答案(1)|浏览(203)

我正在尝试创建一个图形,用户可以在其中选择要打开或关闭的单元格。然后,用户可以单击按钮“Enter”将图板保存为数组。我成功地找到了一种在绘图中创建交互性的方法,这要归功于我在这里找到的一个非常有用的解释。我只是做了一些修改以满足我的需要。
但是,我找不到保存图板的方法。按钮是工作的(或者至少不是不工作的),但是数据没有保存。我不知道如何解决这个问题。任何帮助都将不胜感激。
下面是我的代码:

function CreatePattern
    hFigure = figure;
    hAxes = axes;
    axis equal;
    axis off;
    hold on;

    % create a button to calculate the difference between 2 points
    h = uicontrol('Position',[215 5 150 30],'String','Enter','Callback', @SaveArray);
    
    function SaveArray(ButtonH, eventdata)
        global initial
        initial = Board;
        close(hFigure)
    end

    N = 1; % for line width
    M = 20; % board size
    squareEdgeSize = 1;

    % create the board of patch objects
    hPatchObjects = zeros(M,M);
    for j = M:-1:1
        for k = 1:M
            hPatchObjects(M - j+ 1, k) = rectangle('Position', [k*squareEdgeSize,j*squareEdgeSize,squareEdgeSize,squareEdgeSize], 'FaceColor', [0 0 0],...
                'EdgeColor', 'w', 'LineWidth', N, 'HitTest', 'on', 'ButtonDownFcn', {@OnPatchPressedCallback, M - j+ 1, k});
        end
    end

    %global Board
    Board = zeros(M,M);
    playerColours = [1 1 1; 0 0 0];
    
    xlim([squareEdgeSize M*squareEdgeSize]);
    ylim([squareEdgeSize M*squareEdgeSize]);
    
    function OnPatchPressedCallback(hObject, eventdata, rowIndex, colIndex)
        % change FaceColor to player colour
        
        value = Board(rowIndex,colIndex);
        if value == 1
            set(hObject, 'FaceColor', playerColours(2, :));
            Board(rowIndex,colIndex) = 0; % update board
        else 
            set(hObject, 'FaceColor', playerColours(1, :));
            Board(rowIndex,colIndex) = 1; % update board
        end

    end
end

%imwrite(~pattern,'custom_pattern.jpeg')
bttbmeg0

bttbmeg01#

通过在工作区中保存为单独的文件找到了解决方法。

function SaveArray(ButtonH, eventdata)
        save('Board.mat', 'Board');             % Can be used to simply save your matrix to your current folder with name Board.mat
        assignin('base','BoardVariable',Board)      % Used to simply output your matrix to your working space
        close(hFigure)
end

感谢您在matlab discord服务器上的Footy#8799!

相关问题