在matlab中创建GUI列表框以加载已保存的数字

时间:2014-08-11 23:20:23

标签: matlab user-interface listbox

我想在我现有的Matlab代码中添加一个Listbox。我希望这个列表框位于我的代码的第二个选项卡上,其中绘制了图表。我希望列表框中填充当前文件夹中的.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

1 个答案:

答案 0 :(得分:0)

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

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

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)”将为您提供所选图像的名称。