我建立了一个用子图显示多个图像的gui,但是有很多图像你必须用子图制作更多的轴,因此图像变得更小,所以我想将滑块链接到子图生成的轴,所以我可以保持图像的大小,并在它们之间向下滚动,现在有我的代码:
function pushbutton1_Callback(hObject, eventdata, handles)
[filename pathname] = uigetfile({'*.*'},'File Selector','MultiSelect', 'on')
iscellstr(filename) celldata1 = cellstr(pathname) celldata2 =
cellstr(filename) celldata3 = strcat(celldata1,celldata2)
subplot(3,3,1),imshow(celldata3{1})
subplot(3,3,2),imshow(celldata3{2})
subplot(3,3,3),imshow(celldata3{3})
subplot(3,3,4),imshow(celldata3{4})
subplot(3,3,5),imshow(celldata3{5})
和gui看起来像这样:http://img.prntscr.com/img?url=http://i.imgur.com/Vt56E6Y.jpg
PS:我想使用带滑块的子图,这样我就可以看到所有图像,而我每次用静态轴向下滚动都不会被替换。
答案 0 :(得分:3)
你应该正确地格式化代码而不是使用blockquotes,这使得很难快速阅读和理解代码。
要使用滑块一次性更改多个图像,您需要根据上传的图像设置您似乎已经完成的gui。
首先要做的是更改一些滑块属性。最简单的方法是双击指南图中的滑块并更改以下值:
以下代码背后的基本思想是当您移动滑块时,根据位置生成一个数字,然后该数字用于搜索图像并显示正确的数字。
按钮调用uigetfile函数,允许选择多个文件,就像在问题中一样。它产生两个变量文件名和路径名。文件名是一个单元格,其中包含您选择的所有文件的名称。您将使用基于滑块位置的数字来决定要显示的图像。
以下代码用于按钮。
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
%global variables allow sharing of variables between functions
global filename pathname k1 SliderMax SliderMin
%multi-file selection
[filename, pathname, ~] = uigetfile({ '*.jpg'}, 'Pick files',...
'MultiSelect', 'on');
%determine the maximum value the slider can be. this value is based on the
%number of files selected
%the min value will be set to 1
SliderMax = length(filename)
SliderMin = 1;
set(handles.slider1,'Max',SliderMax);
set(handles.slider1,'Min',SliderMin);
%initialise k1 (slider position) since 4 images will be shown k1 needs to
%be incremented so you show
%4 different images
k1 = 1;
k2 = k1 + 1;
k3 = k1 + 2;
k4 = k1 + 3;
%generating of strings containing the full path to the that will be
%displayed.
img1 = strcat(pathname, filename{k1});
img2 = strcat(pathname, filename{k2});
img3 = strcat(pathname, filename{k3});
img4 = strcat(pathname, filename{k4});
%assign an axes handle to precede imshow and use imshow to display the
%images
% in individual axes.
axes(handles.axes1);
imshow(img1);
axes(handles.axes6);
imshow(img2);
axes(handles.axes7);
imshow(img3);
axes(handles.axes8);
imshow(img4);
以下代码适用于滑块
% --- Executes on slider movement.
function slider1_Callback(hObject, eventdata, handles)
global filename pathname SliderMax SliderMin
%put slider postion value into variable k1
k1 = get(handles.slider1,'Value');
%initialise k4 so that it can be used in the if statement for the check.
k4 = k1 + 3;
%the if statement below is a check to make sure the value of k4 is never
%larger than the total number of images, without some sort of check then
%k4 will exceed the matrix dimensions and give an error.
if k4 > SliderMax
k1 = SliderMax - 3;
k2 = SliderMax - 2;
k3 = SliderMax - 1;
k4 = SliderMax;
else
k1 = floor(k1);
k2 = k1 + 1;
k3 = k1 + 2;
k4 = k1 + 3;
end
%generating of strings containing the full path to the that will be
%displayed.
img1 = strcat(pathname, filename{k1});
img2 = strcat(pathname, filename{k2});
img3 = strcat(pathname, filename{k3});
img4 = strcat(pathname, filename{k4});
%assign an axes handle to precede imshow and use imshow to display the
%images in individual axes.
axes(handles.axes1);
imshow(img1);
axes(handles.axes6);
imshow(img2);
axes(handles.axes7);
imshow(img3);
axes(handles.axes8);
imshow(img4);