Matlab GUI with activexcontrol Drop Down updates with Push button
2 次查看(过去 30 天)
显示 更早的评论
Hello, I am having such a hard time with getting an activexcontrol GUI to work. I am trying to make it so that a drop down selects from different items in a struct. This part is working. I am able to add an additional entry into the struct by pressing a button, but I don't really understand the code I made (edited several different example codes together). Also, the struct is not reloading into the popupbox when the button is pressed? Thanks, Sarah
function [] = GUI_22()
%Load transducer data and permute to correct form.
load transducer.mat
transducercell = struct2cell(transducer);
%Get the different categories of data seperately
names = permute(transducercell(1,:,:),[3 2 1])
bandwidth = permute(transducercell(2,:,:),[3 2 1])
numpts = permute(transducercell(3,:,:),[3 2 1])
low_freq = permute(transducercell(4,:,:),[3 2 1])
high_freq = permute(transducercell(5,:,:),[3 2 1])
names = names';
bandwidth = bandwidth';
numpts = numpts';
low_freq = low_freq';
high_freq = high_freq';
%Define the GUI figure.
S.fh = figure('units','pixels',...
'position',[600 650 600 600],...
'menubar','none',...
'name','GUI_22',...
'numbertitle','off',...
'resize','off');
%Define the popup Menu with prepopulated items from the transducer struct.
S.pop = uicontrol('style','pop',...
'units','pixels',...
'position',[100 100 260 40],...
'string',names);
%Define the edit boxes that will display the items in the struct that go with each transducer type.
S.ed = uicontrol('style','edit',...
'units','pix',...
'position',[50 300 260 30],...
'fontsize',16,'string','');
S.ed2 = uicontrol('style','edit',...
'units','pix',...
'position',[250 300 260 30],...
'fontsize',16,'string','');
S.ed3 = uicontrol('style','edit',...
'units','pix',...
'position',[50 250 260 30],...
'fontsize',16,'string','');
S.ed4 = uicontrol('style','edit',...
'units','pix',...
'position',[ 250 250 260 30],...
'fontsize',16,'string','');
S.ed5 = uicontrol('style','edit',...
'units','pix',...
'position',[ 50 200 260 30],...
'fontsize',16,'string','');
%Define the pushbutton to update the struct
S.pushbutton1 = uicontrol('style','pushbutton',...
'units','pix',...
'position',[ 50 150 260 30],...
'fontsize',16,'string','New Transducer Type');
set([S.pop,S.ed],{'callback'},{{@pp_call,S};{@ed_call_2,S}});
set([S.pop,S.ed2],{'callback'},{{@pp_call,S};{@ed_call_2,S}});
set([S.pop,S.ed3],{'callback'},{{@pp_call,S};{@ed_call_2,S}});
set([S.pop,S.ed4],{'callback'},{{@pp_call,S};{@ed_call_2,S}});
set([S.pop,S.ed5],{'callback'},{{@pp_call,S};{@ed_call,S}});
set([S.pop,S.pusbutton1],{'callback'},{{@pp_call,S};{@pushbutton1_call,S}});
function [] = pp_call(varargin)
% Callback for the popup.
S = varargin{3}; % Get the structure.
P = get(S.pop,{'string','val'}); % Get the users choice.
p_var = P{1,2}
load transducer.mat
transducercell = struct2cell(transducer);
%Get the different categories of data seperately
names = permute(transducercell(1,:,:),[3 2 1])
bandwidth = permute(transducercell(2,:,:),[3 2 1])
numpts = permute(transducercell(3,:,:),[3 2 1])
low_freq = permute(transducercell(4,:,:),[3 2 1])
high_freq = permute(transducercell(5,:,:),[3 2 1])
names = names';
bandwidth = bandwidth';
numpts = numpts';
low_freq = low_freq';
high_freq = high_freq';
bandwidth = bandwidth;
numpts = numpts;
names = names;
low_freq = low_freq;
high_freq = high_freq;
names_selection = names{1,p_var}
num_pts_selection = numpts{1,p_var}
bandwidth_selection = bandwidth{1,p_var}
low_freq_selection = low_freq{1,p_var}
high_freq_selection = high_freq{1,p_var}
numpts_selection = numpts{1,p_var}
set(S.ed,'string',P{1}{P{2}}); % Assign the user's choice to the edit.
set(S.ed2,'string',bandwidth_selection); % Assign the user's choice to the edit.
set(S.ed3,'string',low_freq_selection); % Assign the user's choice to the edit.
set(S.ed4,'string',high_freq_selection); % Assign the user's choice to the edit.
set(S.ed5,'string',numpts_selection ); % Assign the user's choice to the edit.
function [] = ed_call(varargin)
% Callback for the edit.
S = varargin{3}; % Get the structure.
E = get(S.ed,'string'); % Get the string from the edit.
P = get(S.pop,{'string','value'}); % Get the users choice.
% Check if edit string is found in pop-up list.
tmp = strmatch(E,P{1});
if ~isempty(tmp)
set(S.pop,'value',tmp) % Set the pop-up to match the edit.
else
% We could add the new element to the popup string at either the top or
% the bottom. Both methods are shown below. Only uncomment one of
% these at a time!
%Get the different categories of data seperately
load transducer.mat
transducercell = struct2cell(transducer);
names = permute(transducercell(1,:,:),[3 2 1])
bandwidth = permute(transducercell(2,:,:),[3 2 1])
numpts = permute(transducercell(3,:,:),[3 2 1])
low_freq = permute(transducercell(4,:,:),[3 2 1])
high_freq = permute(transducercell(5,:,:),[3 2 1])
names = names';
bandwidth = bandwidth';
numpts = numpts';
low_freq = low_freq';
high_freq = high_freq';
bandwidth = bandwidth;
numpts = numpts;
names = names;
low_freq = low_freq;
high_freq = high_freq;
%First add the new row to the struct
num = length(names)
num_plus_1 = num +1
%Get the edit boxes info
e1 = get(S.ed,{'string','val'})
e2 = get(S.ed2,{'string','val'})
e3 = get(S.ed3,{'string','val'})
e4 = get(S.ed4,{'string','val'})
e5 = get(S.ed5,{'string','val'})
S = guidata(gcbf); % Get the structure.
%set(S.tx,'str',get(S.ed,'string'));
S.STR = [];
guidata(gcbf,S)
%Update the transducer struct and resave the transducer struct.
transducer(num_plus_1).name = e1{1,1}
transducer(num_plus_1).bandwidth = e2{1,1}
transducer(num_plus_1).low_freq = e3{1,1}
transducer(num_plus_1).high_freq = e4{1,1}
transducer(num_plus_1).numpts = e5{1,1}
save('transducer.mat','transducer');
load transducer.mat
set(S.pop,'string',{P{1}{:},E},'value',length(P{1})+1) % Bottom
end
% %
function [] = ed2_call(varargin)
% Callback for editbox.
S = guidata(gcbf); % Get the structure.
set(S.tx,'str',get(S.ed2,'string'));
S.STR = [];
guidata(gcbf,S)
function [] = ed3_call(varargin)
% Callback for editbox.
S = guidata(gcbf); % Get the structure.
set(S.tx,'str',get(S.ed3,'string'));
S.STR = [];
guidata(gcbf,S)
function [] = ed4_call(varargin)
% Callback for editbox.
S = guidata(gcbf); % Get the structure.
set(S.tx,'str',get(S.ed4,'string'));
S.STR = [];
guidata(gcbf,S)
function [] = ed5_call(varargin)
% Callback for editbox.
S = guidata(gcbf); % Get the structure.
set(S.tx,'str',get(S.ed5,'string'));
S.STR = [];
guidata(gcbf,S)
function [] = ed_kpfcn(H,E)
% Keypressfcn for editbox
S = guidata(gcbf); % Get the structure.
if strcmp(E.Key,'backspace')
S.STR = S.STR(1:end-1);
elseif isempty(E.Character)
return
else
S.STR = [S.STR E.Character];
end
set(S.tx,'string',S.STR)
guidata(gcbf,S)
function [] = pushbutton_call(varargin)
% Callback for the popup.
S = varargin{3}; % Get the structure.
P = get(S.pop,{'string','val'}); % Get the users choice.
p_var = P{1,2}
load transducer.mat
transducercell = struct2cell(transducer);
%Get the different categories of data seperately
names = permute(transducercell(1,:,:),[3 2 1])
bandwidth = permute(transducercell(2,:,:),[3 2 1])
numpts = permute(transducercell(3,:,:),[3 2 1])
low_freq = permute(transducercell(4,:,:),[3 2 1])
high_freq = permute(transducercell(5,:,:),[3 2 1])
names = names';
bandwidth = bandwidth';
numpts = numpts';
low_freq = low_freq';
high_freq = high_freq';
bandwidth = bandwidth;
numpts = numpts;
names = names;
low_freq = low_freq;
high_freq = high_freq;
num_pts_selection = numpts{1,p_var}
bandwidth_selection = bandwidth{1,p_var}
low_freq_selection = low_freq{1,p_var}
high_freq_selection = high_freq{1,p_var}
numpts_selection = numpts{1,p_var}
set(S.ed,'string',P{1}{P{2}}); % Assign the user's choice to the edit.
set(S.ed2,'string',bandwidth_selection); % Assign the user's choice to the edit.
set(S.ed3,'string',low_freq_selection); % Assign the user's choice to the edit.
set(S.ed4,'string',high_freq_selection); % Assign the user's choice to the edit.
set(S.ed5,'string',numpts_selection ); % Assign the user's choice to the edit.
function [] = pushbutton1_call(varargin)
% Callback for the edit.
S = varargin{3}; % Get the structure.
E = get(S.ed,'string'); % Get the string from the edit.
P = get(S.pop,{'string','value'}); % Get the users choice.
% Check if edit string is found in pop-up list.
tmp = strmatch(E,P{1});
% set(S.ed,'str',get(S.ed,'string'));
% set(S.ed2,'str',get(S.ed2,'string'));
% set(S.ed3,'str',get(S.ed3,'string'));
% set(S.ed4,'str',get(S.ed4,'string'));
% set(S.ed5,'str',get(S.ed5,'string'));
if ~isempty(tmp)
set(S.pop,'value',tmp) % Set the pop-up to match the edit.
else
% We could add the new element to the popup string at either the top or
% the bottom. Both methods are shown below. Only uncomment one of
% these at a time!
%Get the different categories of data seperately
load transducer.mat
transducercell = struct2cell(transducer);
names = permute(transducercell(1,:,:),[3 2 1])
bandwidth = permute(transducercell(2,:,:),[3 2 1])
numpts = permute(transducercell(3,:,:),[3 2 1])
low_freq = permute(transducercell(4,:,:),[3 2 1])
high_freq = permute(transducercell(5,:,:),[3 2 1])
names = names';
bandwidth = bandwidth';
numpts = numpts';
low_freq = low_freq';
high_freq = high_freq';
%First add the new row to the struct
num = length(names)
num_plus_1 = num +1
%Get the edit boxes info
e1 = get(S.ed,{'string','val'})
e2 = get(S.ed2,{'string','val'})
e3 = get(S.ed3,{'string','val'})
e4 = get(S.ed4,{'string','val'})
e5 = get(S.ed5,{'string','val'})
S = guidata(gcbf); % Get the structure.
%set(S.tx,'str',get(S.ed,'string'));
S.STR = [];
guidata(gcbf,S)
%Update the transducer struct and resave the transducer struct.
transducer(num_plus_1).name = e1
transducer(num_plus_1).bandwidth = e2
transducer(num_plus_1).low_freq = e3
transducer(num_plus_1).high_freq = e4
transducer(num_plus_1).numpts = e5
save('transducer.mat','transducer');
load transducer.mat
set(S.pop,'string',{P{1}{:},E},'value',length(P{1})+1) % Bottom
% set(S.pop,'string',{E,P{1}{:}},'value',1) % Top
end
0 个评论
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Structures 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!