在MATLAB中创建一个独立的exe来比较文件

时间:2015-12-14 17:11:36

标签: matlab comparison binaryfiles matlab-deployment matlab-compiler

我正在尝试创建一个独立的可执行文件,在Windows系统上运行,比较2个txt文件。我一直试图从MATLAB做到这一点。我喜欢visdiff工具在MATLAB中的工作方式,但是当我在MATLAB中使用Application编译器编译文件并运行程序时,visdiff工具不会出现。使用visdiff工具不是必需的,但它是一个很好的预建工具。

这是我尝试使用的代码。它是使用MATLAB的“GUIDE”工具创建的。然后我尝试在MATLAB R2015b中单击功能区中的“APPS”,然后单击“APPLICATION COMPILER”。

function varargout = Comparator(varargin)
% COMPARATOR MATLAB code for Comparator.fig
%      COMPARATOR, by itself, creates a new COMPARATOR or raises the         existing
%      singleton*.
%
%      H = COMPARATOR returns the handle to a new COMPARATOR or the handle to
%      the existing singleton*.
%
%      COMPARATOR('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in COMPARATOR.M with the given input arguments.
%
%      COMPARATOR('Property','Value',...) creates a new COMPARATOR or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before Comparator_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to Comparator_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 Comparator

% Last Modified by GUIDE v2.5 11-Dec-2015 13:20:24

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @Comparator_OpeningFcn, ...
                   'gui_OutputFcn',  @Comparator_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 Comparator is made visible.
function Comparator_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 Comparator (see VARARGIN)

% Choose default command line output for Comparator
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes Comparator wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = Comparator_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 pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[FName, PathName, FilterIndex] = uigetfile('*.txt')
% set(hOject,'String',FName)%%%%%%%%%%CHANGE THIS!!!!!!!!!
handles.fid1=FName;
guidata(hObject,handles)


% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[FName, PathName, FilterIndex] = uigetfile('*.txt')
% set(hOject,'String',FName)%%%%%%%%%%CHANGE THIS!!!!!!!!!
handles.fid2=FName;
guidata(hObject,handles)


% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
visdiff(handles.fid1,handles.fid2)

我也在玩从头开始创建一个如下:

clc;clear;
fid1 = fopen('t1.txt', 'r');
fid2 = fopen('t2.txt', 'r');

lines1 = textscan(fid1,'%s','delimiter','\n');
lines2 = textscan(fid2,'%s','delimiter','\n');
lines1 = lines1{1};
lines2 = lines2{1};

fclose(fid1);
fclose(fid2);

[idx1 idx2] = ismember(lines1,lines2);

有关如何使visdiff工具独立工作或创建新工具的任何建议都将不胜感激。

2 个答案:

答案 0 :(得分:2)

我怀疑visdiff属于类别MATLAB Functions That Cannot Be Compiled,因为它会启动(或可以显示)用户界面。一般来说,MATLAB及其工具箱中内置的所有GUI都不能与MATLAB编译器一起部署。当然,请检查mccExcludedFiles.log以查找在编译过程中排除的文件。

您可能需要使用“从头开始”的方法,就像您在问题中提到的那样。

答案 1 :(得分:1)

我已输入您的程序并从命令行运行并编译。我也为编译版本收到此错误消息:

Undefined variable "comparisons" or class "comparisons.internal.resolvePath".
Error in comparisons_private>i_compareFiles (line 92)
Error in comparisons_private (line 22)
Error in cf>pushbutton3_Callback (line 101)

我认为这是试图告诉我们比较工具在编译代码中不可用。

当您的代码在MATLAB中运行时,它打开的窗口看起来非常像this page所说的编辑器无法使用的编辑器。

注意,我用r2014a和r2015b进行了实验,结果是一样的。

我从windows命令行启动了编译的.exe以查看错误输出。

为什么不试试winmerge

编辑:

WinMerge只是文本,但是你谷歌它,人们会解释如何使用它来找到二进制文件的第一个区别。文件大小限制为> 2Gytes。

UltraEdit/UltraCompare具有二进制文件的比较模式,许多更强大的程序员使用十六进制编辑模式编辑文本编辑器。

假设您使用的是Windows,如果您有一个bash shell,例如cygwinmingw(默认安装中不是git bash),您可以将二进制文件转储到文本然后使用任何文本比较工具,如WinMerge。搜索hexdump或od。遗憾的是,Microsoft尚未将hex转储添加到电源外壳或命令窗口,但您可以下载herehere的工具。十六进制转储实用程序是Unix / Linux上的标准。

或者您可以编写一个MATLAB程序来将二进制文件转储为最适合您的文本,并使用WinMerge比较文本等价物。

此问题Tool for comparing 2 binary files in Windows可能有比我更好的建议。