Matlab:如何在GUI中将Matrix显示为图像

时间:2016-06-21 12:21:33

标签: image matlab loops user-interface matrix

我正在使用Matlab开发我的第一个GUI。它会变得有点大,但我开始非常基本。到目前为止,我所拥有的只是一个按钮和轴。

我正在循环一个每次循环时都要更新的矩阵。我想在我的GUI中显示这个矩阵。

当我取出“axes(handles.axes1)”时,我的GUI关闭,一个新窗口打开,其中包含我想要的GUI图片。当我把它全部留下时,我得到的是错误:

引用已清除的变量句柄。

RackWriter中的错误> onOff_Callback(第141行)

轴(handles.axes1)

gui_mainfcn错误(第95行)

    feval(varargin{:});

RackWriter出错(第42行)

gui_mainfcn(gui_State, varargin{:});

错误 @(hObject,EVENTDATA)RackWriter( 'onOff_Callback',hObject,EVENTDATA,guidata(hObject)) 评估DestroyedObject Callback时出错

任何人都知道我做错了什么?

非常感谢

以下是矩阵的创建方式以及我计划如何展示矩阵:

        % Reshape data (1D -> 2D array)

data2d = zeros(nrow, ncol);

k = 1;

for i = 1:nrow

   for j = 1:ncol

      data2d(row_index(i), col_index(j)) = data(k);

      k = k + 1;

   end

end


%resize 16x10 image to 160x100 image

data2d_resized = imresize(data2d,10);

%sensetivity

axes(handles.axes1)

imshow(data2d_resized,[0 255]);

2 个答案:

答案 0 :(得分:0)

这应该可以解决问题:

    handles.figure = imshow(data2d_resized, [0 255], 'parent', handles.axes1);

如果您想在稍后阶段更新您的数字,则可以使用:

    set(handles.figure, 'CData', updated_matrix);

另外,请确保在代码中的每个函数之后放置下一行,它会更新句柄:

    guidata(hObject,handles);

答案 1 :(得分:0)

    function varargout = RackWriter(varargin)
    % Begin initialization code - DO NOT EDIT
    gui_Singleton = 1;
    gui_State = struct('gui_Name',       mfilename, ...
                       'gui_Singleton',  gui_Singleton, ...
                       'gui_OpeningFcn', @RackWriter_OpeningFcn, ...
                       'gui_OutputFcn',  @RackWriter_OutputFcn, ...
                       'gui_LayoutFcn',  [] , ...
                       'gui_Callback',   []);
    if nargin && ischar(varargin{1})
        gui_State.gui_Callback = str2func(varargin{1});
    end

    if nargout
        [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
    else
        gui_mainfcn(gui_State, varargin{:});
    end
    % End initialization code - DO NOT EDIT


    % --- Executes just before RackWriter is made visible.
    function RackWriter_OpeningFcn(hObject, eventdata, handles, varargin)

    % Choose default command line output for RackWriter
    handles.output = hObject;

    % Update handles structure
    guidata(hObject, handles);
    axes(handles.axes2)
    imshow('sensordeckelOben.jpg');

    % UIWAIT makes RackWriter wait for user response (see UIRESUME)
    % uiwait(handles.figure1);


    % --- Outputs from this function are returned to the command line.
    function varargout = RackWriter_OutputFcn(hObject, eventdata, handles) 

    % Get default command line output from handles structure
    varargout{1} = handles.output;


    % --- Executes on button press in onOff.
    function onOff_Callback(hObject, eventdata, handles)
    % hObject    handle to onOff (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)

%This is where my stuff begins
    % Preparations
    close all                   %close all figures
    clear all                   %clear all workspace variables
    fclose('all')               %close all Files
    delete(instrfindall)        %Reset Com Port
    delete(timerfindall)        %Delete Timers
    %clear handles

    % setup serial
    serialPort = serial('COM3');
    command = 'A';

    nrow = 16;
    ncol = 10;
    row_index = [9,10,11,12,13,14,15,16,8,7,6,5,4,3,2,1];
    col_index = [1,2,3,4,5,6,7,8,9,10];

    % 10x16 = 160 bytes
    lendata = 160;

    BaudRate = 115200;

    %InputBufferSize is bein displayed (disp(serialPort.BytesAvailable))
    %with only 322 Bytes. The more information it has to process, the more
    %bytes that havve to be stored in the InputBufferSize. But it seams to
    %not go over 400
    InputBufferSize = 500;

    Timeout = 1;

    set(serialPort , 'BaudRate', BaudRate);
    set(serialPort , 'InputBufferSize', InputBufferSize);
    set(serialPort , 'Timeout', Timeout);

    fopen(serialPort);

    while 1

    % Request data
    fprintf(serialPort, command);

    % Get data
    %Data is read as string (CSV)
    data_string = fgetl(serialPort);
    data_string_array = strsplit(data_string, ',');
    data = str2double(data_string_array);

    % Reshape data (1D -> 2D array)
    data2d = zeros(nrow, ncol);
    k = 1;
    for i = 1:nrow
       for j = 1:ncol
          data2d(row_index(i), col_index(j)) = data(k);
          k = k + 1;
       end
    end

    %resize 16x10 image to 160x100 image
    data2d_resized = imresize(data2d,10);
    %sensetivity [0 255]
    %axes(handles.axes1)
    imshow(data2d_resized,[0 50]);

    %clean out the InputBufferSize
    flushinput(serialPort)
    end
    fclose(serialPort);