How do I link a Pushbutton and a List box?

11 vues (au cours des 30 derniers jours)
Anand  Gudlur
Anand Gudlur le 14 Juin 2015
Hello People, I am starter with Matlab and I want link a pushbutton and List box. For example: If I load (a .mat file or any file format compatible with matlab ) using push button it should populate the list box? Below I have Copied my listbox and pushbutton command. It would be of great if someone could help.
Thanks Anand Gudlur
function listbox1_CreateFcn(hObject, eventdata, handles)
% hObject handle to listbox1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: listbox controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
d = uigetfile;
load(d);
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

Réponses (1)

Walter Roberson
Walter Roberson le 14 Juin 2015
function pushbutton2_Callback(hObject, eventdata, handles)
[filename, pathstr] = uigetfile('Which .mat?', '*.mat'));
if ~ischar(filename); return; end %cancelled
fullname = fullfile(pathstr, filename);
%don't just load() a .mat file; it could clobber internal variables
data = load(fullname); %assign load() result to variable
%we need something to write into the list box. How about the names
%of the variables in the .mat file?
varnames = fieldnames(data);
what_to_put_in_list = cellstr(varnames);
set(handles.listbox1, 'String', what_to_put_in_list);
%save the loaded data where it can be retrieved
handles.datafromfile = data;
%update master copy of handles information
guidata(hObject, handles);
  3 commentaires
Walter Roberson
Walter Roberson le 15 Juin 2015
I am not sure if the "without defining its path" applies to the .mat file or applies to the listbox?
Your code has uigetfile() already. uigetfile() will automatically request the directory name as well as the file name if you request two outputs; it is not something the user has to worry about. It is not strictly necessary to pay attention to the directory and build the complete file name before doing the load(), but why not give the user the flexibility of switching directories? The coding cost is very small.
If you are going to update a listbox then you need some way of indicating which listbox you are going to update. In my sample I used the fixed "handles.listbox1", corresponding to the function listbox1_CreateFcn you gave the header code for.
In order for a listbox to update itself, it is necessary for the listbox callback to be invoked by something. It is possible for the pushbutton to invoke the listbox callback. But remember that the listbox callback would also be invoked when the user makes a selection from the listbox, so you would need to somehow signal that you were initializing rather than that the user made a selection. It is possible, but it is unnecessarily complicated. What I would suggest is that you put the code to populate the listbox and put it into a function, and then invoke the function from the pushbutton that loads the file.
Perhaps for "defining its path" you were talking about specifying the variable name to load? .mat files always store the variable name even if only one value is stored in them. If you know the variable name you want ahead of time, you could use that instead of examining what was returned. For example,
data = load(fullname);
z = data.zvals; %if it is always the "zvals" variable that you want
Pavel Chatterjee
Pavel Chatterjee le 7 Mai 2018
I am trying to populate the listbox of another GUI after this button has been pressed. Any ideas?

Connectez-vous pour commenter.

Catégories

En savoir plus sur Adding custom doc dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by