Search code examples
matlabgraphmatlab-guideline-plotmatlab-gui

Matlab GUI not showing line plot graph data


I'm working with my Matlab GUI file to play video and plot the mean value from the color channel (RGB). It has 2 axes, the first one for the video player and second axis for the mean graph, but the second axis is not showing any data, it just updates the x and y coordinate but not showing anything.

I've tried to change the handles, changes the next plot setting in the property inspector but it doesn't work

function main_OpeningFcn(hObject, eventdata, handles, varargin)

handles.output = hObject;
video = vision.VideoFileReader();
handles.video = video;
frameCount = 0;
handles.frameCount = frameCount;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes main wait for user response (see UIRESUME)
uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.

function varargout = main_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
handles.output = hObject;
varargout{1} = handles.output;
% --- Executes on button press in Browse.

function Browse_Callback(hObject, eventdata, handles)
% hObject    handle to Browse (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[ video_file_name,video_file_path ] = uigetfile({'*.avi'},'Pick a video file');      %;*.png;*.yuv;*.bmp;*.tif'},'Pick a file');
if(video_file_path == 0)
    return;
end
input_video_file = [video_file_path,video_file_name];
fullpath = strcat(video_file_path,video_file_name);
set(handles.edit1,'String',fullpath);
video = vision.VideoFileReader(input_video_file);
vidFrame = step(video);
axes(handles.axes1);set(handles.StartButton,'String','Start');
frameCount = 1;
imshow(vidFrame);
drawnow;
axis(handles.axes1,'off');
   for nChannel = 1:3
       colorChannel = vidFrame(:,:,nChannel);
       rawColorSignal(nChannel,frameCount) =  mean(mean(colorChannel));
   end

%plot(frameCount,rawColorSignal(1, :),frameCount,rawColorSignal(2, :),frameCount,rawColorSignal(3, :), handles.axes2);
axes(handles.axes2)
plot(frameCount,rawColorSignal(1, :));
grid on
drawnow;
axes(handles.axes1)
% Display Frame Number
%Update handles
handles.video = video;
guidata(hObject,handles);

% --- Executes on button press in StartButton.
function StartButton_Callback(hObject, eventdata, handles)
% hObject    handle to StartButton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if strcmp(get(handles.StartButton,'String'),'Pause')
    set(handles.StartButton,'String','Start');
else
    set(handles.StartButton,'String','Pause');
end
video = handles.video;
if  isDone(video)
    reset(video)
    frameCount = 0;
    handles.frameCount = frameCount;
end
frameCount = handles.frameCount;
 while ~isDone(video) && strcmp(get(handles.StartButton,'String'),'Pause')
     vidFrame   = step(video);
     imshow(vidFrame,'Parent',handles.axes1); %plot frame is specific axis
     drawnow;
    frameCount = frameCount + 1;
    for nChannel = 1:3
       colorChannel = vidFrame(:,:,nChannel);
       rawColorSignal(nChannel,frameCount) =  mean(mean(colorChannel));
    end

    plot(frameCount,rawColorSignal(1, :),'Parent',handles.axes2);
    grid on
    drawnow;
 end

%plot(frameCount,rawColorSignal(1, :),'r',frameCount,rawColorSignal(2, :),'g',frameCount,rawColorSignal(3, :),'b','Parent', handles.axes2);
%drawnow;
 set(handles.StartButton,'String','Start');
% --- Executes on button press in PauseButton.
function PauseButton_Callback(hObject, eventdata, handles)
% hObject    handle to PauseButton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
%axes(handles.axes2)
%surf(membrane(3))

What I expected is the line plot is visible and updating alongside the axes.


Solution

  • I think I found the problem:

    Replace: plot(frameCount,rawColorSignal(1, :));
    With: plot(1:frameCount,rawColorSignal(1, :));

    Replace: plot(frameCount,rawColorSignal(1, :),'Parent',handles.axes2);
    With: plot(1:frameCount,rawColorSignal(1, :),'Parent',handles.axes2);


    plot(frameCount,... uses the scalar frameCount as X coordinates.
    You want the X coordinates in your plot to be a vector that goes from 1 to frameCount.

    In case you are getting an error, try:
    plot(1:length(rawColorSignal(1, :)), rawColorSignal(1, :), 'Parent', handles.axes2);

    Not plotting


    I created the following sample code for demonstrating the problem:

    Following sample is not plotting:

    frameCount = 0;
    rawColorSignal = [];
    
    for i = 1:10
        frameCount = frameCount + 1;
        rawColorSignal(frameCount) =  i;
    end
    
    plot(frameCount, rawColorSignal);
    grid on
    drawnow;
    

    When replacing plot(frameCount, rawColorSignal); with plot(1:frameCount, rawColorSignal); it does:

    frameCount = 0;
    rawColorSignal = [];
    
    for i = 1:10
        frameCount = frameCount + 1;
        rawColorSignal(frameCount) =  i;
    end
    
    plot(1:frameCount, rawColorSignal);
    grid on
    drawnow;
    

    Plotting