MATLAB GUI uitable  使用方法

来源:互联网 发布:卖生活用品的淘宝店铺 编辑:程序博客网 时间:2024/06/03 16:49


uitable.m代码如下:
  1. function varargout = uitable_3(varargin)
  2. % UITABLE_3 MATLAB code for uitable_3.fig
  3. % UITABLE_3, by itself, creates a new UITABLE_3 or raises the existing
  4. % singleton*.
  5. %
  6. % H = UITABLE_3 returns the handle to a new UITABLE_3 or the handle to
  7. % the existing singleton*.
  8. %
  9. % UITABLE_3('CALLBACK',hObject,eventData,handles,...) calls the local
  10. % function named CALLBACK in UITABLE_3.M with the given input arguments.
  11. %
  12. % UITABLE_3('Property','Value',...) creates a new UITABLE_3 or raises the
  13. % existing singleton*. Starting from the left, property value pairs are
  14. % applied to the GUI before uitable_3_OpeningFcn gets called. An
  15. % unrecognized property name or invalid value makes property application
  16. % stop. All inputs are passed to uitable_3_OpeningFcn via varargin.
  17. %
  18. % *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
  19. % instance to run (singleton)".
  20. %
  21. % See also: GUIDE, GUIDATA, GUIHANDLES
  22. % Edit the above text to modify the response to help uitable_3
  23. % Last Modified by GUIDE v2.5 08-Apr-2016 08:35:53
  24. % Begin initialization code - DO NOT EDIT
  25. gui_Singleton = 1;
  26. gui_State = struct('gui_Name', mfilename, ...
  27. 'gui_Singleton', gui_Singleton, ...
  28. 'gui_OpeningFcn', @uitable_3_OpeningFcn, ...
  29. 'gui_OutputFcn', @uitable_3_OutputFcn, ...
  30. 'gui_LayoutFcn', [] , ...
  31. 'gui_Callback', []);
  32. if nargin && ischar(varargin{1})
  33. gui_State.gui_Callback = str2func(varargin{1});
  34. end
  35. if nargout
  36. [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
  37. else
  38. gui_mainfcn(gui_State, varargin{:});
  39. end
  40. % End initialization code - DO NOT EDIT
  41. % --- Executes just before uitable_3 is made visible.
  42. function uitable_3_OpeningFcn(hObject, eventdata, handles, varargin)
  43. % This function has no output args, see OutputFcn.
  44. % hObject handle to figure
  45. % eventdata reserved - to be defined in a future version of MATLAB
  46. % handles structure with handles and user data (see GUIDATA)
  47. % varargin command line arguments to uitable_3 (see VARARGIN)
  48. % Choose default command line output for uitable_3
  49. handles.output = hObject;
  50. % Update handles structure
  51. guidata(hObject, handles);
  52. % UIWAIT makes uitable_3 wait for user response (see UIRESUME)
  53. % uiwait(handles.figure1);
  54. % --- Outputs from this function are returned to the command line.
  55. function varargout = uitable_3_OutputFcn(hObject, eventdata, handles)
  56. % varargout cell array for returning output args (see VARARGOUT);
  57. % hObject handle to figure
  58. % eventdata reserved - to be defined in a future version of MATLAB
  59. % handles structure with handles and user data (see GUIDATA)
  60. % Get default command line output from handles structure
  61. varargout{1} = handles.output;
  62. % --- Executes on button press in pushbutton1.
  63. function pushbutton1_Callback(hObject, eventdata, handles)
  64. % hObject handle to pushbutton1 (see GCBO)
  65. % eventdata reserved - to be defined in a future version of MATLAB
  66. % handles structure with handles and user data (see GUIDATA)
  67. [filename, pathname] = uigetfile({'*.xlsx'; '*.xls'; '*.txt'}, '读入测试数据文件');
  68. [num, txt, raw] = xlsread([pathname, filename])
  69. %{
  70. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%下面是读取txt文件数据
  71. delimiter = '\t';
  72. formatSpec = '%s%s%s%[^\n\r]';%读取3列的格式
  73. fileID = fopen([pathname '\' filename], 'r');
  74. dataArray = textscan(fileID, formatSpec, 'Delimiter', delimiter, 'EmptyValue' ,NaN, 'ReturnOnError', false);
  75. fclose(fileID);
  76. data = [dataArray{1:end}]%cell数组
  77. data = [data(2:end, 2:end)]%取数据部分
  78. data = char(data)%转换成字符数组
  79. data = str2num(data)%字符转换成数值数组
  80. data = reshape(data, [], 3)%转换成3*3数组,在这里面占位符[ ] 只能使用一次。
  81. figure
  82. plot(data(2, :), data(2, :))
  83. set(handles.tab1, 'Data', data(1:end, 1:end))
  84. %}
  85. set(handles.tab1, 'Data', num( :, 2:end))
  86. % --- Executes during object creation, after setting all properties.
  87. function tab2_CreateFcn(hObject, eventdata, handles)
  88. % hObject handle to tab2 (see GCBO)
  89. % eventdata reserved - to be defined in a future version of MATLAB
  90. % handles empty - handles not created until after all CreateFcns called
  91. set(hObject, 'Data', [4.97, 0.01, 5.1])
  92. % --- Executes on button press in pushbutton2.
  93. function pushbutton2_Callback(hObject, eventdata, handles)
  94. % hObject handle to pushbutton2 (see GCBO)
  95. % eventdata reserved - to be defined in a future version of MATLAB
  96. % handles structure with handles and user data (see GUIDATA)
  97. Testdata = get(handles.tab1, 'Data')
  98. Limits = get(handles.tab2, 'Data')
  99. axes(handles.axes1)
  100. plot(1:4, Testdata, 'Marker', 'o')
  101. hold on
  102. plot([1,4], [Limits(1), Limits(1)], 'g--', 'LineWidth',2)
  103. plot([1,4], [Limits(3), Limits(3)], 'r--', 'LineWidth',2)
  104. set(gca, 'YTick',[Limits(1):Limits(2):Limits(3)]) %标上小刻度
  105. set(gca, 'YLim', [Limits(1)-0.01 Limits(3)+0.01]) %设置Y坐标范围
  106. legend({'测量员甲','测量员乙','测量员丙','重量下限','重量上限'}, 'Location', 'NorthEastOutside') %设置图例,以及位置
  107. hold off %方便下次改变数值的时候,刷新界面
效果如下:




0 0
原创粉丝点击