matlab 设置UserData,gui子函数中创建axes对象,主函数中无法读取userdata属性

% --- Executes on button press in LoadPushbutton.

function LoadPushbutton_Callback(hObject, eventdata, handles)

% hObject    handle to LoadPushbutton (see GCBO)

% eventdata  reserved - to be defined in a future version of MATLAB

% handles    structure with handles and user data (see GUIDATA)

global ImageFileSelected FilenameCollection NumLoaded;

% 打开读取文件对话框

[filename,pathname,filterindex]=uigetfile( ...

{'*.jpg;*.bmp;*.tif','Image Files(*.jpg,*.bmp,*.tif)';

'*.jpg','*.jpg';

'*.bmp','*.bmp';

'*.tif','*.tif';

'*.*','All Files(*.*)'},...

'Select images','Multiselect','on');           %对话框读取多幅图像信息

%%%%% 确定所有载入图像的路径集合 %%%%%

if isequal([filename,pathname],[0,0])

return

else

%循环读取图片

if iscell(filename)~=1   %若只选择了一幅图片,则将filename数组转化为元胞数组

filename=mat2cell(filename);

end

num=size(filename,2);

for n=1:num        %若选择了多幅图片,循环读取图片

CurrentFilename=filename{n};

pic = fullfile(pathname,CurrentFilename);  %创建完整的文件名

if NumLoaded==0  % 若尚未打开过图片,无需验证,直接写入路径

NumLoaded=NumLoaded+1;

ImageFileSelected{NumLoaded}=pic;

FilenameCollection{NumLoaded}=CurrentFilename;

else

signal=1;

for k=1:NumLoaded % 判断图片路径是否已写入,若已写入直接跳过,避免重复写入路径

if isequal(ImageFileSelected{k},pic)

signal=0;

break

end

end

if signal==1   %图片未打开,执行后续操作

NumLoaded=NumLoaded+1;

ImageFileSelected{NumLoaded}=pic; %更新已打开图片文件信息元胞数组

FilenameCollection{NumLoaded}=CurrentFilename;

end

end

end

end

%%%%%

%显示图像

handles=ShowImages(handles.figure1,ImageFileSelected, FilenameCollection, NumLoaded,handles);

get(handles.ha,'UserData')  %查看userdata属性值

% Update handles structure

guidata(hObject, handles);

function [vargout]=ShowImages(hObject, FilePath, Filename, NumFile, handles)

% This function aims at showing the selected images

delete(get(handles.panel1,'Children'));  %删除所有当前panel内的图像

if NumFile>0 %若选中有图像则执行后续操作

NumLine=fix(NumFile/3)+1;  %图像显示的行数

NumRow=mod(NumFile,3);     %图像显示的列数

if NumLine>2         %图像显示行数超过两行,则需要更改panel的position属性

xStartPanel1=0;

yStartPanel1=1-0.35*NumLine-0.2;

WidthPanel1=0.8;

HeightPanel1=0.35*NumLine+0.2;

handles.PosPanel1=[xStartPanel1 yStartPanel1 WidthPanel1 HeightPanel1];

set(handles.panel1,'position',handles.PosPanel1);

for i=1:NumFile     %逐个显示图像

line=fix((i-1)/3)+1;

row=mod(i,3);

switch row

case 1

xStartAxes=0.01;

case 2

xStartAxes=0.32;

case 0

xStartAxes=0.63;

end

yStartAxes=(0.65-0.35*(line-1)+HeightPanel1-1)/HeightPanel1;%0.65-0.35*line为更改panel的position属性前的axes起点坐标

WidthAxes=0.3;

HeightAxes=0.3/HeightPanel1;

PosAxes=[xStartAxes yStartAxes WidthAxes HeightAxes];

handles.ha(i)=axes('Parent',handles.panel1,'Position',PosAxes,...

'Units','normalized');

set(handles.ha(i),'UserData',0);

imshow(FilePath{i});

title(Filename{i});

end

else

for i=1:NumFile     %逐个显示图像

line=fix((i-1)/3)+1;

row=mod(i,3);

switch row

case 1

xStartAxes=0.01;

case 2

xStartAxes=0.32;

case 0

xStartAxes=0.63;

end

yStartAxes=0.65-(0.35*(line-1));

WidthAxes=0.3;

HeightAxes=0.3;

PosAxes=[xStartAxes yStartAxes WidthAxes HeightAxes];

handles.ha(i)=axes('Parent',handles.panel1,'Position',PosAxes,...

'Units','normalized');

set(handles.ha(i),'UserData',0);

imshow(FilePath{i});

title(Filename{i});

end

end

end

vargout=handles;

guidata(hObject, handles);

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值