|
|
MATLAB GUI中读取txt文件数据按钮回调函数
! N. B4 C% G; T; a) cfunction ReadData_Callback(hObject, eventdata, handles)
- E. M" X0 J5 y. A; ]% hObject handle to ReadData (see GCBO)6 [2 q" k8 a( t( L. l, z
% eventdata reserved - to be defined in a future version of MATLAB( C* t- v) k' j! T+ H6 S' d
% handles structure with handles and user data (see GUIDATA)# R& W) q/ K- |- s! [2 G- Y2 E
[file path]=uigetfile('*.txt','Choose a File');%读取文件,弹出对话框,提示选择文件 P4 N! {# `3 g& U. l8 |
if file==05 n8 i, Z; `2 u* e) N
warndlg('用户必须选择一个txt文件');0 Y: ]0 \2 \3 H5 g# D, N
else" i, f1 e+ R; X& {0 G% c
[x0,y0]=textread(fullfile(path,file),'%n%n','headerlines',6);%省略txt文件中前6行,只读取数字,共两列数据2 w& m+ {3 C# |/ x, X$ S H' p! Y
handles.x0=x0;%将读取的第一列数据x0放到 handles结构体中,以便其他函数可以直接调用
; R# X1 F8 H" J6 {& Z handles.y0=y0;%将读取的第二列数据y0放到 handles结构体中,同上
; W! H+ t6 e6 ?+ ]4 m$ u2 u plot(handles.axes1,x0,y0);%通过handles.axes1访问绘图区域,并将数据绘制在绘图区8 |$ p/ u0 {/ G& l
end
5 J: y" F! F' K# B, S" U% Update handles structure& ^9 c7 c' T" V" ~% Z
guidata(hObject, handles);- l6 o0 I0 U: g' L9 G% i
%通过handles.axes1访问绘图区域- E4 t% y& s# T$ y# B, I
%axes1对象属性中,nextplot为replace时,每次绘图均覆盖,为add时,就可以叠加了9 d3 L8 _6 N/ p* Y8 ]5 J
! \ v, [! Q7 N$ k8 B4 o不知道这个是不是你想要的。 j! ]; F. H2 a& O
|
|