【问题标题】:How to create next and back push buttons in Guide Matlab如何在 Guide Matlab 中创建下一个和后退按钮
【发布时间】:2014-06-25 18:16:55
【问题描述】:

我正在制作一个 GUI,我正在使用 GUIDE。在写我的问题之前,我必须解释一些细节,所以它们是: - 数据类型:文本文件,来自 2 个人腿的 16 块肌肉的 EMG 数据,所以我必须绘制 16 个图表。 - 我的 GUI 的目标是在每个“页面”中显示 4 个,所以我需要下一个和后退按钮才能看到所有 16 个图形。换句话说,我的 Gui 需要 4 个“页面”。 - 我的指南由 1 次按下底部浏览我的数据、1 次按下底部以切换(更新)4 个图形的形状在 ABS 中显示为波形、带包络的图形以及 ABS 和包络图的组合、1 个用于切换的弹出菜单显示图形的形状,还有两个额外的按钮,一个是下一个按钮,另一个是返回按钮。

总而言之,我只是纠结于如何创建下一个和后退按钮。我的代码的其他角色已经开发完毕。

提前致谢。

Ps:感谢建议我改进问题的人。

是的,我想重绘我的图表(我的英语不太好,所以我无法更好地解释)

->>>>>> 这是我的浏览器功能

    function Search_Patient_Callback(hObject, eventdata, handles)
    % hObject    handle to Search_Patient (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    % Hint: get(hObject,'Value') returns toggle state of Search_Patient
        global cell_data
        [file, path]=uigetfile('*.txt', 'Select file');
        arq = [path,file];
        full_pathname=strcat(path,file);
        text=fileread(full_pathname);
        cell_data= textscan(text, repmat('%f',1,41),'headerlines',11);

        delimiter = '\t';
        startRow = 3;
        endRow = 8;
        formatSpec = '%*s%*s%*s%f%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%[^\n\r]';
        fileID = fopen(arq,'r');
        textscan(fileID, '%[^\n\r]', startRow-1, 'ReturnOnError', false);
        dataArray = textscan(fileID, formatSpec, endRow-startRow+1, 'Delimiter', delimiter, 'ReturnOnError', false);

        time_foot1 = dataArray{:, 1};

    %h = handles.Search_Patient;
    %setappdata(0,'My_main_Data',cell_data)
                     setappdata(0,'time_foot',time_foot1)


    handles = guidata(hObject); %Updating variables
    guidata(hObject,handles);   %Updating variables

->>>>>>>这是我的更新功能。

    function Updating_Callback(hObject, eventdata, handles)

    % hObject    handle to Updating (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    global cell_data
    y_data=0.001;
    t=(cell_data{1,1})/1000;
    time_foot1= getappdata(0,'time_foot');
    text_rfoot=0.0009;
    text_lfoot=0.0007;


    [emg1_1 emg2_1 emg3_1 emg4_1  emg1e_1 emg2e_1 emg3e_1 emg4e_1]=emg_signal(cell_data); % EMG RMS and EMG Envelope



    popup_sel_index = get(handles.popupmenu, 'Value');
    switch popup_sel_index
        case 1        
            axes (handles.axes1);
            plot (t,emg1_1,'r-');
            title('EMG Left Vastus medialis  :  ' )
            xlabel('Time(s)')
            ylabel('Voltage (uV)')

             axes (handles.axes2);
             plot (t,emg2_1,'r-');

            axes (handles.axes3);
            plot (t,emg3_1,'r-');

            axes (handles.axes4);
            plot (t,emg4_1,'r-');

            axis ([t(1) (t(1)+(length (t')/1000)) 0 y_data])
            %Right-Foot Strike1
            text(time_foot1(4),text_rfoot, ' Right-Strike 1', 'Color', 'k')
            line('XData',[time_foot1(4) time_foot1(4)], 'YData', [0 y_data], 'LineStyle', '-','LineWidth', 1, 'Color','k');
            %Right  Foot Off
            text(time_foot1(6),text_rfoot, ' Right-Off', 'Color', 'k')
            line('XData',[time_foot1(6) time_foot1(6)], 'YData', [0 y_data], 'LineStyle', '-','LineWidth', 1, 'Color','k');
            %Right-Foot Strike2
            text(time_foot1(5),text_rfoot, ' Right-Strike 2', 'Color', 'k')
            line('XData',[time_foot1(5) time_foot1(5)], 'YData', [0 y_data], 'LineStyle', '-','LineWidth', 1, 'Color','k');
            %Left-Foot Strike
            text(time_foot1(1),text_lfoot, ' Left-Strike', 'Color', 'b')
            line('XData',[time_foot1(1) time_foot1(1)], 'YData', [0 y_data], 'LineStyle', '-','LineWidth', 1, 'Color','b');
            %Left   Foot Off
            text(time_foot1(3),text_lfoot, ' Left-Off', 'Color', 'b')
            line('XData',[time_foot1(3) time_foot1(3)], 'YData', [0 y_data], 'LineStyle', '-','LineWidth', 1, 'Color','b');
            title('EMG Left Vastus medialis  :  ' )
            xlabel('Time(s)')
            ylabel('Voltage (uV)')

        case 2      
            axes (handles.axes1);
            plot (t,emg1e_1,'b-','linewidth', 2);
            title('EMG Left Vastus medialis  :  ' )
            xlabel('Time(s)')
            ylabel('Voltage (uV)')

            axes (handles.axes2);
            plot (t,emg2e_1,'b-','linewidth', 2);

            axes (handles.axes3);
            plot (t,emg3e_1,'b-','linewidth', 2);

            axes (handles.axes4);
            plot (t,emg4e_1,'b-','linewidth', 2);

        case 3
            axes (handles.axes1); 
            plot (t,emg1_1,'r-');
            hold on
            plot (t,emg1e_1,'b-','linewidth', 2);
            hold off

            axes (handles.axes2);
            plot (t,emg2_1,'r-');
            hold on
            plot (t,emg2e_1,'b-','linewidth', 2);
            hold off

            axes (handles.axes3);
            plot (t,emg3_1,'r-');
            hold on
            plot (t,emg3e_1,'b-','linewidth', 2);
            hold off

            axes (handles.axes4);
            plot (t,emg4_1,'r-');
            hold on
            plot (t,emg4e_1,'b-','linewidth', 2);
            hold off
    end

    a=get(handles.next_pushbutton,'Value')

    handles = guidata(hObject) 
    guidata(hObject,handles);   

->>>>>>>>>弹出菜单功能

    function popupmenu_CreateFcn(hObject, eventdata, handles)
    % hObject    handle to popupmenu (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    empty - handles not created until after all CreateFcns called

    % Hint: popupmenu 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

    set(hObject, 'String', {'EMG_abs','EMG_Envelope','EMG_abs + EMG_Envelope'})

set(hObject, 'String', {'EMG_abs','EMG_Envelope','EMG_abs + EMG_Envelope'})


我所有的代码都在这里,我不知道我所做的是否是在 Matlab 中编程的最佳方式,因为这是我正在制作的第一个 GUI,我有很多疑问。

谢谢

【问题讨论】:

  • 有人否决了您的问题,可能是因为它没有提供足够的细节。我想到了几个问题:你使用 GUIDE 吗?你需要你的按钮来做什么?只更改 4 个图表?您尝试了什么(我不知道您为什么尝试 while 循环)?您的数据是如何存储的?无论如何,我会创建一个变量(我们称之为handles.index),并且每个按下的后退/下一个按钮都会修改它的值(1、2、3 或4)。然后将运行一个函数来实现handles.index 值之后的图形。
  • 我仍然认为您的 GUI 上不需要有 4 个“页面”。按下这些上一个/下一个按钮之一后,简单地重新绘制图形会更简单。如果您不明白我的意思,您必须将您的图表代码添加到您的问题中,以便我们看到您如何显示它们。
  • 是的,我真的很想重绘。谢谢。附言。我的英语不太好,所以我无法更好地表达我的疑问。

标签: matlab matlab-guide


【解决方案1】:

让我更正您的代码,向您展示您所犯的小错误(我不是很有经验,但我认为这可以帮助您)。我没有尝试理解 textscan(...) 调用,因为我没有您的任何 txt 文件。

function Search_Patient_Callback(hObject, eventdata, handles)
    % global cell_data (I avoid global variables)
    [file, path]=uigetfile('*.txt', 'Select file');
    % arq = [path,file]; (it is the same as below, no ?)
    full_pathname=strcat(path,file);
    text=fileread(full_pathname);

    cell_data= textscan(text, repmat('%f',1,41),'headerlines',11);
    delimiter = '\t';
    startRow = 3;
    endRow = 8;
    formatSpec = '%*s%*s%*s%f%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%[^\n\r]';
    fileID = fopen(full_pathname,'r');
    textscan(fileID, '%[^\n\r]', startRow-1, 'ReturnOnError', false);
    dataArray = textscan(fileID, formatSpec, endRow-startRow+1, 'Delimiter', delimiter, 'ReturnOnError', false);
    time_foot1 = dataArray{:, 1};

    % Now store the relevant data in the handles structure :
    handles.timefoot=timefoot1;
    handles.cell_data=cell_data;
    guidata(hObject,handles);   % This line saves the current handles (so it saves the changes above)
    % handles = guidata(hObject); % This line replaces current handles with the one stored. Use it only in the beginning of a function that doesn't take handles as an input, or after a call to a function that affects handles.

然后

function Updating_Callback(hObject, eventdata, handles)
    y_data=0.001;
    t=(handles.cell_data{1,1})/1000; % else you can say 'cell_data=handles.cell_data;' and use 'cell_data' as you did
    %time_foot1= getappdata(0,'time_foot'); (let is use the handles structure instead of getappdata)
    time_foot1=handles.time_foot;
    text_rfoot=0.0009;
    text_lfoot=0.0007;

    [emg1_1 emg2_1 emg3_1 emg4_1  emg1e_1 emg2e_1 emg3e_1 emg4e_1]=emg_signal(handles.cell_data); % EMG RMS and EMG Envelope

    popup_sel_index = get(handles.popupmenu, 'Value');
    switch popup_sel_index
        [skipping a few lines]
    end

    % a=get(handles.next_pushbutton,'Value') (what for ??)

    % handles = guidata(hObject) (you do not want to erase the handles structure do you ?)
    guidata(hObject,handles); % OK but not neccesary because nothing changed in the handles structure in the function)

现在你想要做的是修改你的更新功能,以便它考虑到你推前/后推的次数。我认为。所以我会在 GUI OpeningFcn 内创建一个简单的变量 handles.index=1(不要忘记用 guidata 保存它),每次按下其中一个按钮时,您都会修改它的值(+1 或 -1) .然后在更新函数中,newIndex=mod(handles.index,4); 和 newIndex 告诉您必须显示哪个“平面”。

另一种解决方案是使用另一个带有字符串 {'1','2','3','4'} 的弹出菜单,并像您已经使用它一样使用它。

【讨论】:

  • 非常感谢!什么时候应该使用全局变量?我正在使用“time_foot1”,因为我必须在图表中显示患者的脚是罢工还是关闭,以便我可以看到 EMG 是否正常。但我发现的唯一方法是一直复制代码的一部分。有没有办法在轴中设置信息?
  • %Right-Foot Strike1 text(time_foot1(4),text_rfoot, ' Right-Strike 1', 'Color', 'k') line('XData',[time_foot1(4) time_foot1( 4)], 'YData', [0 y_data], 'LineStyle', '-','LineWidth', 1, 'Color','k'); %右脚离开 text(time_foot1(6),text_rfoot, 'Right-Off', 'Color', 'k') line('XData',[time_foot1(6) time_foot1(6)], 'YData', [0 y_data], 'LineStyle', '-','LineWidth', 1, 'Color','k');
  • 好吧,看来您每次更新轴时都必须绘制 textline,但如果您希望代码干净,您应该创建一个更新所有内容的新函数对于一个轴,这样您每次只需复制一行代码。例如update_one_axes(handles,current_axes_handle,right_off_yes_or_no)。玩得开心 ! :)
  • 非常感谢!祝你有美好的一天!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-26
  • 1970-01-01
  • 2019-01-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多