各位大侠,帮帮忙
function varargout = sheji(varargin)% SHEJI Application M-file for sheji.fig
% FIG = SHEJI launch sheji GUI.
% SHEJI('callback_name', ...) invoke the named callback.
% Last Modified by GUIDE v2.0 20-May-2004 13:14:38
if nargin == 0 % LAUNCH GUI
fig = openfig(mfilename,'reuse');
% Use system color scheme for figure:
set(fig,'Color',get(0,'defaultUicontrolBackgroundColor'));
% Generate a structure of handles to pass to callbacks, and store it.
handles = guihandles(fig);
guidata(fig, handles);
if nargout > 0
varargout{1} = fig;
end
elseif ischar(varargin{1}) % INVOKE NAMED SUBFUNCTION OR CALLBACK
try
if (nargout)
[varargout{1:nargout}] = feval(varargin{:}); % FEVAL switchyard
else
feval(varargin{:}); % FEVAL switchyard
end
catch
disp(lasterr);
end
end
%| ABOUT CALLBACKS:
%| GUIDE automatically appends subfunction prototypes to this file, and
%| sets objects' callback properties to call them through the FEVAL
%| switchyard above. This comment describes that mechanism.
%|
%| Each callback subfunction declaration has the following form:
%| <SUBFUNCTION_NAME>(H, EVENTDATA, HANDLES, VARARGIN)
%|
%| The subfunction name is composed using the object's Tag and the
%| callback type separated by '_', e.g. 'slider2_Callback',
%| 'figure1_CloseRequestFcn', 'axis1_ButtondownFcn'.
%|
%| H is the callback object's handle (obtained using GCBO).
%|
%| EVENTDATA is empty, but reserved for future use.
%|
%| HANDLES is a structure containing handles of components in GUI using
%| tags as fieldnames, e.g. handles.figure1, handles.slider2. This
%| structure is created at GUI startup using GUIHANDLES and stored in
%| the figure's application data using GUIDATA. A copy of the structure
%| is passed to each callback. You can store additional information in
%| this structure at GUI startup, and you can change the structure
%| during callbacks. Call guidata(h, handles) after changing your
%| copy to replace the stored original so that subsequent callbacks see
%| the updates. Type "help guihandles" and "help guidata" for more
%| information.
%|
%| VARARGIN contains any extra arguments you have passed to the
%| callback. Specify the extra arguments by editing the callback
%| property in the inspector. By default, GUIDE sets the property to:
%| <MFILENAME>('<SUBFUNCTION_NAME>', gcbo, [], guidata(gcbo))
%| Add any extra arguments after the last argument, before the final
%| closing parenthesis.
% --------------------------------------------------------------------
function varargout = edit1_Callback(h, eventdata, handles, varargin)
% --------------------------------------------------------------------
function varargout = button_gatherdata_Callback(h, eventdata, handles, varargin)
display('进入函数');
global value;
global bb;
% 初始化串口
s1 = serial('com2');
set(s1,'parity','even');
set(s1,'Timeout',5);
s1.BaudRate = 9600;
%s1.InputBufferSize = 66537;
fopen(s1);
if bb == 0
bb = 1;
value = zeros(100, 1);
fwrite(s1, 128);
% 从串口读数据
display('开始读数据');
value = fread(s1, 100)
display('读数据完成');
fwrite(s1, 255);
% 保存数据
fid1 = fopen('d:\test1.dat','w+'); % Save binary data
if fid1 ~= 0
%count = fwrite(fid1, value);
%for i = 1:100
% fprintf(fid1, '%d,', value(i));
%end
fwrite(fid1, value);
end
fclose(fid1);
else
bb = 0;
end
fclose(s1);
%delete(s1);
%clear s1;
display('退出函数');
set(handles.edit1,'String','采集完成!稍候...');
% --------------------------------------------------------------------
function varargout = button_plotdata_Callback(h, eventdata, handles, varargin)
set(handles.edit1,'string','数据绘图操作.');
off = [handles.button_gatherdata,handles.button_savedata,handles.button_loaddata];
count = 100;
fid1 = fopen('d:\test1.dat', 'r');
value = zeros(count, 1)
if (fid1 ~= 0)
%for i = 1:count
% value(1, i) = fscanf(fid1,'%d,', 1);
%end
value = fread(fid1, count);
display(value);
fftValue = fft(value)
figure(1);
plot(1:size(value), value);
grid on
figure(2)
plot(1:size(fftValue),fftValue);
grid on
end
msgbox(['数据绘图完成!'],'Message','warn','modal');
set(handles.edit1,'string','数据绘图完成!');
end
% --------------------------------------------------------------------
function varargout = button_savedata_Callback(h, eventdata, handles, varargin)
set(handles.edit1,'string','文件存盘.');
off = [handles.button_plotdata,handles.button_gatherdata,handles.button_loaddata];
set(off,'value',0); % mutually exclude
set(handles.button_savedata,'value',1);
names = fieldnames(handles); % Check handles.ts exists or not;if not,no data available
%if(~isempty(find(strcmp(names,'ts')))) % First time for gathering data or not? if so, set flag to 1
[filename, pathname]=uiputfile('*.dat','Save as...');
if(pathname ~= 0)
% 保存数据
copyfile('d:\test1.dat', strcat(pathname, filename, '.dat'));
if (0 == 1)
fid1 = fopen(strcat(pathname, filename, '.dat'), 'w+');
if fid1 ~= 0
%count = fwrite(fid1, value);
%for i = 1:100
% fprintf(fid1, '%d,', value(i));
%end
abc = value;
fwrite(fid1, abc);
end
fclose(fid1);
end
end
% --------------------------------------------------------------------
function varargout = button_loaddata_Callback(h, eventdata, handles, varargin)
set(handles.edit1,'string','打开文件.');
off=[handles.button_plotdata,handles.button_savedata,handles.button_gatherdata];
set(off,'value',0);
set(handles.button_loaddata,'value',1);
global value;
names=fieldnames(handles);
if(isempty(find(strcmp(names,'savestatus'))))
handles.savestatus=1;
end
if(handles.savestatus==1)
[filename,pathname]=uigetfile('*.dat','load file...');
if(pathname~=0)
if(~isempty(strfind(filename,'.dat')))
fid1=fopen(strcat(pathname,filename),'rb');
count = 100;
value = zeros(count, 1)
if (fid1 ~= 0)
%for i = 1:count
% value(1, i) = fscanf(fid1,'%d,', 1);
%end
value = fread(fid1, count);
display(value);
fftValue = fft(value)
figure(1);
plot(1:size(value), value);
grid on
figure(2)
plot(1:size(fftValue),fftValue);
grid on
end
end
end
end
% --------------------------------------------------------------------
function varargout = file_Callback(h, eventdata, handles, varargin)
% --------------------------------------------------------------------
function varargout = button_close_Callback(h, eventdata, handles, varargin)
close
谁能帮我把这个程序改一下,使它能够运行.我将万分感谢.改完请发至我的邮箱:rfg0401@