|
|
MATLAB GUI中读取txt文件数据按钮回调函数0 l) o2 c4 W' Z
function ReadData_Callback(hObject, eventdata, handles): |) z$ L9 W+ `! H* P
% hObject handle to ReadData (see GCBO)
9 f* [4 |6 P" I3 ~1 J$ \ y% eventdata reserved - to be defined in a future version of MATLAB
( [4 S2 K f' @% handles structure with handles and user data (see GUIDATA)
H# r! G& X% k2 [" w. M4 n[file path]=uigetfile('*.txt','Choose a File');%读取文件,弹出对话框,提示选择文件
1 a5 v' Q$ U- Q* v- N& U+ J) Eif file==0
! D+ j) J& o7 M$ c1 W warndlg('用户必须选择一个txt文件');7 I" O& g+ R' s' N
else
* n- z& t. N: h$ H# p' \9 }( v4 o [x0,y0]=textread(fullfile(path,file),'%n%n','headerlines',6);%省略txt文件中前6行,只读取数字,共两列数据' z, B; x& z8 ^4 W4 \9 f# u
handles.x0=x0;%将读取的第一列数据x0放到 handles结构体中,以便其他函数可以直接调用
+ E" I$ K/ e7 Q. l handles.y0=y0;%将读取的第二列数据y0放到 handles结构体中,同上
0 @% Y7 e5 X' m5 A8 p plot(handles.axes1,x0,y0);%通过handles.axes1访问绘图区域,并将数据绘制在绘图区
0 U2 K9 M: K! ?) b- [end
. s3 O# U; K- _* L& a% Update handles structure
9 I" \5 R, h4 n7 t, Y; B' Iguidata(hObject, handles);
" }! U t9 m: e%通过handles.axes1访问绘图区域 X, p7 s& l- j
%axes1对象属性中,nextplot为replace时,每次绘图均覆盖,为add时,就可以叠加了
4 A' A1 t6 B- i" r% [: g5 Y" E+ T# G- u! }
不知道这个是不是你想要的。9 V) x4 H0 ]! q6 q4 M+ v
|
|