当前位置:   article > 正文

使用matlab GUI设计离线问卷

使用matlab GUI设计离线问卷

很久没有更新了。没有更新的日子,也在努力学习,坚定了继续搞科研的决心,至死不渝。希望我们每个人都能有远大的理想,并为之付出不懈的奋斗。

目录

一、为什么

二、怎么设计

1. 问题和答案的显示

2. 答案的记录和保存

三、代码

四、附言


一、为什么

问卷为什么要用离线的?

因为项目不能使用线上问卷,而纸质版的问卷,人一多,后期答案统计起来会非常费劲!所以想着弄一个离线的。

为什么要用matlab GUI?

首先,不是因为熟悉matlab GUI。本人第一次使用matlab GUI,项目中刚好要用到matlab,所以就想着用matlab GUI去设计离线问卷。

二、怎么设计

关于matlab GUI的简单操作,有很多资源可供了解,也比较简单易懂,在此就不再多说。真正难住我的是,怎么写回调函数(callback),回调函数是指当你鼠标左击按下按钮之后的操作。因为是问卷,基本只涉及:

1. 问题和答案的显示;

2. 答案的保存和记录。

1. 问题和答案的显示

问题用静态文本框显示。而且还需要在开始前给答题者提示,对问卷做一个简单的介绍,也用静态文本框表示即可。界面如下:

答案选项的话:

1. 需要输入的用可编辑文本框显示;

2. 选择题的话用一个按钮组,把ABCD等每个选项用普通按钮显示,然后一起放进那个按钮组里。

本来想的是滚动页面显示不同的问题和答案,后来在博士师兄的建议下,使用的方法是:每次完成一个题,就更新内容。提供了上一题和下一题的按钮,不过每次鼠标左击选中答案后就会自动跳转到下一个问题。

另外,还需要把答案和问题都保存成excel表格,然后再用matlab读取。

举例如下:

2. 答案的记录和保存

1. 填空题的话,答题者输入后,点击文本框下的按钮,就记录并保存为mat。

2. 选择题的话,点击每个选项对应的按钮,就以数字(A对应1,B对应2,C对应3,D对应4,以此类推)传给问题的value,而我每次只需要保存并记录显示问题的静态文本框的value值即可。

三、代码

废话不多说,上代码:

  1. function varargout = wj_3(varargin)
  2. % WJ_3 MATLAB code for wj_3.fig
  3. % WJ_3, by itself, creates a new WJ_3 or raises the existing
  4. % singleton*.
  5. %
  6. % H = WJ_3 returns the handle to a new WJ_3 or the handle to
  7. % the existing singleton*.
  8. %
  9. % WJ_3('CALLBACK',hObject,eventData,handles,...) calls the local
  10. % function named CALLBACK in WJ_3.M with the given input arguments.
  11. %
  12. % WJ_3('Property','Value',...) creates a new WJ_3 or raises the
  13. % existing singleton*. Starting from the left, property value pairs are
  14. % applied to the GUI before wj_3_OpeningFcn gets called. An
  15. % unrecognized property name or invalid value makes property application
  16. % stop. All inputs are passed to wj_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 wj_3
  23. % Last Modified by GUIDE v2.5 13-Apr-2024 09:37:41
  24. %% 数据
  25. global que_cnt
  26. global answs
  27. global answers_3
  28. global ques
  29. % 读取表格 注意:读取之前删除不需要的数据
  30. [num,txt,raw] = xlsread('3.状态-特质焦虑量表(STAI).xlsx');
  31. [num0,txt0,raw0] = xlsread('3.答案.xlsx');
  32. ques = txt;
  33. answs =txt0;
  34. %%
  35. % Begin initialization code - DO NOT EDIT
  36. gui_Singleton = 1;
  37. gui_State = struct('gui_Name', mfilename, ...
  38. 'gui_Singleton', gui_Singleton, ...
  39. 'gui_OpeningFcn', @wj_3_OpeningFcn, ...
  40. 'gui_OutputFcn', @wj_3_OutputFcn, ...
  41. 'gui_LayoutFcn', [] , ...
  42. 'gui_Callback', []);
  43. if nargin && ischar(varargin{1})
  44. gui_State.gui_Callback = str2func(varargin{1});
  45. end
  46. if nargout
  47. [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
  48. else
  49. gui_mainfcn(gui_State, varargin{:});
  50. end
  51. % End initialization code - DO NOT EDIT
  52. % --- Executes just before wj_3 is made visible.
  53. function wj_3_OpeningFcn(hObject, eventdata, handles, varargin)
  54. % This function has no output args, see OutputFcn.
  55. % hObject handle to figure
  56. % eventdata reserved - to be defined in a future version of MATLAB
  57. % handles structure with handles and user data (see GUIDATA)
  58. % varargin command line arguments to wj_3 (see VARARGIN)
  59. % Choose default command line output for wj_3
  60. handles.output = hObject;
  61. % Update handles structure
  62. guidata(hObject, handles);
  63. % UIWAIT makes wj_3 wait for user response (see UIRESUME)
  64. % uiwait(handles.figure1);
  65. % --- Outputs from this function are returned to the command line.
  66. function varargout = wj_3_OutputFcn(hObject, eventdata, handles)
  67. % varargout cell array for returning output args (see VARARGOUT);
  68. % hObject handle to figure
  69. % eventdata reserved - to be defined in a future version of MATLAB
  70. % handles structure with handles and user data (see GUIDATA)
  71. % Get default command line output from handles structure
  72. varargout{1} = handles.output;
  73. % --- Executes on button press in xia.
  74. function xia_Callback(hObject, eventdata, handles)
  75. global que_cnt
  76. global answs
  77. global answers_3
  78. global ques
  79. % 保存上一题的答案
  80. disp(['上一题:',num2str(que_cnt)])
  81. answers_3(1,que_cnt) = get(handles.Q1,'Value');
  82. % 开始下一题
  83. if que_cnt >= length(ques) | que_cnt < 1
  84. que_cnt = 1;
  85. % 已经完成问卷
  86. set(handles.Q1,'Visible','off')
  87. set(handles.xuan_xiang,'Visible','off')
  88. set(handles.submmit,'Visible','on')
  89. set(handles.shang,'Visible','off')
  90. set(handles.xia,'Visible','off')
  91. set(handles.tishi,'String','问卷已完成,请提交!')
  92. set(handles.tishi,'Visible','on')
  93. set(handles.tishi,'Enable','on')
  94. else
  95. que_cnt = que_cnt + 1;
  96. end
  97. disp(['下一题:',num2str(que_cnt)])
  98. % 显示下一题的问题和答案
  99. set(handles.Q1,'String',ques(1,que_cnt))
  100. set(handles.radiobutton1,'String',answs{que_cnt,1})
  101. set(handles.radiobutton2,'String',answs{que_cnt,2})
  102. set(handles.radiobutton3,'String',answs{que_cnt,3})
  103. set(handles.radiobutton4,'String',answs{que_cnt,4})
  104. % --- Executes on button press in shang.
  105. function shang_Callback(hObject, eventdata, handles)
  106. global que_cnt
  107. global answs
  108. global answers_3
  109. global ques
  110. % 返回上一题
  111. if que_cnt <= 1
  112. que_cnt = 1;
  113. else
  114. que_cnt = que_cnt - 1;
  115. end
  116. disp(que_cnt);
  117. % 显示上一题的问题
  118. set(handles.Q1,'String',ques(1,que_cnt))
  119. % 显示上一题的答案
  120. set(handles.radiobutton1,'String',answs{que_cnt,1})
  121. set(handles.radiobutton2,'String',answs{que_cnt,2})
  122. set(handles.radiobutton3,'String',answs{que_cnt,3})
  123. set(handles.radiobutton4,'String',answs{que_cnt,4})
  124. % 保存答案
  125. answers_3(1,que_cnt) = get(handles.Q1,'Value');
  126. % --- Executes during object creation, after setting all properties.
  127. function T1_CreateFcn(hObject, eventdata, handles)
  128. % --- Executes during object creation, after setting all properties.
  129. function Q1_CreateFcn(hObject, eventdata, handles)
  130. % --- Executes on button press in radiobutton1.
  131. function radiobutton1_Callback(hObject, eventdata, handles)
  132. set(handles.Q1,'Value',1); % 把问题的的 value值分别用 1 2 3 4 表示,分别代表 A B C D
  133. global que_cnt
  134. global answs
  135. global answers_3
  136. global ques
  137. % 保存上一题的答案
  138. disp(['上一题:',num2str(que_cnt)])
  139. answers_3(1,que_cnt) = get(handles.Q1,'Value');
  140. % 开始下一题
  141. if que_cnt >= length(ques) | que_cnt < 1
  142. que_cnt = 1;
  143. % 已经完成问卷
  144. set(handles.Q1,'Visible','off')
  145. set(handles.xuan_xiang,'Visible','off')
  146. set(handles.submmit,'Visible','on')
  147. set(handles.shang,'Visible','off')
  148. set(handles.xia,'Visible','off')
  149. set(handles.tishi,'String','问卷已完成,请提交!')
  150. set(handles.tishi,'Visible','on')
  151. set(handles.tishi,'Enable','on')
  152. else
  153. que_cnt = que_cnt + 1;
  154. end
  155. disp(['下一题:',num2str(que_cnt)])
  156. % 显示下一题的问题和答案
  157. set(handles.Q1,'String',ques{1,que_cnt})
  158. set(handles.radiobutton1,'String',answs{que_cnt,1})
  159. set(handles.radiobutton2,'String',answs{que_cnt,2})
  160. set(handles.radiobutton3,'String',answs{que_cnt,3})
  161. set(handles.radiobutton4,'String',answs{que_cnt,4})
  162. % --- Executes on button press in radiobutton2.
  163. function radiobutton2_Callback(hObject, eventdata, handles)
  164. set(handles.Q1,'Value',2); % 把问题的的 value值分别用 1 2 3 4 表示,分别代表 A B C D
  165. global que_cnt
  166. global answs
  167. global answers_3
  168. global ques
  169. % 保存上一题的答案
  170. disp(['上一题:',num2str(que_cnt)])
  171. answers_3(1,que_cnt) = get(handles.Q1,'Value');
  172. % 开始下一题
  173. if que_cnt >= length(ques) | que_cnt < 1
  174. que_cnt = 1;
  175. % 已经完成问卷
  176. set(handles.Q1,'Visible','off')
  177. set(handles.xuan_xiang,'Visible','off')
  178. set(handles.submmit,'Visible','on')
  179. set(handles.shang,'Visible','off')
  180. set(handles.xia,'Visible','off')
  181. set(handles.tishi,'String','问卷已完成,请提交!')
  182. set(handles.tishi,'Visible','on')
  183. set(handles.tishi,'Enable','on')
  184. else
  185. que_cnt = que_cnt + 1;
  186. end
  187. disp(['下一题:',num2str(que_cnt)])
  188. % 显示下一题的问题和答案
  189. set(handles.Q1,'String',ques{1,que_cnt})
  190. set(handles.radiobutton1,'String',answs{que_cnt,1})
  191. set(handles.radiobutton2,'String',answs{que_cnt,2})
  192. set(handles.radiobutton3,'String',answs{que_cnt,3})
  193. set(handles.radiobutton4,'String',answs{que_cnt,4})
  194. % --- Executes on button press in radiobutton3.
  195. function radiobutton3_Callback(hObject, eventdata, handles)
  196. set(handles.Q1,'Value',3); % 把问题的的 value值分别用 1 2 3 4 表示,分别代表 A B C D
  197. global que_cnt
  198. global answs
  199. global answers_3
  200. global ques
  201. % 保存上一题的答案
  202. disp(['上一题:',num2str(que_cnt)])
  203. answers_3(1,que_cnt) = get(handles.Q1,'Value');
  204. % 开始下一题
  205. if que_cnt >= length(ques) | que_cnt < 1
  206. que_cnt = 1;
  207. % 已经完成问卷
  208. set(handles.Q1,'Visible','off')
  209. set(handles.xuan_xiang,'Visible','off')
  210. set(handles.submmit,'Visible','on')
  211. set(handles.shang,'Visible','off')
  212. set(handles.xia,'Visible','off')
  213. set(handles.tishi,'String','问卷已完成,请提交!')
  214. set(handles.tishi,'Visible','on')
  215. set(handles.tishi,'Enable','on')
  216. else
  217. que_cnt = que_cnt + 1;
  218. end
  219. disp(['下一题:',num2str(que_cnt)])
  220. % 显示下一题的问题和答案
  221. set(handles.Q1,'String',ques{1,que_cnt})
  222. set(handles.radiobutton1,'String',answs{que_cnt,1})
  223. set(handles.radiobutton2,'String',answs{que_cnt,2})
  224. set(handles.radiobutton3,'String',answs{que_cnt,3})
  225. set(handles.radiobutton4,'String',answs{que_cnt,4})
  226. % --- Executes on button press in radiobutton4.
  227. function radiobutton4_Callback(hObject, eventdata, handles)
  228. set(handles.Q1,'Value',4); % 把问题的的 value值分别用 1 2 3 4 表示,分别代表 A B C D
  229. global que_cnt
  230. global answs
  231. global answers_3
  232. global ques
  233. % 保存上一题的答案
  234. disp(['上一题:',num2str(que_cnt)])
  235. answers_3(1,que_cnt) = get(handles.Q1,'Value');
  236. % 开始下一题
  237. if que_cnt >= length(ques) | que_cnt < 1
  238. que_cnt = 1;
  239. % 已经完成问卷
  240. set(handles.Q1,'Visible','off')
  241. set(handles.xuan_xiang,'Visible','off')
  242. set(handles.submmit,'Visible','on')
  243. set(handles.shang,'Visible','off')
  244. set(handles.xia,'Visible','off')
  245. set(handles.tishi,'String','问卷已完成,请提交!')
  246. set(handles.tishi,'Visible','on')
  247. set(handles.tishi,'Enable','on')
  248. else
  249. que_cnt = que_cnt + 1;
  250. end
  251. disp(['下一题:',num2str(que_cnt)])
  252. % 显示下一题的问题和答案
  253. set(handles.Q1,'String',ques{1,que_cnt})
  254. set(handles.radiobutton1,'String',answs{que_cnt,1})
  255. set(handles.radiobutton2,'String',answs{que_cnt,2})
  256. set(handles.radiobutton3,'String',answs{que_cnt,3})
  257. set(handles.radiobutton4,'String',answs{que_cnt,4})
  258. % --- Executes during object creation, after setting all properties.
  259. function xuan_xiang_CreateFcn(hObject, eventdata, handles)
  260. % --- Executes during object creation, after setting all properties.
  261. function tishi_CreateFcn(hObject, eventdata, handles)
  262. % --- Executes during object creation, after setting all properties.
  263. function tishi_DeleteFcn(hObject, eventdata, handles)
  264. function edit1_Callback(hObject, eventdata, handles)
  265. function edit1_CreateFcn(hObject, eventdata, handles)
  266. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
  267. set(hObject,'BackgroundColor','white');
  268. end
  269. % --- Executes on button press in start.
  270. function start_Callback(hObject, eventdata, handles)
  271. global que_cnt
  272. que_cnt = 1;
  273. % 关闭提示和开始按钮
  274. set(handles.tishi,'Visible','off')
  275. set(handles.start,'Visible','off')
  276. set(handles.tishi,'Enable','off')
  277. set(handles.start,'Enable','off')
  278. set(handles.Q1,'Visible','on')
  279. % 可编辑文本
  280. set(handles.edit1,'Visible','on')
  281. set(handles.bianhao_ok,'Visible','on')
  282. % --- Executes on button press in bianhao_ok.
  283. function bianhao_ok_Callback(hObject, eventdata, handles)
  284. % 开始填写问卷
  285. global que_cnt
  286. global ques
  287. global answers_3
  288. que_cnt = 2;
  289. set(handles.Q1,'String',ques(1,que_cnt))
  290. set(handles.xuan_xiang,'Visible','on')
  291. % set(handles.submmit,'Visible','on')
  292. set(handles.shang,'Visible','on')
  293. set(handles.xia,'Visible','on')
  294. % 把从可编辑文本框中输入的编号传给Q1,保存
  295. set(handles.Q1,'Value',str2num(get(handles.edit1,'String')))
  296. set(handles.edit1,'Visible','off')
  297. set(handles.edit1,'Enable','off')
  298. set(handles.bianhao_ok,'Visible','off')
  299. set(handles.bianhao_ok,'Enable','off')
  300. answers_3(1,que_cnt-1) = str2num(get(handles.edit1,'String'));
  301. % --- Executes on button press in submmit.
  302. function submmit_Callback(hObject, eventdata, handles)
  303. set(handles.T1,'Visible','on') % 弹出问卷提交成功
  304. global answers_3
  305. save answers_3.mat answers_3 % 保存答案

注意:

假如你直接运行这个代码肯定是不行的。你需要打开guide,新建一个fig,然后再根据需要修改对应的回调函数。

四、附言

谨以此抛砖引玉,初次尝试,花了三四个半天写了10份离线问卷,每份问卷要求不一,我也是焦头烂额。

以上定然还有诸多不全,有建议请各位大佬赐教。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/435734
推荐阅读
相关标签
  

闽ICP备14008679号