|
MATLAB GUI中读取txt文件数据按钮回调函数9 M# l+ q! c0 U: _
function ReadData_Callback(hObject, eventdata, handles)
( i" D# `7 l8 M0 v% @% hObject handle to ReadData (see GCBO)# r8 \4 c8 }5 k! i ~
% eventdata reserved - to be defined in a future version of MATLAB
. G# V: u, q0 x* v6 O% handles structure with handles and user data (see GUIDATA)
0 n* v# m. N. V2 I0 z- b1 V[file path]=uigetfile('*.txt','Choose a File');%读取文件,弹出对话框,提示选择文件, W/ ?; `+ H# D6 [5 E* w
if file==0
5 n1 t/ H% r8 R; i warndlg('用户必须选择一个txt文件');
9 i, x' h: D* [: Selse2 x) b. P5 M+ T
[x0,y0]=textread(fullfile(path,file),'%n%n','headerlines',6);%省略txt文件中前6行,只读取数字,共两列数据
& c [6 S) s. a handles.x0=x0;%将读取的第一列数据x0放到 handles结构体中,以便其他函数可以直接调用
3 D3 n5 g v- I4 J' P handles.y0=y0;%将读取的第二列数据y0放到 handles结构体中,同上# t9 e$ W) j' R" X) B
plot(handles.axes1,x0,y0);%通过handles.axes1访问绘图区域,并将数据绘制在绘图区
% r( z0 U! p$ V/ M E% ^: pend
: D5 j. W5 T7 m; x$ m: S% Update handles structure
& u1 W' N' W: ~( nguidata(hObject, handles);, x( ~2 h4 ?2 u+ y, o
%通过handles.axes1访问绘图区域
+ y& z. ^& O- p" a6 ]8 ?4 T%axes1对象属性中,nextplot为replace时,每次绘图均覆盖,为add时,就可以叠加了
1 c2 r# V9 o' h8 [1 p
$ L" D, Y# S; t _4 J不知道这个是不是你想要的。" M9 r9 R2 P9 U
|
|