找回密码
 注册
Simdroid-非首页
查看: 102|回复: 4

GUI的一个传参问题

[复制链接]
发表于 2010-9-25 12:23:49 | 显示全部楼层 |阅读模式 来自 湖南长沙
最近刚学GUI,想做一个图片浏览器。在做的过程中遇到了一个问题
截图如下:

想在点击选择目录后,我设置callback函数获取目录名,然后双击list控件对应的文件名可以打开相应的图片
可是由于我在写打开图片目录的callback函数时是这样写的
  1. function select_index_Callback(hObject, eventdata, handles)
  2. pic_dir=uigetdir('start_path','请选择图片所在的目录');
  3. if pic_dir
  4.     str_all=dir([pic_dir '\*.jpg']);
  5.     str_all=[str_all dir([pic_dir '\*.bmp'])];
  6.     {str_all.name}=[]
  7.     set(handles.pic_list,'string',{str_all.name});
  8. else
  9.     warndlg('打开的目录不能为空','敬告!');
  10. end
复制代码
所以在双击对应文件名后只能够获取文件全名,请问路径pic_dir变量在以下的function里面应该怎样访问啊?
  1. function pic_list_Callback(hObject, eventdata, handles)
  2. if isequal(get(gcf,'SelectionType'),'open')
  3.     str=get(hObject,'string');
  4.     n=get(hObject,'value');
  5.     current_pic=imread(str{n});
  6.     imshow(current_pic);
  7. end
复制代码
发表于 2010-9-25 14:00:42 | 显示全部楼层 来自 河北廊坊
Simdroid开发平台
关于数据传递的方式比较多
1.使用全局变量(global)
2.GUI数据(handles),guidata
3.Application数据
4.UserData属性
下面是我根据你的代码修改后的代码

  1. function my_pushb_2_Callback(hObject, eventdata, handles)
  2. % hObject    handle to my_pushb_2 (see GCBO)
  3. % eventdata  reserved - to be defined in a future version of MATLAB
  4. % handles    structure with handles and user data (see GUIDATA)
  5. pic_dir=uigetdir('start_path','请选择图片所在的目录');
  6. handles.pic_dir=pic_dir;
  7. if pic_dir
  8.     str_all=dir([pic_dir '\*.jpg']);
  9.     str_all=[str_all dir([pic_dir '\*.bmp'])];
  10. %     {str_all.name}=[]
  11.     set(handles.my_list_1,'string',{str_all.name});
  12. else
  13.     warndlg('打开的目录不能为空','敬告!');
  14. end
  15. guidata(hObject,handles);
复制代码

  1. function my_list_1_Callback(hObject, eventdata, handles)
  2. % hObject    handle to my_list_1 (see GCBO)
  3. % eventdata  reserved - to be defined in a future version of MATLAB
  4. % handles    structure with handles and user data (see GUIDATA)
  5. % Hints: contents = get(hObject,'String') returns my_list_1 contents as cell array
  6. %        contents{get(hObject,'Value')} returns selected item from my_list_1
  7. if isequal(get(gcf,'SelectionType'),'open')
  8.     str=get(hObject,'string');
  9.     n=get(hObject,'value');
  10.     current_pic=imread(fullfile(handles.pic_dir,str{n}));
  11.     imshow(current_pic);
  12. end
  13. guidata(hObject,handles);
复制代码

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×

评分

1

查看全部评分

回复 不支持

使用道具 举报

 楼主| 发表于 2010-9-25 15:38:25 | 显示全部楼层 来自 湖南长沙
2# qibbxxt
非常感谢,从改的源码上来看使用的guidata的方法,能不能给出用你说的方法3-4的源码呢?我想学习一下,非常感谢~~
3.Application数据
4.UserData属性
回复 不支持

使用道具 举报

发表于 2010-9-25 16:07:54 | 显示全部楼层 来自 河北廊坊
本帖最后由 qibbxxt 于 2010-9-25 16:12 编辑

3# starbinbin_csu
上面的函数相应部分写成

  1. setappdata(handles.my_pushb_2,'data',pic_dir);
复制代码
下面的相应部分写成

  1. current_pic=imread(fullfile(getappdata(handles.my_pushb_2,'data'),str{n}));
复制代码
具体可以参考一下:
http://forum.simwe.com/viewthread.php?tid=934476
http://xpku.blog.163.com/blog/static/23965002009101365053880/
回复 不支持

使用道具 举报

发表于 2010-9-25 17:09:06 | 显示全部楼层 来自 河北廊坊
3# starbinbin_csu
第4种:
上面的函数相应部分可以改为:

  1. set(gcbf,'UserData',pic_dir);
复制代码

下面的相应函数部分改为:

  1. current_pic=imread(fullfile(get(gcbf,'UserData'),str{n}));
复制代码
回复 不支持

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Simapps系列直播

Archiver|小黑屋|联系我们|仿真互动网 ( 京ICP备15048925号-7 )

GMT+8, 2024-10-6 17:13 , Processed in 0.042654 second(s), 17 queries , Gzip On, MemCache On.

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表