标签:
校园 |
分类: 学习总结 |
事件:用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
http://www.blinkdagger.com/tutorials/matlab/matlab-icon.jpgLISTBOX 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:
http://www.blinkdagger.com/tutorials/matlab/GUI/advanced/inputGUI/image007.pngLISTBOX
The Example Files and Code
-
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.
-
Now, type
guide
at the command prompt.http://www.blinkdagger.com/tutorials/matlab/GUI/basic/handles-intro/image001.pngLISTBOX
折腾了我两个小时" /> -
Choose to open the sample GUI by clicking on “Open Existing GUI”. Click on “Browse” to locate where you saved the GUI files.
http://www.blinkdagger.com/tutorials/matlab/GUI/basic/handles-intro/image003.pngLISTBOX
折腾了我两个小时" /> -
Here is what the GUI should look like when you open it:
http://www.blinkdagger.com/tutorials/matlab/GUI/advanced/inputGUI/image001.pngLISTBOX
折腾了我两个小时" /> -
Click on the http://www.blinkdagger.com/tutorials/matlab/GUI/basic/handles-intro/image007.pngLISTBOX
折腾了我两个小时" /> icon on the GUI figure to bring up the accompanying .m file. -
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) -
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) -
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
http://www.blinkdagger.com/tutorials/matlab/GUI/advanced/inputGUI/image003.pngLISTBOX
折腾了我两个小时" /> -
Press the Add File(s) button. You should see the following dialog box appear. Select all of the files at once.
http://www.blinkdagger.com/tutorials/matlab/GUI/advanced/inputGUI/image005.pngLISTBOX
折腾了我两个小时" /> -
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.
http://www.blinkdagger.com/tutorials/matlab/GUI/advanced/inputGUI/image007.pngLISTBOX
折腾了我两个小时" /> -
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 -
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.
http://www.blinkdagger.com/tutorials/matlab/GUI/advanced/inputGUI/image009.pngLISTBOX
折腾了我两个小时" />