【问题标题】:Creating a GUI Listbox in matlab to load saved figures在 matlab 中创建 GUI 列表框以加载保存的图形
【发布时间】:2014-08-11 23:20:23
【问题描述】:

我想在我现有的 Matlab 代码中添加一个列表框。我希望在绘制图形的代码的第二个选项卡上显示此列表框。我希望在当前文件夹中使用 .bmp 保存的绘图填充列表框。我对 MATLAB gui 几乎没有经验,甚至无法显示列表框。

这是我当前的代码

 function PizanoGUI()
x=linspace(-2,2,100);
power=1;
y=x.^power;
ctrl_fh = figure; 

hPwr = uicontrol('Style','edit','Parent',... 
                     ctrl_fh,...
                     'Position',[45 100 100 20],...
                     'String',num2str(power),...
                     'CallBack',@pwrHandler);

hButton = uicontrol('Style','pushbutton','Parent',ctrl_fh,...  
                    'Position',[45 150 100 20],...
                    'String','Reset','Callback',@reset); 

hButton = uicontrol('Style','pushbutton','Parent',ctrl_fh,...  
                    'Position',[45 50 100 20],...
                    'String','EXIT','Callback',@close_Callback);                 

htext=uicontrol('Style', 'text',... 
'String', 'Welcome! Please Enter any value p to view the graph of y=x^p then press enter', ... 
'Position',[35,250,500,90], ... 
'FontSize',20);

function close_Callback(hObject, eventdata, handles)

close all;
end

function reset(source,event,handles,varargin) 
    fprintf('resetting...\n');
    power=1;
    set(hPwr,'String',num2str(power));
    y=x.^power;
    close(gcf);
    compute_and_draw_plot();
end

function pwrHandler(source,event,handles,varargin)     
    power=str2num(get(hPwr,'string'));
    fprintf('Setting power to %s\n',get(hPwr,'string'));
    close(gcf);
    compute_and_draw_plot();
end


function compute_and_draw_plot()
    plot_fh = figure;
    y=x.^power;
    figure(plot_fh); plot(x,y)
    xlabel('X axis') 
    ylabel('Y axis') 
    str = sprintf('Plot of Y=X^%d',power);
    title(str);

    hButton = uicontrol('Style','pushbutton','Parent',plot_fh,...  
                    'Position',[80 395 100 20],...
                    'String','Previous','Callback',@pushbutton1_Callback);



function pushbutton1_Callback(source, eventdata, handles)
    close(gcf);
    PizanoGUI();
end


 filename = inputdlg('Save figure as...');

 extensions = {'fig','bmp'};

 for k = 1:length(extensions)

saveas(gcf, filename{:}, extensions{k})

set(gcf,'PaperPositionMode','auto')

end
end
end

【问题讨论】:

    标签: matlab user-interface listbox


    【解决方案1】:

    好的,Dan,也许你正在寻找这样的东西?

    在与您的脚本相同的文件夹中创建一个文件夹并将其命名为图像,这是您保存图像的位置,然后可以轻松地再次加载它们。像这样创建列表框:

    files = dir('images');
    files(strncmp({files.name}, '.', 1)) = [];
    nbrOfFiles = length(files(not([files.isdir])));
    
    for i = 1:nbrOfFiles
        listBoxContent{i,1} = files(i).name;
    end
    
    handles.listbox1 = uicontrol('Style','listbox','Parent',...                                
                            handles.figure1,...
                            'Position',[100 100 500 50],...
                            'String',listBoxContent,...
                            'CallBack',@loadImage_Callback);
    

    而且回调看起来像这样:

    function loadImage_Callback(hObject, eventdata, handles)
    
     items = get(hObject,'String');
     index_selected = get(hObject,'Value');
    
     %More code...
    

    我不知道你想对图像做什么,但我猜你想以某种方式加载它们。也许你知道该怎么做。无论如何,“items(index_selected)”会给你选择的图像的名称。

    【讨论】:

    • 感谢您的帮助 Joakim。我用你的代码来创建列表框。我需要列表框来显示当前 matlab 文件夹中的文件。在我的程序运行后,用户将绘图的图像保存到当前的 matlab 文件夹中。我希望列表框列出这些保存的文件。我没有在列表框中显示图像 1、图像 2、图像 3,而是我保存的要显示的文件。我不确定我是否可以理解。我是 Matlab GUI 新手
    • 我对答案做了一些修改,看看有没有帮助。
    猜你喜欢
    • 1970-01-01
    • 2014-05-11
    • 2014-11-30
    • 2015-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-17
    • 2021-11-27
    相关资源
    最近更新 更多