Sharing variables between unicontrol function and WindowScrollWheelFcn
4 ビュー (過去 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
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)
採用された回答
その他の回答 (0 件)
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!