Adding a List box to my Matlab GUI program. I want the list box to be populated with image files that were previously saved in the current folder.

2 ビュー (過去 30 日間)
Dan
Dan 2014 年 8 月 12 日
回答済み: Yucheng Ma 2014 年 8 月 19 日
I want to add a Listbox to my exsisting Matlab code. I want this listbox on the second tab of my code where the graph is plotted. I want the listbox to be populated with .bmp saved plots in the current folder. I have little experience with MATLAB gui and I am having trouble even displaying the list box.
Here is my current code
if true
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 件)

Yucheng Ma
Yucheng Ma 2014 年 8 月 19 日
It is my understanding that you would like to create a List Box and fill it with the names of the BMP files in the current directory.
You can use the following statement to create a List Box:
hListBox = uicontrol('Style', 'listbox', ...
'Parent', plot_fh, ...
'Position', [100, 100, 50, 50], ...
'String', listString, ...
'Callback', @ListBoxCallback);
Please note that the position of the List Box must be within the current figure window to be displayed.
The "listString" above is a cell array of strings, specifying the texts displayed in the List Box. You can get a list of the names of the BMP files in the current directory as follows:
function listString = getBMPFiles()
bmpfiles = dir('*.bmp');
listString = cell(1, numel(bmpfiles));
[listString{:}] = bmpfiles.name;
end
You can refer to the following document for more information on "dir":
In order to update the contents of the List Box after a figure is saved, you can get the new "listString" with the function above and use "set" to change the "String" property of the List Box:
set(hListBox, 'String', listString);
You can refer to the following documentation for a more detailed example of using the List Box in MATLAB UI: http://www.mathworks.com/help/matlab/creating_guis/interactive-list-box-in-a-guide-gui.html

カテゴリ

Help Center および File ExchangeEnvironment and Settings についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by