|
|
MATLAB GUI中读取txt文件数据按钮回调函数
9 V) c/ w+ W- `. A! H3 D9 c5 C7 Ifunction ReadData_Callback(hObject, eventdata, handles)
8 e& k- Q7 W0 x& r, x% hObject handle to ReadData (see GCBO)
" O8 H/ _0 x# U+ i3 n- v% eventdata reserved - to be defined in a future version of MATLAB0 t6 a6 G, A& c& ?$ h$ a, |
% handles structure with handles and user data (see GUIDATA)9 {% ^: g- |7 h3 `: N l* D1 W
[file path]=uigetfile('*.txt','Choose a File');%读取文件,弹出对话框,提示选择文件
0 M# }2 V i3 {if file==0
, [3 M$ [0 ~. ] warndlg('用户必须选择一个txt文件');
2 _: e) F" J4 p7 C+ f8 j9 Z* ^! G. j# Zelse% g. V7 _2 {' G" Q
[x0,y0]=textread(fullfile(path,file),'%n%n','headerlines',6);%省略txt文件中前6行,只读取数字,共两列数据7 v2 i+ |5 R8 b7 M
handles.x0=x0;%将读取的第一列数据x0放到 handles结构体中,以便其他函数可以直接调用
/ B n) S+ e( S( H" n handles.y0=y0;%将读取的第二列数据y0放到 handles结构体中,同上
g a0 I" x( G% s5 g$ i plot(handles.axes1,x0,y0);%通过handles.axes1访问绘图区域,并将数据绘制在绘图区
' f. d/ R& K4 s2 B3 ?6 p: J- c: _end' a8 N1 F$ D+ y6 o
% Update handles structure
2 v+ A' @ `) {! S' L% N4 Sguidata(hObject, handles);
% h2 t0 `% w0 \% h%通过handles.axes1访问绘图区域3 t4 ~0 M" ]: B; T
%axes1对象属性中,nextplot为replace时,每次绘图均覆盖,为add时,就可以叠加了% I1 }1 W! Y0 h' M) h) ^
: a. q3 n' y3 a& g
不知道这个是不是你想要的。# k! T$ H! O0 B: V( S$ z& G7 p
|
|