很久没有更新了。没有更新的日子,也在努力学习,坚定了继续搞科研的决心,至死不渝。希望我们每个人都能有远大的理想,并为之付出不懈的奋斗。
目录
[1. 问题和答案的显示](#1. 问题和答案的显示)
[2. 答案的记录和保存](#2. 答案的记录和保存)
一、为什么
问卷为什么要用离线的?
因为项目不能使用线上问卷,而纸质版的问卷,人一多,后期答案统计起来会非常费劲!所以想着弄一个离线的。
为什么要用matlab GUI?
首先,不是因为熟悉matlab GUI。本人第一次使用matlab GUI,项目中刚好要用到matlab,所以就想着用matlab GUI去设计离线问卷。
二、怎么设计
关于matlab GUI的简单操作,有很多资源可供了解,也比较简单易懂,在此就不再多说。真正难住我的是,怎么写回调函数(callback),回调函数是指当你鼠标左击按下按钮之后的操作。因为是问卷,基本只涉及:
-
问题和答案的显示;
-
答案的保存和记录。
1. 问题和答案的显示
问题用静态文本框显示。而且还需要在开始前给答题者提示,对问卷做一个简单的介绍,也用静态文本框表示即可。界面如下:
答案选项的话:
-
需要输入的用可编辑文本框显示;
-
选择题的话用一个按钮组 ,把ABCD等每个选项用普通按钮显示,然后一起放进那个按钮组里。
本来想的是滚动页面显示不同的问题和答案,后来在博士师兄的建议下,使用的方法是:每次完成一个题,就更新内容。提供了上一题和下一题的按钮,不过每次鼠标左击选中答案后就会自动跳转到下一个问题。
另外,还需要把答案和问题都保存成excel表格,然后再用matlab读取。
举例如下:
2. 答案的记录和保存
-
填空题的话,答题者输入后,点击文本框下的按钮,就记录并保存为mat。
-
选择题的话,点击每个选项对应的按钮,就以数字(A对应1,B对应2,C对应3,D对应4,以此类推)传给问题的value,而我每次只需要保存并记录显示问题的静态文本框的value值即可。
三、代码
废话不多说,上代码:
Matlab
function varargout = wj_3(varargin)
% WJ_3 MATLAB code for wj_3.fig
% WJ_3, by itself, creates a new WJ_3 or raises the existing
% singleton*.
%
% H = WJ_3 returns the handle to a new WJ_3 or the handle to
% the existing singleton*.
%
% WJ_3('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in WJ_3.M with the given input arguments.
%
% WJ_3('Property','Value',...) creates a new WJ_3 or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before wj_3_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to wj_3_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help wj_3
% Last Modified by GUIDE v2.5 13-Apr-2024 09:37:41
%% 数据
global que_cnt
global answs
global answers_3
global ques
% 读取表格 注意:读取之前删除不需要的数据
[num,txt,raw] = xlsread('3.状态-特质焦虑量表(STAI).xlsx');
[num0,txt0,raw0] = xlsread('3.答案.xlsx');
ques = txt;
answs =txt0;
%%
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @wj_3_OpeningFcn, ...
'gui_OutputFcn', @wj_3_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before wj_3 is made visible.
function wj_3_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to wj_3 (see VARARGIN)
% Choose default command line output for wj_3
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes wj_3 wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = wj_3_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on button press in xia.
function xia_Callback(hObject, eventdata, handles)
global que_cnt
global answs
global answers_3
global ques
% 保存上一题的答案
disp(['上一题:',num2str(que_cnt)])
answers_3(1,que_cnt) = get(handles.Q1,'Value');
% 开始下一题
if que_cnt >= length(ques) | que_cnt < 1
que_cnt = 1;
% 已经完成问卷
set(handles.Q1,'Visible','off')
set(handles.xuan_xiang,'Visible','off')
set(handles.submmit,'Visible','on')
set(handles.shang,'Visible','off')
set(handles.xia,'Visible','off')
set(handles.tishi,'String','问卷已完成,请提交!')
set(handles.tishi,'Visible','on')
set(handles.tishi,'Enable','on')
else
que_cnt = que_cnt + 1;
end
disp(['下一题:',num2str(que_cnt)])
% 显示下一题的问题和答案
set(handles.Q1,'String',ques(1,que_cnt))
set(handles.radiobutton1,'String',answs{que_cnt,1})
set(handles.radiobutton2,'String',answs{que_cnt,2})
set(handles.radiobutton3,'String',answs{que_cnt,3})
set(handles.radiobutton4,'String',answs{que_cnt,4})
% --- Executes on button press in shang.
function shang_Callback(hObject, eventdata, handles)
global que_cnt
global answs
global answers_3
global ques
% 返回上一题
if que_cnt <= 1
que_cnt = 1;
else
que_cnt = que_cnt - 1;
end
disp(que_cnt);
% 显示上一题的问题
set(handles.Q1,'String',ques(1,que_cnt))
% 显示上一题的答案
set(handles.radiobutton1,'String',answs{que_cnt,1})
set(handles.radiobutton2,'String',answs{que_cnt,2})
set(handles.radiobutton3,'String',answs{que_cnt,3})
set(handles.radiobutton4,'String',answs{que_cnt,4})
% 保存答案
answers_3(1,que_cnt) = get(handles.Q1,'Value');
% --- Executes during object creation, after setting all properties.
function T1_CreateFcn(hObject, eventdata, handles)
% --- Executes during object creation, after setting all properties.
function Q1_CreateFcn(hObject, eventdata, handles)
% --- Executes on button press in radiobutton1.
function radiobutton1_Callback(hObject, eventdata, handles)
set(handles.Q1,'Value',1); % 把问题的的 value值分别用 1 2 3 4 表示,分别代表 A B C D
global que_cnt
global answs
global answers_3
global ques
% 保存上一题的答案
disp(['上一题:',num2str(que_cnt)])
answers_3(1,que_cnt) = get(handles.Q1,'Value');
% 开始下一题
if que_cnt >= length(ques) | que_cnt < 1
que_cnt = 1;
% 已经完成问卷
set(handles.Q1,'Visible','off')
set(handles.xuan_xiang,'Visible','off')
set(handles.submmit,'Visible','on')
set(handles.shang,'Visible','off')
set(handles.xia,'Visible','off')
set(handles.tishi,'String','问卷已完成,请提交!')
set(handles.tishi,'Visible','on')
set(handles.tishi,'Enable','on')
else
que_cnt = que_cnt + 1;
end
disp(['下一题:',num2str(que_cnt)])
% 显示下一题的问题和答案
set(handles.Q1,'String',ques{1,que_cnt})
set(handles.radiobutton1,'String',answs{que_cnt,1})
set(handles.radiobutton2,'String',answs{que_cnt,2})
set(handles.radiobutton3,'String',answs{que_cnt,3})
set(handles.radiobutton4,'String',answs{que_cnt,4})
% --- Executes on button press in radiobutton2.
function radiobutton2_Callback(hObject, eventdata, handles)
set(handles.Q1,'Value',2); % 把问题的的 value值分别用 1 2 3 4 表示,分别代表 A B C D
global que_cnt
global answs
global answers_3
global ques
% 保存上一题的答案
disp(['上一题:',num2str(que_cnt)])
answers_3(1,que_cnt) = get(handles.Q1,'Value');
% 开始下一题
if que_cnt >= length(ques) | que_cnt < 1
que_cnt = 1;
% 已经完成问卷
set(handles.Q1,'Visible','off')
set(handles.xuan_xiang,'Visible','off')
set(handles.submmit,'Visible','on')
set(handles.shang,'Visible','off')
set(handles.xia,'Visible','off')
set(handles.tishi,'String','问卷已完成,请提交!')
set(handles.tishi,'Visible','on')
set(handles.tishi,'Enable','on')
else
que_cnt = que_cnt + 1;
end
disp(['下一题:',num2str(que_cnt)])
% 显示下一题的问题和答案
set(handles.Q1,'String',ques{1,que_cnt})
set(handles.radiobutton1,'String',answs{que_cnt,1})
set(handles.radiobutton2,'String',answs{que_cnt,2})
set(handles.radiobutton3,'String',answs{que_cnt,3})
set(handles.radiobutton4,'String',answs{que_cnt,4})
% --- Executes on button press in radiobutton3.
function radiobutton3_Callback(hObject, eventdata, handles)
set(handles.Q1,'Value',3); % 把问题的的 value值分别用 1 2 3 4 表示,分别代表 A B C D
global que_cnt
global answs
global answers_3
global ques
% 保存上一题的答案
disp(['上一题:',num2str(que_cnt)])
answers_3(1,que_cnt) = get(handles.Q1,'Value');
% 开始下一题
if que_cnt >= length(ques) | que_cnt < 1
que_cnt = 1;
% 已经完成问卷
set(handles.Q1,'Visible','off')
set(handles.xuan_xiang,'Visible','off')
set(handles.submmit,'Visible','on')
set(handles.shang,'Visible','off')
set(handles.xia,'Visible','off')
set(handles.tishi,'String','问卷已完成,请提交!')
set(handles.tishi,'Visible','on')
set(handles.tishi,'Enable','on')
else
que_cnt = que_cnt + 1;
end
disp(['下一题:',num2str(que_cnt)])
% 显示下一题的问题和答案
set(handles.Q1,'String',ques{1,que_cnt})
set(handles.radiobutton1,'String',answs{que_cnt,1})
set(handles.radiobutton2,'String',answs{que_cnt,2})
set(handles.radiobutton3,'String',answs{que_cnt,3})
set(handles.radiobutton4,'String',answs{que_cnt,4})
% --- Executes on button press in radiobutton4.
function radiobutton4_Callback(hObject, eventdata, handles)
set(handles.Q1,'Value',4); % 把问题的的 value值分别用 1 2 3 4 表示,分别代表 A B C D
global que_cnt
global answs
global answers_3
global ques
% 保存上一题的答案
disp(['上一题:',num2str(que_cnt)])
answers_3(1,que_cnt) = get(handles.Q1,'Value');
% 开始下一题
if que_cnt >= length(ques) | que_cnt < 1
que_cnt = 1;
% 已经完成问卷
set(handles.Q1,'Visible','off')
set(handles.xuan_xiang,'Visible','off')
set(handles.submmit,'Visible','on')
set(handles.shang,'Visible','off')
set(handles.xia,'Visible','off')
set(handles.tishi,'String','问卷已完成,请提交!')
set(handles.tishi,'Visible','on')
set(handles.tishi,'Enable','on')
else
que_cnt = que_cnt + 1;
end
disp(['下一题:',num2str(que_cnt)])
% 显示下一题的问题和答案
set(handles.Q1,'String',ques{1,que_cnt})
set(handles.radiobutton1,'String',answs{que_cnt,1})
set(handles.radiobutton2,'String',answs{que_cnt,2})
set(handles.radiobutton3,'String',answs{que_cnt,3})
set(handles.radiobutton4,'String',answs{que_cnt,4})
% --- Executes during object creation, after setting all properties.
function xuan_xiang_CreateFcn(hObject, eventdata, handles)
% --- Executes during object creation, after setting all properties.
function tishi_CreateFcn(hObject, eventdata, handles)
% --- Executes during object creation, after setting all properties.
function tishi_DeleteFcn(hObject, eventdata, handles)
function edit1_Callback(hObject, eventdata, handles)
function edit1_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in start.
function start_Callback(hObject, eventdata, handles)
global que_cnt
que_cnt = 1;
% 关闭提示和开始按钮
set(handles.tishi,'Visible','off')
set(handles.start,'Visible','off')
set(handles.tishi,'Enable','off')
set(handles.start,'Enable','off')
set(handles.Q1,'Visible','on')
% 可编辑文本
set(handles.edit1,'Visible','on')
set(handles.bianhao_ok,'Visible','on')
% --- Executes on button press in bianhao_ok.
function bianhao_ok_Callback(hObject, eventdata, handles)
% 开始填写问卷
global que_cnt
global ques
global answers_3
que_cnt = 2;
set(handles.Q1,'String',ques(1,que_cnt))
set(handles.xuan_xiang,'Visible','on')
% set(handles.submmit,'Visible','on')
set(handles.shang,'Visible','on')
set(handles.xia,'Visible','on')
% 把从可编辑文本框中输入的编号传给Q1,保存
set(handles.Q1,'Value',str2num(get(handles.edit1,'String')))
set(handles.edit1,'Visible','off')
set(handles.edit1,'Enable','off')
set(handles.bianhao_ok,'Visible','off')
set(handles.bianhao_ok,'Enable','off')
answers_3(1,que_cnt-1) = str2num(get(handles.edit1,'String'));
% --- Executes on button press in submmit.
function submmit_Callback(hObject, eventdata, handles)
set(handles.T1,'Visible','on') % 弹出问卷提交成功
global answers_3
save answers_3.mat answers_3 % 保存答案
注意:
**假如你直接运行这个代码肯定是不行的。**你需要打开guide,新建一个fig,然后再根据需要修改对应的回调函数。
四、附言
谨以此抛砖引玉,初次尝试,花了三四个半天写了10份离线问卷,每份问卷要求不一,我也是焦头烂额。
以上定然还有诸多不全,有建议请各位大佬赐教。