|
MATLAB GUI中读取txt文件数据按钮回调函数
* W+ y5 h. e" C* Z# T, R7 Qfunction ReadData_Callback(hObject, eventdata, handles)" q- q( I! m- k$ b) I; z( G
% hObject handle to ReadData (see GCBO)
% s- e5 j" C" G: P% eventdata reserved - to be defined in a future version of MATLAB; f4 R! h2 U5 o j0 L
% handles structure with handles and user data (see GUIDATA)
' h5 c' c& Q2 Q" c6 n- N6 d4 A- \[file path]=uigetfile('*.txt','Choose a File');%读取文件,弹出对话框,提示选择文件. u: ~" T, Z5 o
if file==0+ `, q, G6 v$ q0 R
warndlg('用户必须选择一个txt文件');
1 V9 ~. T& H0 {/ g7 U1 V! lelse
3 D4 ?. A) p* M: g* R [x0,y0]=textread(fullfile(path,file),'%n%n','headerlines',6);%省略txt文件中前6行,只读取数字,共两列数据
: @1 Y0 J$ T4 ]8 e/ ] handles.x0=x0;%将读取的第一列数据x0放到 handles结构体中,以便其他函数可以直接调用
& b) d& U2 U. k) ~: W handles.y0=y0;%将读取的第二列数据y0放到 handles结构体中,同上; c. t% X' [& w
plot(handles.axes1,x0,y0);%通过handles.axes1访问绘图区域,并将数据绘制在绘图区
2 B8 R& H3 V1 p8 i# Y- Y3 mend( A' p0 E' X, v. }" I
% Update handles structure
% E3 s( M( j6 K8 T+ n! fguidata(hObject, handles);% h; {* n8 }' D( r* F; i; }$ Q% K
%通过handles.axes1访问绘图区域% I; a T/ |: L4 l6 P
%axes1对象属性中,nextplot为replace时,每次绘图均覆盖,为add时,就可以叠加了& E+ o# C) Q' h5 Y; }+ Q9 ^
* e& L: X! W0 i: G不知道这个是不是你想要的。1 w" m/ _6 A2 A* `1 M" l
|
|