使用 GUIDE 的 MATLAB GUI:列表框问题

发布于 2024-07-17 13:04:26 字数 2201 浏览 5 评论 0原文

我正在创建一个 MATLAB GUI,其中包含两个 uicontrol 对象:一个按钮和一个列表框。 我使用按钮将文件名添加到列表框中。 当我从 m 文件运行 GUI 时,它工作正常。 仅当我运行 .fig 文件本身时才会出现此问题。 这是回调代码和错误:

function add_file_Callback(hObject, eventdata, handles)
% hObject    handle to add_file (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
%gets input file(s) from user

[input_file,pathname] = uigetfile( ...
       {'*.jpg;*.tif;*.png;*.gif;*.bmp;*.pgm'}, ...
        'Select files', ... 
        'MultiSelect', 'on');

%if file selection is cancelled, pathname should be zero
%and nothing should happen
if pathname == 0
    return
end

%gets the current data file names inside the listbox

inputFileNames = get(handles.img_list,'String');

%if they only select one file, then the data will not be a cell
%if more than one file selected at once,
%then the data is stored inside a cell
if iscell(input_file) == 0

    %add the most recent data file selected to the cell containing
    %all the data file names
    inputFileNames{end+1} = input_file;

%else, data will be in cell format
else
    %stores full file path into inputFileNames
    for n = 1:length(input_file)
        %notice the use of {}, because we are dealing with a cell here!
        inputFileNames{end+1} = input_file{n};
    end
end

%updates the gui to display all filenames in the listbox
set(handles.img_list,'String',inputFileNames);

%make sure first file is always selected so it doesn't go out of range
%the GUI will break if this value is out of range
set(handles.img_list,'Value',1);

% Update handles structure
guidata(hObject, handles);

错误:

Error in ==> Texture_Classification_GUI>add_file_Callback at 154
inputFileNames = get(handles.img_list,'String');

Error in ==> gui_mainfcn at 95
        feval(varargin{:});

Error in ==> Texture_Classification_GUI at 42
    gui_mainfcn(gui_State, varargin{:});

??? Error using ==> Texture_Classification_GUI('add_file_Callback',gcbo,[],guidata(gcbo))
Attempt to reference field of non-structure array.

??? Error while evaluating uicontrol Callback

任何帮助将不胜感激。

I'm creating a MATLAB GUI that contains two uicontrol objects: a pushbutton and a listbox. I use the pushbutton to add file names to the listbox. When I run the GUI from the m-file it works fine. The problem occurs only when I run the .fig file itself. Here is the callback code and the error:

function add_file_Callback(hObject, eventdata, handles)
% hObject    handle to add_file (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
%gets input file(s) from user

[input_file,pathname] = uigetfile( ...
       {'*.jpg;*.tif;*.png;*.gif;*.bmp;*.pgm'}, ...
        'Select files', ... 
        'MultiSelect', 'on');

%if file selection is cancelled, pathname should be zero
%and nothing should happen
if pathname == 0
    return
end

%gets the current data file names inside the listbox

inputFileNames = get(handles.img_list,'String');

%if they only select one file, then the data will not be a cell
%if more than one file selected at once,
%then the data is stored inside a cell
if iscell(input_file) == 0

    %add the most recent data file selected to the cell containing
    %all the data file names
    inputFileNames{end+1} = input_file;

%else, data will be in cell format
else
    %stores full file path into inputFileNames
    for n = 1:length(input_file)
        %notice the use of {}, because we are dealing with a cell here!
        inputFileNames{end+1} = input_file{n};
    end
end

%updates the gui to display all filenames in the listbox
set(handles.img_list,'String',inputFileNames);

%make sure first file is always selected so it doesn't go out of range
%the GUI will break if this value is out of range
set(handles.img_list,'Value',1);

% Update handles structure
guidata(hObject, handles);

Error:

Error in ==> Texture_Classification_GUI>add_file_Callback at 154
inputFileNames = get(handles.img_list,'String');

Error in ==> gui_mainfcn at 95
        feval(varargin{:});

Error in ==> Texture_Classification_GUI at 42
    gui_mainfcn(gui_State, varargin{:});

??? Error using ==> Texture_Classification_GUI('add_file_Callback',gcbo,[],guidata(gcbo))
Attempt to reference field of non-structure array.

??? Error while evaluating uicontrol Callback

Any help would be much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

最丧也最甜 2024-07-24 13:04:26

运行 Fig 文件本身”是什么意思? GUIDE 创建两个文件:一个 m-file 和一个 .fig 文件(例如 my_guide_app.mmy_guide_app.fig)。 您是否使用诸如 openfig 之类的内容打开 .fig ? 这是行不通的,因为m-file需要设置创建句柄结构的图形打开函数。 因此,要运行使用 GUIDE 制作的 GUI,需要调用 m-file 来启动应用程序,而不仅仅是打开 .fig 文件。

如果我误解了您关于打开 .fig 文件的声明,请告诉我,因为可能还有其他问题。

What do you mean by "running the fig file itself"? GUIDE creates two files: an m-file and a .fig file (for example my_guide_app.m and my_guide_app.fig). Are you opening the .fig with something like openfig? This won't work because the m-file needs to set the figures opening function which creates the handle structure. So to run GUI's made with GUIDE it is necessary to call the m-file to launch the application and not just open the .fig file.

Let me know if I misinterpreted your statement about opening the .fig file, because there could be something else wrong.

禾厶谷欠 2024-07-24 13:04:26

我猜列表框需要在 GUIDE 中初始化。 如果使用 1 个选项初始化它,它将是一个 char 数组,如果使用超过 1 个选项初始化它,它将是一个元胞数组。 因此,您还必须在那里进行类似的检查(iscell); 然后添加新选项。

Listbox needs to be initialized in the GUIDE I guess. If you initialize it with 1 option, it will be a char array, if you initialize it with more than 1 option, it will be a cell array. So, you have to put a similar check there (iscell) as well; and then add the new options.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文