Azzera filtri
Azzera filtri

GUI listbox: problem to load/initialize the list from a data file?

3 visualizzazioni (ultimi 30 giorni)
Hello Everybody,
I would be very grateful to get your help... I'm trying to create (from an empty listbox UI) a list coming from an ascii file. It works ok, except that I have to click on the first empty line of the listbox UI to make appear my "new" string list in the listbox. I would like to have it appears straight after the data file opening.
Note that the line : "data=ImportTxtData_Function(filename)" is a function importing/storing a structure containing the "data.datasetNameFields" which is a cell containing the list of strings (to be displayed in the listbox UI).
I attached the main "simple_ListBox_Test_V2.m", the "ImportTxtData_Function.m" and an the txt file from where the data are imported...
Here is the main GUI code:
function simple_ListBox_Test()
% Click "Open file" to download the list from the ascii file, then
% the list of strings is supposed to appear in the listbox UI
clc
%clear all; close all;
% Create and then hide the GUI as it is being constructed. Position
% give the position and size of the GUI interface
hFig = figure('Visible','off','Position',[560,800,300,600]);%, 'ToolBar', 'none');
% create structure of handles
myhandles = guihandles(hFig); % contains at this stage only the initial handles of the figure
% Save the structure
guidata(hFig,myhandles) ;
%%%Create a new toolbar with factory icons
hFig.ToolBar = 'none'; %%hide the standard toolbar
hNewToolbar = uitoolbar ('parent',hFig) ;
hButton=uitoolfactory(hNewToolbar,'Standard.EditPlot');
hButton=uitoolfactory(hNewToolbar,'Exploration.Rotate');
hButton=uitoolfactory(hNewToolbar,'Annotation.InsertColorbar');
set(hButton,'Separator','on');
hButton=uitoolfactory(hNewToolbar,'Exploration.ZoomIn');
hButton=uitoolfactory(hNewToolbar,'Exploration.ZoomOut');
set(hButton,'Separator','on');
% Assign the GUI a name to appear in the window title.
hFig.Name = 'Simple GUI';
% Move the GUI to the center of the screen.
movegui(hFig,'center')
% Make the GUI visible.
hFig.Visible = 'on';
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Construct the components.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
OpenTifFile = uicontrol(hFig, 'Style','pushbutton',...
'String','Open File',...
'Position',[100,550,70,25],...
'Callback',@OpenTifFilebutton_Callback);
hVariableListBox = uicontrol(hFig, 'Style','listbox',...
'String',{[]},...
'Position',[50,50,200,450],...
'Callback',@VariableListBox_menu_Callback);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Callbacks for simple_gui.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function OpenTifFilebutton_Callback(hObject, eventdata, handles)
% GUI file opening
disp('File selection in progress')
[filename, pathname] = uigetfile('*.txt', 'Select a .txt file');
if isequal(filename,0)
disp('File selection cancelled')
return
else
disp(['User selected ', fullfile(pathname, filename)])
filename=strcat(pathname, '\',filename);
end
fid = fopen(filename);
if fid==-1
error(['File ' filename ' not found']);
end
data=ImportTxtData_Function(filename);
% Get the structure using guidata in the local function
myhandles = guidata(gcbo);
% Add my data to the current handle structure
myhandles.data=data;
% Save the change made to the structure, i.e. adding data struct in myhandles
guidata(gcbo, myhandles) ;
end
function VariableListBox_menu_Callback(hObject, eventdata, handles)
% Get the structure using guidata in the local function
myhandles = guidata(gcbo);
VariableList= myhandles.data.datasetNameFields(1:end);
% update the list box with the new selection
set(hVariableListBox,'String', VariableList)
% Get the number of the item which is click in the listbox
SelectedVariableNo = get(hVariableListBox, 'value')
% Store the string name of the selected variable
SelectedVariableName= VariableList(SelectedVariableNo)
end
end

Risposte (1)

Jan
Jan il 23 Nov 2016
All you have to do is updating the strings as in VariableListBox_menu_Callback:
data=ImportTxtData_Function(filename);
VariableList = data.datasetNameFields; % Without the unneeded "(1:end)"
set(hVariableListBox, 'String', VariableList);
Updating the string in the callback of the listbox is not useful. So remove the code there.
Notes:
1. Better: Prefer fullfile in every case:
disp(['User selected ', fullfile(pathname, filename)])
filename=strcat(pathname, '\',filename);
==>
filename = fullfile(pathname, filename);
disp(['User selected ', filename])
Do not insert filenames in the error message directly:
error(['File ' filename ' not found']);
This will have unexpected results, if the name contains a '%' or other control sequences. Safe:
error('File %s not found', filename);
"1:end" wastes time only:
VariableList = myhandles.data.datasetNameFields(1:end)
==>
VariableList = myhandles.data.datasetNameFields;

Categorie

Scopri di più su Dialog Boxes in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by