|
MATLAB GUI中读取txt文件数据按钮回调函数
. n' g% `7 B# Q& L! Vfunction ReadData_Callback(hObject, eventdata, handles)
: h- r) b, i% B3 N% hObject handle to ReadData (see GCBO)
: z% X* S3 O0 u% b% eventdata reserved - to be defined in a future version of MATLAB
8 R4 c- I' D; L% handles structure with handles and user data (see GUIDATA)" s0 P/ T2 F* A6 @& H- g; D/ a
[file path]=uigetfile('*.txt','Choose a File');%读取文件,弹出对话框,提示选择文件 n3 Y6 }/ G9 z% x, `( B2 A! b! @* A
if file==0
2 A& e# h. {! V warndlg('用户必须选择一个txt文件');* d' n7 ^0 w D/ Q8 R3 Z2 i+ ?
else5 a+ g* j; G, O! b/ h" }
[x0,y0]=textread(fullfile(path,file),'%n%n','headerlines',6);%省略txt文件中前6行,只读取数字,共两列数据
9 L) r- W/ B5 k* I2 B7 c handles.x0=x0;%将读取的第一列数据x0放到 handles结构体中,以便其他函数可以直接调用
; _7 F/ S5 F4 \) e! y handles.y0=y0;%将读取的第二列数据y0放到 handles结构体中,同上
5 y4 W$ P) _ e- \) t4 S plot(handles.axes1,x0,y0);%通过handles.axes1访问绘图区域,并将数据绘制在绘图区7 T: @, M4 Z r! K
end/ [( L, C, i8 x; r9 [/ j g
% Update handles structure
# L9 ?) W% ]* L1 ^! x( bguidata(hObject, handles);1 l* P( _+ F$ S: D% r
%通过handles.axes1访问绘图区域
_# y X6 y9 D9 z q4 g$ {, C%axes1对象属性中,nextplot为replace时,每次绘图均覆盖,为add时,就可以叠加了( Z0 ^/ J' H3 z/ F1 W
; F8 Q2 g. D4 p不知道这个是不是你想要的。
3 {" ?, w+ U1 Q1 J+ M. ^/ U! }) f2 k0 _ |
|