Mike-C
Mike-C

Reputation: 43

Matlab: How to display Matrix as Image in GUI

I’m working on my first GUI in Matlab. It’s gonna get kinda big but I’m starting very basic. So far all I’ve got is a button and axes.

I’m looping a matrix that is being updated every time it goes through the loop. I’d like to display this matrix in my GUI.

When I take out “axes(handles.axes1)” my GUI shuts down and a new window opens with exactly the picture I want on my GUI. When I leave it in all I get is Errors:

Reference to a cleared variable handles.

Error in RackWriter>onOff_Callback (line 141)

axes(handles.axes1)

Error in gui_mainfcn (line 95)

    feval(varargin{:});

Error in RackWriter (line 42)

gui_mainfcn(gui_State, varargin{:});

Error in @(hObject,eventdata)RackWriter('onOff_Callback',hObject,eventdata,guidata(hObject)) Error while evaluating DestroyedObject Callback

Anyone knows what I’m doing wrong?

Thanks so much in advance

Here’s how the matrix is created and how i was planning on showing it:

        % 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]);

Upvotes: 0

Views: 806

Answers (2)

Mike-C
Mike-C

Reputation: 43

    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);

Upvotes: 0

sdbonte
sdbonte

Reputation: 102

This should do the trick:

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

If you want to update your figure in a later stage, you can then use:

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

Also, make sure to put the next line after each function in you code, it updates the handles:

    guidata(hObject,handles);

Upvotes: 0

Related Questions