如何在matlab中将变量写入编辑框

时间:2014-10-14 21:01:29

标签: matlab user-interface

所以我只是尝试选择一个文件夹并在Matlab的GUI中编写一个编辑框的路径。

但在那里的某个地方,我完全搞砸了。

根据我所看到的一切,这看起来应该有效。

我包含了一大块GUI创建,以防错误出现在某处。

我对Matlab很新,所以这对我来说是第一次尝试。

%######################################################################
%Main Figure
%######################################################################
    %Used to center figure on screen
    sz = [600 700]; %figure size
    screensize = get(0,'ScreenSize'); %get screen size
    xpos = ceil((screensize(3)-sz(2))/2); %x position
    ypos = ceil((screensize(4)-sz(1))/2); %Y position

h = figure('units','pixels',...
              'position',[xpos ypos sz(1) sz(2)],...
              'menubar','none',...
              'toolbar','none',...
              'name','VAST TOOL MOCKUP',...
              'resize','off',...
              'numbertitle','off');

%Menu Items
m = uimenu(h,'Label','File');
aboutm = uimenu(m,'Label','About','callback',@about);
docm = uimenu(m,'Label','Documentation','callback',@documentation);
% loadm = uimenu(m,'Label','Load Data','callback','load');
exitm = uimenu(m,'Label','Exit','callback',@exit);

rightcolumn = 310;
    leftcolumn = 70;
    labellength = 200;
    labelalign = 'center';
    labelvalign = 'Middle';

    pbtWorking = uicontrol('style','push',...
                            'units','pix',...
                            'position',[rightcolumn+200 600 30 30],...
                            'string','...',...
                            'fontweight','bold',...
                            'fontsize',11,...
                            'callback',@pbtWorking_CB);


    editWorking = uicontrol ('style','edit',...
                        'units','pix',...
                        'position',[rightcolumn 600 200 30],...
                        'string','',...
                        'fontweight','bold',...
                        'fontsize',11);

这是给我错误的部分。

使用VAST_alpha时出错> pbtWorking_CB(第311行) 没有足够的输入参数。

评估UIControl回调时出错

function pbtWorking_CB(hObject,eventdata,handles)
    WD_filepath = uigetdir('Select a Working Directory...')

    if WD_filepath ~=0
        set(handles.editWorking,'String',WD_filepath)
    else
        set(handles.editWorking,'String',' ')
    end

我一直试图修复它已经很长时间了,我可以告诉它将是一个我完全忽略的超级简单修复。

提前谢谢!

1 个答案:

答案 0 :(得分:0)

我冒昧地修改了一下你的GUI,它现在运行正常。以下是我改变的事情:

1)在图形的句柄结构中存储GUI组件,以便更容易检索/更改/更改。使用此句柄结构,可以非常轻松地在GUI的回调之间共享数据。我认为错误是由于Matlab没有将句柄作为输入引起的,因为它们从未被定义过或类似的东西。

2)在函数pbtWorking_CB中,我更改了strcmp的if / else语句,在我看来这更直观。无论如何,请尝试这个,看看它是否适合你:

function TestGUIListBox

%Used to center figure on screen
sz = [600 700]; %figure size
screensize = get(0,'ScreenSize'); %get screen size
xpos = ceil((screensize(3)-sz(2))/2); %x position
ypos = ceil((screensize(4)-sz(1))/2); %Y position



%// Create the handles structure associated with the figure. Every handles is stored in it and is thus accessible with the right calls to guidata and such. Please see below.
    handles.figure = figure('units','pixels',...
    'position',[xpos ypos sz(1) sz(2)],...
    'menubar','none',...
    'toolbar','none',...
    'name','VAST TOOL MOCKUP',...
    'resize','off',...
    'numbertitle','off');

%Menu Items
m = uimenu(handles.figure,'Label','File');
aboutm = uimenu(m,'Label','About','callback',@about);
docm = uimenu(m,'Label','Documentation','callback',@documentation);
% loadm = uimenu(m,'Label','Load Data','callback','load');
exitm = uimenu(m,'Label','Exit','callback',@exit);

rightcolumn = 310;
leftcolumn = 70;
labellength = 200;
labelalign = 'center';
labelvalign = 'Middle';

%// Store GUI components in handles structure...
handles.pbtWorking = uicontrol('style','push',...
    'units','pix',...
    'position',[rightcolumn+200 600 30 30],...
    'string','...',...
    'fontweight','bold',...
    'fontsize',11,...
    'callback',@pbtWorking_CB);


handles.editWorking = uicontrol ('style','edit',...
    'units','pix',...
    'position',[rightcolumn 600 200 30],...
    'string','',...
    'fontweight','bold',...
    'fontsize',11);

%// Update structure...IMPORTANT
guidata(handles.figure, handles);

function pbtWorking_CB(hObject,handles)

handles=guidata(gcf); %// Retrieve handles structure to access the components...i.e. the edit box in this case

WD_filepath = uigetdir('Select a Working Directory...')


   %// Actually you could skip the if/else statement altogether since the edit box is filled with wathever WD_filepath is, even if it's empty.
if ~strcmp(WD_filepath,'')
    set(handles.editWorking,'String',WD_filepath)
else
    set(handles.editWorking,'String',' ')
end