Sharing variables between unicontrol function and WindowScrollWheelFcn
Show older comments
Hello,
I wrote a little program to read dicom files and then show them by using scroll mouse function. The program works well. However I would like to open another folder with dicom images and then read it again without closing the program. And here I have a problem because I do not know how to pass variables from unicontrol function to WindowScrollWheelFcn. Below I present my program.
function ct_scans_read
clear all
f = figure('Name','Image diacom file read','Position',[600 300 900 650]);
h = axes('Parent',f,'units','pixels','Position',[50 100 512 512]);
set(f, 'WindowScrollWheelFcn',@figScroll);
text1=uicontrol('Parent',f,'Style','text','String','Path to directory','Position',[100 60 160 25],'BackgroundColor','white','FontSize',12,'FontWeight','bold');
pathr= uicontrol('Parent',f,'Style', 'edit', 'String','C:\CT Images\','Position', [20 30 850 20],'BackgroundColor','white','FontSize',10,'FontWeight','bold');
name_path = get( pathr , 'string' );
dirOutput = dir(name_path);
row = size(dirOutput,1);
for i = 3:row
nname = [name_path dirOutput(i).name];
info = dicominfo(nname);
dicom_images(:,:,i-2) = dicomread(info);
end
h = imshow(dicom_images(:,:,1));
function figScroll(src,callbackdata)
if callbackdata.VerticalScrollCount > 0
if i >= row-2;
i = row-2;
else
i=i+1;
end
h = imshow(imadjust(uint16(dicom_images(:,:,i)),[1923/65535 1924/65535],[]));
elseif callbackdata.VerticalScrollCount < 0
if i <= 1;
i = 1;
else
i=i-1;
end
h = imshow(imadjust(uint16(dicom_images(:,:,i)),[1923/65535 1924/65535],[]));
end
end
end
If I do this way I have an error: Error: File: ct_scans_read.m Line: 39 Column: 17 "i" previously appeared to be used as a function or command, conflicting with its use here as the name of a variable. A possible cause of this error is that you forgot to initialize the variable, or you have initialized it implicitly using load or eval.
function ct_scans_read
clear all
f = figure('Name','Image diacom file read','Position',[600 300 900 650]);
h = axes('Parent',f,'units','pixels','Position',[50 100 512 512]);
set(f, 'WindowScrollWheelFcn',@figScroll);
text1=uicontrol('Parent',f,'Style','text','String','Path to directory','Position',[100 60 160 25],'BackgroundColor','white','FontSize',12,'FontWeight','bold');
pathr= uicontrol('Parent',f,'Style', 'edit', 'String','C:\CT Images\','Position', [20 30 850 20],'BackgroundColor','white','FontSize',10,'FontWeight','bold');
set(pathr, 'callback', {@unicontrol,h})
function f=unicontrol(hObject, eventdata, f)
name_path = get( pathr , 'string' );
dirOutput = dir(name_path);
row = size(dirOutput,1);
for i = 3:row
nname = [name_path dirOutput(i).name];
info = dicominfo(nname);
dicom_images(:,:,i-2) = dicomread(info);
end
h = imshow(dicom_images(:,:,1));
i=row-2;
end
function figScroll(src,callbackdata)
if callbackdata.VerticalScrollCount > 0
if i >= row-2;
i = row-2;
else
i=i+1;
end
h = imshow(imadjust(uint16(dicom_images(:,:,i)),[1923/65535 1924/65535],[]));
elseif callbackdata.VerticalScrollCount < 0
if i <= 1;
i = 1;
else
i=i-1;
end
h = imshow(imadjust(uint16(dicom_images(:,:,i)),[1923/65535 1924/65535],[]));
end
end
end
Any help will be appreciated. Thank you.
4 Comments
Putting
clear all
as the first line of a function is not required. This is just a bad habit that beginners pick up when they don't realize that functions begin with an empty workspace. Avoid cargo cult programming: do not add clear all at the start of your functions.
Even better is to learn to avoid using clear (and friends) in scripts too.
Kamil
on 16 Feb 2016
Gabe
on 8 Aug 2023
Just curious, what seemed to be the problem?
Walter Roberson
on 8 Aug 2023
The user tried to use the variable i as a shared variable in nested functions. However, shared variables need to be defined before a nested function that uses the shared variable. The user needed to initialize i before definining function f=unicontrol(hObject, eventdata, f)
Accepted Answer
More Answers (0)
Categories
Find more on DICOM Format in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!