MATLAB Answers

Matlab GUI becomes too heavy for computer and makes the system slower

28 views (last 30 days)
Muhammad Farhan  Mughal
Muhammad Farhan Mughal on 3 Apr 2016
Commented: Steven Lord on 5 Apr 2016
I am doing pair comparison in which I show pair of images and ask user to select one of them on the basis of different questions. For this I am using pair comparison. The MATLAB code is working fine but the problem is it slows down the problem is that the code make system (core i5, 4 Gb RAM) very slow after showing few pairs of images. Following is the code for my MATLAB GUI, I need to know how can I improve the performance of this code?,
function Farhan_Experiment_2_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to FArhan_GUI (see VARARGIN)
% Choose default command line output for FArhan_GUI
handles.output = hObject;
set(handles.axes1,'units','pixels','visible','off');
set(handles.axes2,'units','pixels','visible','off');
set(gcf, 'units','normalized','outerposition',[0 0 10 10]);
scrz=get(0,'ScreenSize');
fig_hr = 326;
fig_vr = 493;
pos1 = round((scrz(3)-fig_hr)/4);
pos2 = round((scrz(4)-fig_vr)/1.5);
arr = [1,2; 16,17; 31,32; 41,42; 6,7 ; 21,22; 26,27; 36,37; 11,12;
3,2; 18,17; 33,32; 43,42; 8,7 ; 23,22; 28,27; 38,37; 13,12;
3,4; 18,19; 33,34; 43,41; 8,9 ; 23,24; 28,29; 38,39; 13,14;
5,4; 20,19; 35,34 ; 10,9; 25,24; 30,29; 40,39; 15,14;
5,1; 20,16; 35,31 ; 10,6; 25,21; 30,26; 40,36; 15,11;
3,1; 18,16; 33,31 ; 8,6 ; 23,21; 28,26; 38,36; 13,11;
2,4; 17,19; 32,34 ; 7,9 ; 22,24; 27,29; 37,39; 12,14;
5,3; 20,18; 35,33 ; 10,8; 25,23; 30,28; 40,38; 15,13;
1,4; 16,19; 31,34 ; 6,9 ; 21,24; 26,29; 36,39; 11,14;
5,2; 20,17; 35,32 ; 10,7; 25,22; 30,27; 40,37; 15,12;
1,3; 16,18; 31,33 ; 6,8; 21,23; 26,28; 36,38; 11,13;
3,5; 18,20; 33,35 ; 8,10; 23,25; 28,30; 38,40; 13,15;
44,45; 59,60; 74,75; 84,85; 49,50; 64,65; 69,70; 79,80; 54,55;
46,45; 61,60; 76,75; 86,85; 51,50; 66,65; 71,70; 81,80; 56,55;
46,47; 61,62; 76,77; 86,84; 51,52; 66,67; 71,72; 81,82; 56,57;
48,47; 63,62; 78,77 ; 53,52; 68,67; 73,72; 83,82; 58,57;
48,44; 63,59; 78,74 ; 53,49; 68,64; 73,69; 83,79; 58,54;
46,44; 61,59; 76,74 ; 51,49; 66,64; 71,69; 81,79; 56,54;
45,47; 60,62; 75,77 ; 50,52; 65,67; 70,72; 80,82; 55,57;
48,46; 63,61; 78,76 ; 53,51; 68,66; 73,71; 83,81; 58,56;
44,47; 59,62; 74,77 ; 49,52; 64,67; 69,72; 79,82; 54,57;
48,45; 63,60; 78,75 ; 53,50; 68,65; 73,70; 83,80; 58,55;
44,46; 59,61; 74,76 ; 49,51; 64,66; 69,71; 79,81; 54,56;
46,48; 61,63; 76,78 ; 51,53; 66,68; 71,73; 81,83; 56,58;
];
setappdata(0,'arr',arr);
% for i =1:43*2
% handles.save_img{i} = imread ([num2str(i),'.tif']);
% end
pos3 = [pos1+150 pos2 fig_hr fig_vr];
set(handles.axes1,'pos',pos3);
axes(handles.axes1);
set(handles.axes1,'units','pixels');
posa = pos1 +1.5* round(fig_hr);
pos4 = [posa+150 pos2 fig_hr fig_vr];
set(handles.axes2,'pos',pos4);
handles.results = 0;
handles.text1 =uicontrol('Style','Text', 'HandleVisibility','off','Position' ,[pos1, pos2,500 ,300],'Value',0,'String','Two Images will be shown to you with a question select the image according to the question.Enter user number and press Enter to start', 'Tag','text1','FontSize',25);
handles.edit1 = uicontrol('Style','Edit', 'HandleVisibility','off','Position' ,[4*pos1, pos2,100 ,30]);
handles.button1 = uicontrol('Style', 'pushbutton','Callback', {@pushbutton1_Callback, ancestor(hObject, 'figure')} ,'Units','pixels', 'Position', [4*pos1, pos2-60,100 ,30 ], 'String', 'Enter');
varargout{1} = handles.output;
handles.save = 1;
i =1
handles.co = 1;
% guidata( hObject,handles);
setappdata(0,'i',i);
setappdata(0,'pos1',pos1);
setappdata(0,'pos2',pos2);
setappdata(0,'pos3',pos3);
setappdata(0,'pos4',pos4);
setappdata(0,'posa',posa);
guidata(hObject, handles);
% UIWAIT makes FArhan_GUI wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = Farhan_Experiment_2_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
guidata( hObject,handles);
function pushbutton1_Callback (hObject,eventdata,GUIfig)
handles = guidata(GUIfig);
i= getappdata(0,'i');
save_img{i} = imread ([num2str(i),'.tif']);
x = get(handles.edit1,'String');
handles.results = x;
set(handles.text1,'units','pixels','visible','off');
set(handles.edit1,'units','pixels','visible','off');
set(handles.button1,'units','pixels','visible','off');
pos1= getappdata(0,'pos1');
pos2= getappdata(0,'pos2');
posa= getappdata(0,'posa');
hBtnGrp = uibuttongroup('Position',[ 0 0 0.1 0.1], 'Units','Normalized','BackgroundColor',[0.5,0.5,0.5]);
uicontrol('Style','Radio', 'Parent',hBtnGrp, 'HandleVisibility','off','BackgroundColor',[0.5,0.5,0.5], 'Position',[(pos1+326+pos1)/2+150, pos2-70,70 ,50],'Value',0, 'String','A', 'Tag','A')
uicontrol('Style','Radio', 'Parent',hBtnGrp, 'HandleVisibility','off','BackgroundColor',[0.5,0.5,0.5],'Position' ,[(posa+326+posa)/2+150, pos2-70,70 ,50],'Value',0, 'String','B', 'Tag','B')
next= uicontrol('Style', 'pushbutton','Callback', {@pushbutton1, ancestor(hObject, 'figure'), hBtnGrp}, 'Units', 'pixels', 'Position', [(((pos1+326+pos1)/2)+(posa+326+posa)/2)/2+70, pos2- 140,70 ,50 ], 'String', 'Next','tag','pb1');
back= uicontrol('Style', 'pushbutton','Callback', {@pushbutton2, ancestor(hObject, 'figure'), hBtnGrp}, 'Units', 'pixels', 'Position', [(((pos1+326+pos1)/2)+(posa+326+posa)/2)/2+170,pos2- 140,70 ,50 ], 'String', 'Back','tag','pb1');
setappdata(0,'next',next);
setappdata(0,'back',back);
setappdata(0,'save_img',save_img);
qtext = {'Which one looks more Fussy?', ...
'Which one looks more Imaginative?', ...
'Which one looks more Attractive?', ...
'Which one looks more Masculine?', ...
'Which one looks more Intellegent', ...
'Which one looks more Mature'};
question1 = randperm(6);
question = question1(1);
my_str = qtext{question};
setappdata(0,'question',question);
setappdata(0,'question1',question1);
setappdata(0,'qtext',qtext);
uicontrol('Style','Text', 'HandleVisibility','off','Position',[(((pos1+326+pos1)/2)+(posa+326+posa)/2)/2-150, pos2-250,600 ,50 ],'BackgroundColor',[0.5,0.5,0.5],'Value',0,'String',my_str, 'Tag','text1','FontSize',25)
uicontrol('Style','Text', 'HandleVisibility','off','Position',[0, 10,100 ,50],'BackgroundColor',[0.5,0.5,0.5],'Value',0,'String',[num2str(handles.save),'out of 1188 images'], 'Tag','text1','FontSize',12)
handles.save_Data = [0 , 0 ,0, 0 ,0 ];
arr= getappdata(0,'arr');
% imshow(handles.save_img{handles.co},'Parent',handles.axes1);
% imshow(handles.save_img{handles.co+1},'Parent',handles.axes2);
save_img1 = imread ([num2str(arr(handles.co,1)),'.tif']);
save_img2 = imread ([num2str(arr(handles.co,2)),'.tif']);
imshow(save_img1,'Parent',handles.axes1 );
imshow(save_img2,'Parent',handles.axes2 );
guidata(GUIfig, handles)
function pushbutton1(hObject, event, GUIfig, hBtnGrp)
handles = guidata(GUIfig);
global data;
switch get(get(hBtnGrp,'SelectedObject'),'Tag');
case 'A', data = 1;
case 'B', data = 2;
end
img_select_1 = 0;
img_select_2 = 0;
if ( data == 1)
img_select_1 = 1;
end
if ( data == 2)
img_select_2 = 1;
end
% i = getappdata(0,'i');
arr= getappdata(0,'arr');
pos1= getappdata(0,'pos1');
pos2= getappdata(0,'pos2');
posa= getappdata(0,'posa');
save_img = getappdata(0,'save_image');
question= getappdata(0,'question');
question1= getappdata(0,'question1');
qtext= getappdata(0,'qtext');
next= getappdata(0,'next');
handles.save_Data(handles.save,1) = arr(handles.co,1);
handles.save_Data(handles.save,2) = arr(handles.co,2);
handles.save_Data(handles.save,3) = question;
handles.save_Data(handles.save,4) = img_select_1;
handles.save_Data(handles.save,5) = img_select_2;
handles.co = handles.co + 1;
if (handles.co > 198)
handles.co = 1;
end
if ((handles.save >= 198)&&(handles.save<396))
question = question1(2);
end
if ((handles.save >= 396 )&&(handles.save<594))
question = question1(3);
end
if ((handles.save >= 594)&&(handles.save<792))
question = question1(4);
end
if ((handles.save == 792)&&(handles.save<990))
question =question1(5);
end
if (handles.save >= 990)
question = question1(6);
end
if (handles.save ==1188)
save_Data = handles.save_Data;
save(['Exp2_results',num2str(handles.results),'.mat'],'save_Data');
close all
end
handles.save = handles.save + 1;
my_str = qtext(question);
uicontrol('Style','Text', 'HandleVisibility','off','Position',[(((pos1+326+pos1)/2)+(posa+326+posa)/2)/2-150, pos2-250,600 ,50 ],'BackgroundColor',[0.5,0.5,0.5],'Value',0,'String',my_str, 'Tag','text1','FontSize',25)
uicontrol('Style','Text', 'HandleVisibility','off','Position',[0, 10,100 ,50],'BackgroundColor',[0.5,0.5,0.5],'Value',0,'String',[num2str(handles.save),'out of 1188 images'], 'Tag','text1','FontSize',12)
save_img1 = imread ([num2str(arr(handles.co,1)),'.tif']);
save_img2 = imread ([num2str(arr(handles.co,2)),'.tif']);
imshow(save_img1,'Parent',handles.axes1 );
imshow(save_img2,'Parent',handles.axes2 );
guidata(GUIfig, handles)

  0 Comments

Sign in to comment.

Answers (1)

Jan
Jan on 3 Apr 2016
You create a bunch of new objects inside e.g the pushbutton1_Callback and even in the outputfcn without deleting the older objects. (At least as far as I can see - the posted code is hard to read in the forum's display due to the missing syntax highliting and the commented code lines). The more (unused) objects you have inside a figure, the slower does it react to user inputs.
Note: This block of code looks ugly:
c=1;
handles.(['text',num2str(c)])=('Which one looks more Fussy?');
c =c+1;
handles.(['text',num2str(c)])=('Which one looks more Imaginative?');
c=c+1;
handles.(['text',num2str(c)])=('Which one looks more Attractive?');
c=c+1;
handles.(['text',num2str(c)])=('Which one looks more Masculine?');
c=c+1;
handles.(['text',num2str(c)])=('Which one looks more Intellegent');
c=c+1;
handles.(['text',num2str(c)])=('Which one looks more Mature');
handles.question1 = [1,2,3,4,5,6];
handles.question1 = randsample(handles.question1,6);
What about:
handles.text = {'Which one looks more Fussy?', ...
'Which one looks more Imaginative?', ...
'Which one looks more Attractive?', ...
'Which one looks more Masculine?', ...
'Which one looks more Intellegent', ...
'Which one looks more Mature'};
handles.question1 = randperm(6);
my_str = handles.text{handles.question1(1)};
This is nicer and less prone to errors than divicing the text to different fields of the struct and a pseudo-index in the fieldname.

  4 Comments

Show 1 older comment
Jan
Jan on 5 Apr 2016
Does the figure contain 500 images? Then it is expected, that it is slow. Delete image objects, which are not used anymore. Or even better: Create the image object once only and update its CData property afterwards only.
Muhammad Farhan  Mughal
Muhammad Farhan Mughal on 5 Apr 2016
i have 98 images in total and and i make pairs from these images i have 1188 pairs of images so I need to show 1188 pairs of images. since the images repeat in the process so I cannot delete images. But I am not using handles to show image i just read the image in variable and display them like this
save_img1 = imread ([num2str(arr(handles.co,1)),'.tif']);
save_img2 = imread ([num2str(arr(handles.co,2)),'.tif']);
imshow(save_img1,'Parent',handles.axes1 );
imshow(save_img2,'Parent',handles.axes2 );
So that program should be fast. But it is still very slow
Steven Lord
Steven Lord on 5 Apr 2016
Jan's not suggesting you delete the image files or the image data, just the graphics objects.
ax = axes;
hold on
for k = 1:20
imagesc(rand(10))
pause(1)
end
This displays 20 random images, one at a time, with a pause of a second between each image. The image being displayed at the end is the last one generated; none of the previous images are visible. But are they still there?
findall(ax, 'Type', 'image')
The result shows that yes, all the images ARE present.
ans =
20x1 Image array:
*snip*
Even though those images aren't visible, they still exist consuming resources which will slow your code. Get rid of the image objects once they're no longer going to be visible, or create one image object and reuse it by changing its properties rather than creating hundreds of image objects.

Sign in to comment.

Tags

No tags entered yet.