最近一直在研究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界面:
打包时把MCR也打包进去,使用的是matlab R2011b进行的打包,方便未安装matlab的计算机使用:
完整工程文件链接:
格式转换