1. 程式人生 > >CSV格式轉成libsvm標準格式的小程式

CSV格式轉成libsvm標準格式的小程式

最近一直在研究LibSVM,其中csv格式到libsvm標準格式的轉換,使用的是matlab中的幾行程式碼實現的,也蠻好用的,但由於之後想做一個面向物件和SVM結合的建築物自動提取的程式,主程式初步打算用C#實現,這樣就涉及格式轉換程式碼整合到C#的問題,由於C#調libsvm的介面不太會用,想用matlab的GUI直接實現格式轉換的exe,然後再用C#直接呼叫exe程式,初次接觸matlab的GUI,感覺和C#差不多,程式也沒什麼功能,直接上程式碼:

function varargout = csv_lib(varargin)
% CSV_LIB MATLAB code for csv_lib.fig
%      CSV_LIB, by
itself, creates a new CSV_LIB or raises the existing % singleton*. % % H = CSV_LIB returns the handle to a new CSV_LIB or the handle to % the existing singleton*. % % CSV_LIB('CALLBACK',hObject,eventData,handles,...) calls the local % function named CALLBACK in CSV_LIB.M with
the given input arguments. % % CSV_LIB('Property','Value',...) creates a new CSV_LIB or raises the % existing singleton*. Starting from the left, property value pairs are % applied to the GUI before csv_lib_OpeningFcn gets called. An % unrecognized property name or invalid value makes property
application % stop. All inputs are passed to csv_lib_OpeningFcn via varargin. % % *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one % instance to run (singleton)". % % See also: GUIDE, GUIDATA, GUIHANDLES % Edit the above text to modify the response to help csv_lib % Last Modified by GUIDE v2.5 14-Jul-2015 16:29:47 % Begin initialization code - DO NOT EDIT gui_Singleton = 1; gui_State = struct('gui_Name', mfilename, ... 'gui_Singleton', gui_Singleton, ... 'gui_OpeningFcn', @csv_lib_OpeningFcn, ... 'gui_OutputFcn', @csv_lib_OutputFcn, ... 'gui_LayoutFcn', [] , ... 'gui_Callback', []); if nargin && ischar(varargin{1}) gui_State.gui_Callback = str2func(varargin{1}); end if nargout [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:}); else gui_mainfcn(gui_State, varargin{:}); end % End initialization code - DO NOT EDIT % --- Executes just before csv_lib is made visible. function csv_lib_OpeningFcn(hObject, eventdata, handles, varargin) % This function has no output args, see OutputFcn. % hObject handle to figure % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % varargin command line arguments to csv_lib (see VARARGIN) % Choose default command line output for csv_lib handles.output = hObject; % Update handles structure guidata(hObject, handles); % UIWAIT makes csv_lib wait for user response (see UIRESUME) % uiwait(handles.figure1); % --- Outputs from this function are returned to the command line. function varargout = csv_lib_OutputFcn(hObject, eventdata, handles) % varargout cell array for returning output args (see VARARGOUT); % hObject handle to figure % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Get default command line output from handles structure varargout{1} = handles.output; % --- Executes on button press in OpenCSVFile. function OpenCSVFile_Callback(hObject, eventdata, handles) % hObject handle to OpenCSVFile (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) global strFullPath global FileName [FileName,PathName]=uigetfile('*.csv','開啟csv檔案','MultiSelect','off') strFullPath=[PathName FileName]; % --- Executes on button press in ExeTransform. function ExeTransform_Callback(hObject, eventdata, handles) % hObject handle to ExeTransform (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) global strFullPath global labels global features_sparse csvRead=csvread(strFullPath); labels = csvRead(:, 1); % labels from the 1st column features = csvRead(:, 2:end); features_sparse = sparse(features); % features must be in a sparse matrix tip=msgbox('轉換完成!','提示') % --- Executes on button press in SavelibsvmFormat. function SavelibsvmFormat_Callback(hObject, eventdata, handles) % hObject handle to SavelibsvmFormat (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) global FileName global labels global features_sparse FileNameWithoutExtent= FileName(1:(find(FileName=='.')-1)) [outFileName,outPathName]=uiputfile({'*.*'},'另存為',FileNameWithoutExtent) outFullPath=[outPathName outFileName]; libsvmwrite(outFullPath, labels, features_sparse);

GUI介面:
GUI介面
打包時把MCR也打包進去,使用的是matlab R2011b進行的打包,方便未安裝matlab的計算機使用:
這裡寫圖片描述
完整工程檔案連結:
格式轉換