Update an existing figure window on a GUI button click - matlab

I need to update a separate figure window iteratively on a button click on a GUI. Below is the relevant part of the my current code. It does not plot on the figure window. Hence creates a video with no plot in it. Thank you.
function pushbutton14_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton14 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.pushbutton14,'string','Creating...','enable','off');
folV = handles.folV;
files1T8 = dir(strcat(folV, '*.mat'));
numFiles1T8 = handles.numFiles1T8;
vAngle = handles.vAngle;
hAngle = handles.hAngle;
s = 1;
pS = 1;
vName = strcat(datestr(now,'yyyymmddHHMMSS'),'.avi');
handles.vName = vName;
guidata(hObject,handles);
writerObj = VideoWriter(strcat(folV,vName));
writerObj.FrameRate = 4;
open(writerObj);
az = 0;
el = 0;
for i = 1:numFiles1T8
load(strcat(folV, files1T8(i).name));
h = figure();
scatter3(pc(1:s:size(pc,1),1), pc(1:s:size(pc,1),2), pc(1:s:size(pc,1),3),pS,pc(1:s:size(pc,1),4:6)/255,'filled');
axis equal;
axis off;
view(az, el);
f = getframe;
fi = frame2im(f);
writeVideo(writerObj,fi);
clear pc;
az = az + hAngle;
el = el + vAngle;
close(h);
end
close(writerObj);
set(handles.pushbutton14,'string','Done','enable','off');

Take a look at refreshdata
http://ch.mathworks.com/help/matlab/ref/refreshdata.html?refresh=true
You could then so something like
axh = plot(xdata, ydata);
//... change the values of xdata and ydata
refreshdata(axh)

Related

I would like to record time in seconds each time i am crossing a particular region in Matlab Guide

I would like to track the cursor position within the axes and adds a marker like this for instance(.).
Furthermore, I would like to record and display how much time was spent within a particular region (rectangle, indicating green each time the cursor is within the rectangle.)?
All this done in Guide .
The output in the fig is very different to what I was expecting.
See pictures.
1)The first picture is what I was expecting
2)The second pictures is what I have.
% --- Executes on mouse motion over figure - except title and menu.
function finger_WindowButtonMotionFcn(hObject,~, handles)
% hObject handle to finger (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handles = guidata(hObject);
pos = get(hObject, 'currentpoint'); % get mouse location on figure
global x;
global y;
x = pos(1);
y = pos(2); % assign locations to x and y
set(handles.xloc, 'string', ['x loc:' num2str(x)]); % update text for x loc
set(handles.yloc, 'string', ['y loc:' num2str(y)]); % update text for y loc
% Determine if mouse is within the region
p_x = get(handles.region1,'XData');
p_x = p_x([1 2]);
p_y = get(handles.region1,'YData');
p_y = p_y([1 3]);
ax_xl = get(handles.axes1,'XLim');
ax_yl = get(handles.axes1,'YLim');
ax_units = get(handles.axes1,'Units');
if ~strcmp(ax_units,'pixels')
set(handles.axes1,'Units','pixels')
end
ax_pos = get(handles.axes1,'Position'); % axes1 position in pixels
if ~strcmp(ax_units,'pixels')
set(handles.axes1,'Units',ax_units);
end
% convert the patch XData and YData from axes coordinates to figure coordinates in pixels
p_x = (p_x-ax_xl(1))/(ax_xl(2)-ax_xl(1))*ax_pos(3)+ax_pos(1);
p_y = (p_y-ax_yl(1))/(ax_yl(2)-ax_yl(1))*ax_pos(4)+ax_pos(2);
persistent timein;
if isempty(timein)
timein = datetime('now');
end
if x >= p_x(1) && x <= p_x(2) && y >= p_y(1) && y <= p_y(2)
timein = datetime('now');
set(handles.region1,'FaceColor','g');
%writeline(handles.arduinoObj, "4&MOTOR_1_2_3_4&0!");
else
timeInPatch = seconds(datetime('now')-timein);
ax = ancestor(handles.region1, 'axes');
cp = ax.CurrentPoint;
text(ax, 0.52, cp(1,2), sprintf('%.3f sec.', timeInPatch), ...
'HorizontalAlignment','Left', ...
'VerticalAlignment','bottom', ...
'FontSize', 12, ...
'FontWeight', 'bold', ...
'Color', 'b')
set(handles.region1,'FaceColor','r');
%writeline(handles.arduinoObj, "0&MOTOR_1_2_3_4&0!");
end

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

matlab: inputdlg dialog box comes with tick labels

I have a user input request box basically written as matlab suggests in http://www.mathworks.com/help/matlab/ref/inputdlg.html .
prompt = {'Enter spot number'};
dlg_title = 'Input';
num_lines = 1;
def = {'9'};
answer = inputdlg(prompt,dlg_title,num_lines,def);
handles.count = str2double(answer{1});
However, the dialog box comes with axis labels as the image shows in the link below:
http://imgur.com/yPUYTJb
Edit:
It turns out that the windowsbuttonmotion function is doing the thing. I evaluated it within the function, not by calling a second function. Like this:
function figure1_WindowButtonMotionFcn(hObject, eventdata, handles)
global loop_counter diameter
pos = get(gca, 'currentpoint'); % get mouse location on figure
x = pos(1,1); y = pos(1,2); % assign locations to x and y
set(handles.lbl_x, 'string', ['x loc:' num2str(x)]); % update text for x loc
set(handles.lbl_y, 'string', ['y loc:' num2str(y)]); % update text for y loc
const = diameter/2;
for i = 1:loop_counter
center(i,:) = getPosition(handles.trap(handles.unselected(i)))+const;
if(((x-center(i,1))^2+(y-center(i,2))^2)<= const^2)
setColor(handles.trap(handles.unselected(i)), 'red');
else
setColor(handles.trap(handles.unselected(i)), 'blue');
end
end
guidata(hObject, handles)
How can I get rid of them?
Thanks.

How to pass data from one GUI to an other? Matlab

I am using a sub GUI to open the main GUI. In the sub GUI a pushbutton aLLows the user to select the data files they would like to upload. The first file is dealt with seperately and is then passed to the main GUI using setappdata and getappdata. Here is the code for the push button in the sub GUI:
% --- Executes on button press in ManualMultiple.
function ManualMultiple_Callback(hObject, eventdata, handles)
% hObject handle to ManualMultiple (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
[FileName,PathName,FilterIndex] = uigetfile('*.txt*','MultiSelect','on');
numfiles = size(FileName,2);
setappdata(0,'files',numfiles)
FileData= cell(1,numfiles);
for ii = 1:numfiles
FileName{ii};
A=[];
entirefile =fullfile(PathName,FileName{ii});
fid = fopen(entirefile);
tline = fgets(fid);
while ischar(tline)
parts = textscan(tline, '%f;');
if numel(parts{1}) > 0
A = [ A ; parts{:}' ];
end
tline = fgets(fid);
end
fclose(fid);
FileData{ii} = A;
A = FileData{ii};
X1 = A(:,1);
Y1 = A(:,5);
DataToUse{ii} = [X1, Y1];
end
FirstLoopX1Y1 = DataToUse{1};
X = FirstLoopX1Y1(:,1);
Y = FirstLoopX1Y1(:,2);
setappdata(0,'XValue',X)
setappdata(0,'YValue',Y)
for i = 2:numfiles
OtherLoopsXY = DataToUse{i};
X3 = OtherLoopsXY(:,1);
Y3 = OtherLoopsXY(:,2);
DataUseLater{i} = [X3,Y3]
end
setappdata(handles.ManualMultiple,'Data',DataUseLater)
GUImainwindow
Then when I push a button in the main GUI i should get the data sent from the sub to preform calculations on this data. Here is the code:
% --- Executes on button press in CalculateIntensity.
function CalculateIntensity_Callback(hObject, eventdata, handles)
% hObject handle to CalculateIntensity (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Trapz function
starting_value = getappdata(0,'StartValue');
ending_value = getappdata(0,'EndValue');
StartingValue = str2num(starting_value);
EndingValue = str2num(ending_value);
A = getappdata(0,'XYarray');
%line 122 and 123 finds location of data in the entire spectrum
[~,indx1]=ismember(StartingValue,A,'rows');
[~,indx2]=ismember(EndingValue,A,'rows');
arrayfortrapz = A(indx1:indx2,1:2);
setappdata(0,'arraytapz',arrayfortrapz);
[value,index] = max(arrayfortrapz(:,2)); %finds max intensity / peak value
PeakValue = arrayfortrapz(index,1);
handles.Peak_Value = PeakValue;
guidata(hObject,handles);
X1 = arrayfortrapz(1:end,1);
Y1 = arrayfortrapz(1:end,2);
AUC = trapz(X1,Y1); %intergration
str = num2str(AUC);
[s,v] = listdlg('PromptString','Calculated Intensity:','SelectionMode','single','ListString',str,'ListSize',[200 200]);
IntensityValue = str(s,:);
setappdata(0,'IV',IntensityValue);
DataUseLater = getappdata(handles.ManualMultiple,'Data')
But when I push this button in the main GUI i get the errors:
Reference to non-existent field 'ManualMultiple'.
Error in MichelleLaycockGUImainwindow>CalculateIntensity_Callback (line 207)
DataUseLater = getappdata(handles.ManualMultiple,'Data')
Can anyone tell me how I could resolve this problem or even help me see where I am going wrong? Thanks in advance
What if you add guidata(hObject,handles) after this line:
setappdata(handles.ManualMultiple,'Data',DataUseLater) in your main window? It looks as if the handles structure was not updated after you created handles.ManualMultiple

How to use setappdata and getappdata with data created in loop? Matlab GUI

The code I am using overall for the GUI is quite big so I wont post all here, just what I feel is relivant.
What I'm doing is selecting multiple files to be processed, the files are selected in the first GUI and then something is done with them in a second GUI. I only display the data from the first file in a graph and use it seperatley so using setappdata and getappdata to pass this to the second GUI is fine. But I am having difficulty using setappdata and getappdata with the remaining files.
Here is some of my code:
% --- Executes on button press in ManualMultiple.
function ManualMultiple_Callback(hObject, eventdata, handles)
% hObject handle to ManualMultiple (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
[FileName,PathName,FilterIndex] = uigetfile('*.txt*','MultiSelect','on');
numfiles = size(FileName,2);
setappdata(0,'files',numfiles)
FileData= cell(1,numfiles);
for ii = 1:numfiles
FileName{ii};
A=[];
entirefile =fullfile(PathName,FileName{ii});
fid = fopen(entirefile);
tline = fgets(fid);
while ischar(tline)
parts = textscan(tline, '%f;');
if numel(parts{1}) > 0
A = [ A ; parts{:}' ];
end
tline = fgets(fid);
end
fclose(fid);
FileData{ii} = A;
A = FileData{ii};
X1 = A(:,1);
Y1 = A(:,5);
DataToUse{ii} = [X1, Y1];
end
FirstLoopX1Y1 = DataToUse{1};
X = FirstLoopX1Y1(:,1);
Y = FirstLoopX1Y1(:,2);
setappdata(0,'XValue',X)
setappdata(0,'YValue',Y)
for i = 2:numfiles
OtherLoopsXY = DataToUse{i};
X3 = OtherLoopsXY(:,1);
Y3 = OtherLoopsXY(:,2);
DataUseLater{i} = [X3,Y3]
setappdata(0,'DataForLater',DataUseLater)
end
GUImainwindow
The part I am concerned with is the last for loop for i = 2:numfiles its the data in this loop I want to pass to GUImainwindow.
To try open this I am using:
numfiles = getappdata(0,'files')
for n = 2:numfiles
DataUseLater{n}= getappdata(0,'DataForLater');
end
But this does not give me anything from DataUseLater, can anybody help me with this?
Thanks in advance