Skip to content
Snippets Groups Projects
Commit 88261ca1 authored by Laura Nervo's avatar Laura Nervo
Browse files

gtDBLoadTable : load a general table, given optional sorting variable and order.


Signed-off-by: default avatarLaura Nervo <laura.nervo@esrf.fr>
parent 5f44ffab
No related branches found
No related tags found
No related merge requests found
function [db_info,db_nan] = gtDBLoadTable(table,sort_var,sort_order,fill_empties)
% GTDBLOADTABLE Gets content of a table in the database, sorting values
% according to sort_var in the sort_order
% [db_info,db_nan] = gtDBLoadTable(table,[sort_var],[sort_order],[fill_empties])
% ------------------------------------------------------------------------------
% fill_empties flag is used to fill empty key values for all the table
% columns (useful for difspot table for example, where some blobs are
% not converted into spots).
%
% INPUT:
% table = <string> table name
% sort_var = <string> column to sort table {auto_increment column}
% sort_order = <string> sorting order {'asc'} | 'desc'
% fill_empties = <logical> {true} | false
%
% OUTPUT:
%
% db_info = <struct> table content
% db_nan = <struct> table NaN-entries indexes for each column
%
% Version 001 09-01-2014 by LNervo
if ~exist('sort_order','var') || isempty(sort_order)
sort_order = 'asc';
end
if ~exist('fill_empties','var') || isempty(fill_empties)
fill_empties = true;
end
gtDBConnect();
% check if table exists
check=mym(['SHOW tables LIKE "' table '"']);
if ~isempty(check)
% get column names and values
[columns,~,~,~,~,values] = mym(['SHOW columns FROM ' table]);
if ~isempty(columns)
% set sorting variable
if ~exist('sort_var','var') || isempty(sort_var)
index = findStringIntoCell(values,{'auto_increment'});
if isempty(index)
index = 1;
end
sort_var = columns{index};
else
index = findStringIntoCell(columns,{sort_var});
if isempty(index)
disp('wrong sort_var name...Quitting')
return
end
end
else
disp('table is empty...Quitting')
return
end
disp(['sorting by ' sort_var ' ' sort_order])
% interrogate database
mysqlcmd = ['SELECT * FROM ' table ' ORDER BY ' sort_var ' ' sort_order];
[values{:}] = mym(mysqlcmd);
% get structure
db_info = cell2struct(values,columns,1);
if (fill_empties)
% create vectors of size (numspots,1) (these will have some empty elements since
% not all difspotIDs exist (some are filtered out because fo size
% restrictions, for example)
numentries = max(values{index});
tmpTab = structfun(@(x) NaN(numentries,1), db_info, 'UniformOutput', false);
% indexes of keys
tmp = values{index};
for ii=1:length(columns)
tmpTab.(columns{ii})(tmp) = values{ii};
end
db_info = tmpTab;
% update values
values = struct2cell(db_info);
end
else
db_info = [];
db_nan = [];
disp('table does not exist...Quitting')
return
end
if nargout > 1
db_nan = cellfun(@(num) find(isnan(num)), values, 'UniformOutput', false);
db_nan = cell2struct(db_nan,columns,1);
end
end % end of function
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment