Sunday, July 30, 2017

[Matlab GUI] Build simple Data Processing tool - 2/5

2. Load mat file in structure array, and Display file list loaded on the listbox

We here create the function which loads *.mat file in structure array type. The button 'Load' shown below will call this function.

Before adding the function into the button 'Load', some setting should be done in advance.
1) Right-click on the button 'Load'.
2) Select the menu 'Property Inspector'. Then, the figure below will appear.
3) Change the Tag name on the Property Inspector window above.
As in the same way, change the Tag name of the Listbox.

4) In the pop-up menu appearing by the right-click on the button 'Load',
select the menu 'View Callbacks > Callback'. Then, the corresponding function is added and m-script appears on the screen as below.
Here, the tag name defined at 3) becomes the name of callback function.

 * Create a load callback function
1) To open and select *.mat files, the file directory is necessary.
It is easily obtained using the Matlab function 'uigetdir'.

directory = uigetdir(' ','Select a folder');

2) Get file list

result = dir(directory);
allist = struct2cell(result);
s=size(allist); n=s(2)-2; list=cell(1,n);
for N=1:1:n
    list{1,N} = allist{1,N+2};
end

3) Filtering the list

matfile = ~cellfun(@isempty,strfind(list,'.mat'));
mf = double(matfile);
total = logical(mf);
list = list(total);

4) Display the filtered list
Supposing that the Tag name of the Listbox is 'FileList', file list can be displayed using the command below.

set(handles.FileList,'String',list);

After all, the code looks as following.
%
directory = uigetdir(' ','Select a folder');
%
result = dir(directory);
allist = struct2cell(result);
s=size(allist); n=s(2)-2; list=cell(1,n);
for N=1:1:n
    list{1,N} = allist{1,N+2};
end
%
matfile = ~cellfun(@isempty,strfind(list,'.mat'));
mf = double(matfile);
total = logical(mf);
list = list(total);
%
set(handles.FileList,'String',list);


Related posts
0. Build simple plot tool.
1. Create a tool layout in GUIDE
2. Load mat file in structure array, and Display the file list loaded on the listbox
3. Display variables of mat file, and Plot variables on the axes
4. How to add new plot to the existing plot, and Use dynamic legend
5. Clear the axes, and Move the figure to new figure (Undock)

No comments:

Post a Comment