Sharing variables between unicontrol function and WindowScrollWheelFcn

3 次查看(过去 30 天)
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 个评论
Walter Roberson
Walter Roberson 2023-8-8
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)

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2016-2-16

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Scripts 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by