GUI problem - global variables

Hi,
when im trying to use some global variables, defined in ither funcion, thay change size. Why is that? First i was trying to use as a global variable XYZ matrix of images but matlab has problems with this so I use variable C1 as XY matrix where every row represents one image. But every function change size of C1, C22 and others.
There is part fo my code. And in attachments is full code with some test images
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global Name C1 r s all;
clear Name C1 r s all
[Name, Path] = uigetfile('*.*', 'All Files (*.*)','MultiSelect','on','C:\Users\admin\Documents\MATLAB\Pacienti');%('.png, .PNG')
if ~iscell(Name) %převod na cell v případě výběru pouze jednoho snímku
Name = {Name};
end
for i = 1:1:length(Name) %Vytvoření matice snímků celé sekvence
Nazev = Name(1,i);
Nazev = char(Nazev);
Img_info = [Path Nazev];
Img = imread(Img_info);
Img_matrix(:,:,i) = rgb2gray(Img); % Převod do odstínu šedi
end
%axes(handles.axes1);
imshow(Img_matrix(:,:,1));
[r,s]=size(Img_matrix(:,:,1));
all = r*s;
for i = 1:length(Name)
C1(i,:) = reshape(Img_matrix(:,:,i),1,all);
end
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global C1 C22 r s all Name;
clear C22 all
for i = 1:length(Name)
Img_res1(:,:,i) = reshape(C1(i,:),r,s);
end
Img1 = Img_res1(:,:,1);
axes(handles.axes1)
imshow(Img1,colormap(gray(256)))
axis off
[XData2,YData2] = imcrop(Img1); %Uložení ořezávajícího okna
rect = YData2;
for i = 1:1:length(Name) %Aplikace okna na celou sekvenci
img_m(:,:,i) = imcrop(Img_res1(:,:,i),rect);
end
axes(handles.axes1)
imshow(img_m(:,:,1),colormap(gray(256)))
[r,s]=size(img_m(:,:,1));
all = r*s
for i = 1:length(Name)
C22(i,:) = reshape(img_m(:,:,i),1,all);
end
% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global Name C1 C22 C3 r s all;
clear C3
Ex = exist('C22')
if Ex == 1
for i = 1:length(Name)
Img_res2(:,:,i) = reshape(C22(i,:),r,s);
end
for i = 1:1:length(Name) %Předzpracování pro celou sekvenci
Img_m(:,:,i) = imadjust(Img_res2(:,:,i));
filt_Img(:,:,i) = uint8(medfilt2(Img_m(:,:,i),[3 3])); %Mediánový filtr
Img_d = double(filt_Img(:,:,i)); %převod na double pro další zpracování
Img_mat(:,:,i) = Img_d;
end
for i = 1:length(Name)
C3(i,:) = reshape(Img_mat(:,:,i),1,all);
end
else
for i = 1:length(Name)
Img_res2(:,:,i) = reshape(C1(i,:),r,s);
end
for i = 1:1:length(Name) %Předzpracování pro celou sekvenci
Img_m(:,:,i) = imadjust(Img_res2(:,:,i));
filt_Img(:,:,i) = uint8(medfilt2(Img_m(:,:,i),[3 3])); %Mediánový filtr
Img_d = double(filt_Img(:,:,i)); %převod na double pro další zpracování
Img_mat(:,:,i) = Img_d;
end
for i = 1:length(Name)
C3(i,:) = reshape(Img_mat(:,:,i),1,all);
end
end
axes(handles.axes1);
imshow(filt_Img(:,:,1))

4 Comments

You shouldn't be using globals in the first place. You have the guidata struct available to you if you want to share variables between callbacks. For other functions the interface should be with the input and output arguments.
This thread has a lot helpful links and examples of how to create a GUI.
The question title pretty much sums up everything! Yes, global variables cause a problem. Everywhere, quite often, but definitely in GUIs they are totally un-necessary.
gives details that work in GUIDE GUIs, which is what you appear to be using. I don't think there is an obvious thread to link to about why globals are bad in Matlab, but they are bad in (almost?) all languages so there is a plethora of resources on the web explaining why.
"GUI problem - global variables"
Yes, global variables are definitely a problem in GUIs (as they are in almost all code).
Solution: use better ways of passing data between callbacks.
ok thanks. I hope that it wouldnt be this problem. So i need to rewrite it

Sign in to comment.

Answers (0)

Asked:

on 17 Mar 2020

Commented:

on 17 Mar 2020

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!