加载中…
个人资料
古月星毅
古月星毅
  • 博客等级:
  • 博客积分:0
  • 博客访问:15,248
  • 关注人气:3
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
相关博文
推荐博文
谁看过这篇博文
加载中…
正文 字体大小:

MATLAB LISTBOX 折腾了我两个小时

(2008-05-02 20:54:03)
标签:

校园

分类: 学习总结

事件:用GUI做K-Means聚类算法

目的:试图将每次迭代过程中各类的类心在listbox 中显示

以前的思维:在VB或VC中都可以很方便的用add方法

现状:Matlab中不支持这样的处理方式,matlab的思维是对于每一个unicontrol通过get,set来获得和设置相应的属性,当然在获取过程中要牵涉到句柄,这个很简单,不值得一说。

我首先查看了Listbox的所有属性,理所当然地会用string属性,关键是怎么更改属性,我最初的想法是将要显示的内容存放在一个字符数组里,为了不覆盖前面的内容,我这样做:

for ...

centerstr=[];

centerstr=[centerstr,newcontext];

set(handles.center_listbox,'string',centerstr);

end

但一般有一个问题很难预料到,就是centerstr和newcontext的维数匹配问题,我就是受这个问题所困扰。

显然,类心得坐标是double型的,在为数值型时,保留到小数点后面4位,但在num2str后之后,他把double型最后的零扔掉了,这就造成centerstr和newcontext的维数不匹配。我起初想到用if 语句来判断,谁的维数低了我把他补零,但在迭代过程中,这种现象是随机的,所以我费了很大力气,还是无功而返。

这时候,我想到了internet,打开google,输入matlab类型转换,企图找到曾经碰到相同问题的通知,可还是。。。我就到matlab官方网站上,看有没有提问去,在那儿问问,自然想到了问题得用英语写,所以我又在google 里typed "how to add items to listbox in matlab",这下找到了救星,就是matlab里的帮助。问题终于顺利解决。

具体实现很简单:(当时看到了想吐血)

str=get(handles.center_listbox,'string');

str{end+1}=newcontext;

set(handles.center_listbox,'string',str);

下面是matlab里的example:

Introduction

Matlab Logo In many applications, the user must specify the input. In this tutorial, you will learn how to use the listbox component and the uigetfile command to create an attractive input interface. Specifically, you will learn how to create a GUI that will allow the user to select multiple files and display all the file names inside the listbox. I’ve done a similar example before in the basic processing tool, but I would like to go over this portion in a little more detail. Here’s a picture of the GUI:

GUI

The Example Files and Code

  1. First, download the GUI skeleton here. Unzip the files and place them wherever you please. I also included 4 sample excel files that you can use to test the GUI.

  2. Now, type guide at the command prompt.

    Command prompt

  3. Choose to open the sample GUI by clicking on “Open Existing GUI”. Click on “Browse” to locate where you saved the GUI files.

    GUIDE Screen

  4. Here is what the GUI should look like when you open it:

    GUI Figure

  5. Click on the mfile icon icon on the GUI figure to bring up the accompanying .m file.

  6. Now, add the following code to addFile_pushbutton_Callback. As the name suggests, this is the callback that will add the files to the listbox. For this tutorial, the only file type that I allowed to be added were excel files (.xls).

     
    %gets input file(s) from user
    [input_file,pathname] = uigetfile( ...
           {'*.xls', 'Excel (*.xls)'; ...
            '*.*', 'All Files (*.*)'}, ...
            'Select files', ... 
            'MultiSelect', 'on');
     
    %if file selection is cancelled, pathname should be zero
    %and nothing should happen
    if pathname == 0
        return
    end
     
    %gets the current data file names inside the listbox
    inputFileNames = get(handles.inputFiles_listbox,'String');
     
    %if they only select one file, then the data will not be a cell
    %if more than one file selected at once,
    %then the data is stored inside a cell
    if iscell(input_file) == 0
     
        %add the most recent data file selected to the cell containing
        %all the data file names
        inputFileNames{end+1} = fullfile(pathname,input_file);
     
    %else, data will be in cell format
    else
        %stores full file path into inputFileNames
        for n = 1:length(input_file)
            %notice the use of {}, because we are dealing with a cell here!
            inputFileNames{end+1} = fullfile(pathname,input_file{n});
        end
    end
     
    %updates the gui to display all filenames in the listbox
    set(handles.inputFiles_listbox,'String',inputFileNames);
     
    %make sure first file is always selected so it doesn't go out of range
    %the GUI will break if this value is out of range
    set(handles.inputFiles_listbox,'Value',1);
     
    % Update handles structure
    guidata(hObject, handles);
    
  7. Next, we need to add the following code to the deleteFile_pushbutton_Callback. This code will allow you to delete any files you may have accidentally added into the listbox.

    %get the current list of file names from the listbox
    inputFileNames = get(handles.inputFiles_listbox,'String');
     
    %get the values for the selected file names
    option = get(handles.inputFiles_listbox,'Value');
     
    %is there is nothing to delete, nothing happens
    if (isempty(option) == 1 || option(1) == 0 || isempty(inputFileNames))
        return
    end
     
    %erases the contents of highlighted item in data array
    inputFileNames(option) = [];
     
    %updates the gui, erasing the selected item from the listbox
    set(handles.inputFiles_listbox,'String',inputFileNames);
     
    %moves the highlighted item to an appropiate value or else will get error
    if option(end) > length(inputFileNames)
        set(handles.inputFiles_listbox,'Value',length(inputFileNames));
    end
     
    % Update handles structure
    guidata(hObject, handles);
    
  8. Before we add any code to the final pushbutton, let’s make sure that what we have done so far works. Now, save your .m file and run the GUI. You should see the following GUI appear

    GUI

  9. Press the Add File(s) button. You should see the following dialog box appear. Select all of the files at once.

    Select Files

  10. Now you should see the following on your GUI. The selected files will appear in the listbox! Try using the delete pushbutton to delete some of the files you selected.

    Select Files

  11. Now that you can select the input files, it might be a good idea if we actually did something with them. So let’s add some code that will read the data! Go back to the m-file, and add the following code to the readData_pushbutton_Callback.

    %get the current list of file names from the listbox
    inputFileNames = get(handles.inputFiles_listbox,'String');
     
    for x=1:length(inputFileNames)
        %display the file name on the command prompt
        inputFileNames{x}
        %display the excel data on the display prompt
        xlsread(inputFileNames{x})
    end
    
  12. Now, run your GUI again, add the excel files, and press the read data button. You should see the output on the Matlab command prompt! I showed some of the output here in the following picture.

    Select Files

0

阅读 评论 收藏 转载 喜欢 打印举报/Report
  • 评论加载中,请稍候...
发评论

    发评论

    以上网友发言只代表其个人观点,不代表新浪网的观点或立场。

      

    新浪BLOG意见反馈留言板 电话:4000520066 提示音后按1键(按当地市话标准计费) 欢迎批评指正

    新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 会员注册 | 产品答疑

    新浪公司 版权所有