⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 voicelevelgui.m

📁 matlab数据采集工具箱应用示例。gui是在matlab7.0.1下设计
💻 M
📖 第 1 页 / 共 3 页
字号:
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end




% --- Executes during object creation, after setting all properties.
function editLL5_CreateFcn(hObject, eventdata, handles)
% hObject    handle to editLL5 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



% --- Executes during object creation, after setting all properties.
function editALevel_CreateFcn(hObject, eventdata, handles)
% hObject    handle to editALevel (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


function editSampleR_Callback(hObject, eventdata, handles)
% hObject    handle to editSampleR (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of editSampleR as text
%        str2double(get(hObject,'String')) returns contents of editSampleR as a double
str = get(hObject,'string');
data = str2num(str);
if isempty(data)||data<8000||data>44100||round(data)~=data  % 输入参数有效性检测
    errordlg('输入必须为8000到44100之间的整数值!','参数错误');
    set(hObject,'BackgroundColor','r');
else
    set(hObject,'BackgroundColor','w');
    set(hObject,'UserData',data);
end



function editSamplesPerT_Callback(hObject, eventdata, handles)
% hObject    handle to editSamplesPerT (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of editSamplesPerT as text
%        str2double(get(hObject,'String')) returns contents of editSamplesPerT as a double
str = get(hObject,'string');
data = str2num(str);
if isempty(data)||data<=0||round(data)~=data  % 输入参数有效性检测
    errordlg('输入必须为大于0的整数值!','参数错误');
    set(hObject,'BackgroundColor','r');
else
    set(hObject,'BackgroundColor','w');
    set(hObject,'UserData',data);
end



function editTimerPeriod_Callback(hObject, eventdata, handles)
% hObject    handle to editTimerPeriod (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of editTimerPeriod as text
%        str2double(get(hObject,'String')) returns contents of editTimerPeriod as a double
str = get(hObject,'string');
data = str2num(str);
if isempty(data)||data<=0  % 输入参数有效性检测
    errordlg('输入必须为大于0的数值!','参数错误');
    set(hObject,'BackgroundColor','r');
else
    set(hObject,'BackgroundColor','w');
    set(hObject,'UserData',data);
end


function editChan_Callback(hObject, eventdata, handles)
% hObject    handle to editChan (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of editChan as text
%        str2double(get(hObject,'String')) returns contents of editChan as a double
str = get(hObject,'string');
data = str2num(str);
if isempty(data)||data<0||round(data)~=data||length(data)>1  % 输入参数有效性检测
    errordlg('输入必须为1个不小于0的整数值!','参数错误');
    set(hObject,'BackgroundColor','r');
else
    set(hObject,'BackgroundColor','w');
    set(hObject,'UserData',data);
end



function editId_Callback(hObject, eventdata, handles)
% hObject    handle to editId (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of editId as text
%        str2double(get(hObject,'String')) returns contents of editId as a double
str = get(hObject,'string');
data = str2num(str);
if isempty(data)||data<0||round(data)~=data  % 输入参数有效性检测
    errordlg('输入必须为不小于0的整数值!','参数错误');
    set(hObject,'BackgroundColor','r');
else
    set(hObject,'BackgroundColor','w');
    set(hObject,'UserData',data);
end



% --- Executes on button press in btnRun.
function btnRun_Callback(hObject, eventdata, handles)
% hObject    handle to btnRun (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
str = get(hObject,'string');
flag = get(hObject,'UserData');
if strcmp(str,'Run')    % 如果尚未运行或已经停止
    if flag % 如果是暂停
        try
            start(handles.ai);
            set(hObject,'string','Stop');
            return;
        catch
            errStr = lasterr;
            errordlg(errStr,'Error...');
        end
    end
    % 获取设置的参数
    deviceId = get(handles.editId,'UserData');
    hwChan = get(handles.editChan,'UserData');
    sampleR = get(handles.editSampleR,'UserData');
    samplesPerT = get(handles.editSamplesPerT,'UserData');
    timerPeriod = get(handles.editTimerPeriod,'UserData');
    try
        set(handles.voiceLevelGUI,'pointer','watch'); % 设置鼠标为等待态
        % 创建两个曲线句柄
        axes(handles.axes1);  cla
        hline1 = line('xdata',[0 0.01],'ydata',[0,1e-6],'visible','off');
        axes(handles.axes2);  cla
        axis([0 1 0 1]);
        hline2 = line('xdata',[0 0.01],'ydata',[0,1e-6],'visible','off');
        %%
        % 创建patch
        % 倍频程
        axes(handles.axesL1);
        line([0 1],[0 0],'color','r');
        patchL1 = patch([0 1 1 0],[0 0 0 0],'b');
        axes(handles.axesL2);
        line([0 1],[0 0],'color','r');
        patchL2 = patch([0 1 1 0],[0 0 0 0],'b');
        axes(handles.axesL3);
        line([0 1],[0 0],'color','r');
        patchL3 = patch([0 1 1 0],[0 0 0 0],'b');
         axes(handles.axesL4);
        line([0 1],[0 0],'color','r');
        patchL4 = patch([0 1 1 0],[0 0 0 0],'b');
        axes(handles.axesL5);
        line([0 1],[0 0],'color','r');
        patchL5 = patch([0 1 1 0],[0 0 0 0],'b');
        
        % 1/3倍频程
        axes(handles.axesLL1);
        line([0 1],[0 0],'color','r');
        patchLL1 = patch([0 1 1 0],[0 0 0 0],'b');
        axes(handles.axesLL2);
        line([0 1],[0 0],'color','r');
        patchLL2 = patch([0 1 1 0],[0 0 0 0],'b');
        axes(handles.axesLL3);
        line([0 1],[0 0],'color','r');
        patchLL3 = patch([0 1 1 0],[0 0 0 0],'b');
         axes(handles.axesLL4);
        line([0 1],[0 0],'color','r');
        patchLL4 = patch([0 1 1 0],[0 0 0 0],'b');
        axes(handles.axesLL5);
        line([0 1],[0 0],'color','r');
        patchLL5 = patch([0 1 1 0],[0 0 0 0],'b');
        
        % 存储对象与guidata
        handles.line1 = hline1;
        handles.line2 = hline2;
        handles.patchL1 = patchL1;
        handles.patchL2 = patchL2;
        handles.patchL3 = patchL3;
        handles.patchL4 = patchL4;
        handles.patchL5 = patchL5;
        handles.patchLL1 = patchLL1;
        handles.patchLL2 = patchLL2;
        handles.patchLL3 = patchLL3;
        handles.patchLL4 = patchLL4;
        handles.patchLL5 = patchLL5;

        guidata(gcf,handles);
        %% 
        % 创建并配置object
        ai = analoginput('winsound',deviceId);
        addchannel(ai,hwChan);
        actrualSampleR = setverify(ai,'SampleRate',sampleR);
        set(handles.editSampleR,'string',actrualSampleR);
        set(ai,'SamplesPerTrigger',samplesPerT);
        set(ai,'TriggerRepeat',1);
        set(ai,'TriggerType','manual');
        set(ai,'TimerPeriod',timerPeriod);
        set(ai,'TimerFcn',{@localfftShowData,handles}); % pass addtional paras
%         set(ai,'SamplesAcquiredFcn',{@saveData,handles});
        start(ai);  % Start the analog input object.
        trigger(ai);
        handles.ai = ai;
        guidata(gcf,handles);
        %%
        % 界面状态更新
        set(handles.btnExport,'UserData',[]);
        set(hObject,'string','Stop');
        set(handles.editId,'enable','off');
        set(handles.editChan,'enable','off');
        set(handles.editSampleR,'enable','off');
        set(handles.editSamplesPerT,'enable','off');
        set(handles.editTimerPeriod,'enable','off');
        set(handles.btnReset,'enable','off');
        set(handles.voiceLevelGUI,'pointer','arrow'); % 设置鼠标为正常态
    catch
        errStr = lasterr;
        errordlg(errStr,'Error...');
        set(handles.voiceLevelGUI,'pointer','arrow'); % 设置鼠标为正常态
    end
else    % 如果已经运行
    stop(handles.ai);
    % 将轴上现有数据保存在轴的userdata中备用
    xresult = get(handles.line1,'ydata');
    set(handles.axes1,'UserData',xresult);
    xresult = get(handles.line2,'xdata');
    yresult = get(handles.line2,'ydata');
    set(handles.axes1,'UserData',[xresult(:),yresult(:)]);
    % 更新界面
    set(hObject,'UserData',1);
    set(hObject,'string','Run');
    set(handles.btnReset,'enable','on');
end


% --- Executes on button press in btnHelp.
function btnHelp_Callback(hObject, eventdata, handles)
% hObject    handle to btnHelp (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% msgstr = 'Sorry,it doesn''t finish yet!';
% msgbox(msgstr,'Sorry...');
!voiceLevelHelp.CHM  

% --- Executes on button press in btnExport.
function btnExport_Callback(hObject, eventdata, handles)
% hObject    handle to btnExport (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% 获取待保存的数据
BandData = getappdata(handles.voiceLevelGUI,'BandData');
Band2Data = getappdata(handles.voiceLevelGUI,'Band2Data');
LaLevel = get(handles.editALevel,'UserData');

result.BandData = BandData;
result.Band2Data = Band2Data;
result.LaLevel = LaLevel;

ButtonName=questdlg('保存结果为何种形式?', ...
    '测量结果保存', ...
    'TXT文件','MAT文件','取消','MAT文件');
switch ButtonName,
    case 'MAT文件',
        [fname,path] = uiputfile('*.mat','保存为');
        if path==0  % 取消文件保存操作
            return;
        end
        save([path fname],'result','-v6'); % 保存为低版本也可以用的mat文件
    case 'TXT文件',
        [fname,pathname] = uiputfile('*.txt','保存为');
        if pathname==0  % 取消文件保存操作
            return;
        end
        [pathstr,filename]=fileparts(fname);
        fid = fopen([pathname filename,'.txt'],'w');
        fprintf(fid,'\r\n%%=================%s=================\r\n',datestr(now,0));
        fprintf(fid,'\r\n倍频程声级\r\n');
        fprintf(fid,'%%%8.4f %8.4f %8.4f %8.4f %8.4f\r\n',31.5,63,125,250,500);
        fprintf(fid,'%8.6f %8.6f %8.6f %8.6f %8.6f\r\n',BandData);
        fprintf(fid,'\r\n');
        fprintf(fid,'%%三分之一倍频程声级\r\n');
        fprintf(fid,'%%%8.4f %8.4f %8.4f %8.4f %8.4f\r\n',50,100,200,400,800);
        fprintf(fid,'%8.6f %8.6f %8.6f %8.6f %8.6f\r\n',Band2Data);
%         fprintf(fid,'\r\n');
        fprintf(fid,'\r\n%% A声级\r\n');
        fprintf(fid,'%8.6f\r\n',LaLevel);
        fclose(fid);

    case '取消',
        return;
end % switch



% --- Executes on button press in btnReset.
function btnReset_Callback(hObject, eventdata, handles)
% hObject    handle to btnReset (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

set(handles.editId,'enable','on');
set(handles.editChan,'enable','on');
set(handles.editSampleR,'enable','on');
set(handles.editSamplesPerT,'enable','on');
set(handles.editTimerPeriod,'enable','on');
set(handles.editId,'string',num2str(0),'UserData',0);
set(handles.editChan,'string',num2str(1),'UserData',1);
set(handles.editSampleR,'string',num2str(8000),'UserData',8000);
set(handles.editSamplesPerT,'string',num2str(1024),'UserData',1024);
set(handles.editTimerPeriod,'string',num2str(0.2),'UserData',0.2);
set(handles.btnRun,'UserData',0);



% --- Executes on button press in btnExit.
function btnExit_Callback(hObject, eventdata, handles)
% hObject    handle to btnExit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
selection = questdlg(['退出 ' get(handles.voiceLevelGUI,'Name') '?'],...
    ['退出 ...'],...
    '是','否','是');
if strcmp(selection,'否')
    return;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -