get_celldata.m 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. function [S, extra_args]=get_celldata(varargin)
  2. % [S, extra_args]=get_celldata(cellid)
  3. % [S, extra_args]=get_celldata(ratname,experimenter, date)
  4. % [S, extra_args]=get_celldata(ratname,experimenter, daterange)
  5. %
  6. % A frontend to get timestamps and waveforms from the spktimes table that does some nice input
  7. % parsing. Useful to use in other functions to avoid having to parse
  8. % inputs.
  9. %
  10. % S by default contains fields: cellid, sessiondate, and protocol_data
  11. %
  12. % needs more documentation!
  13. %
  14. % cellid can be a single cellid or a vector of cellids
  15. % date should be of the form "YYYY-MM-DD" or a relative date like -5
  16. % daterange should be a numeric vector in relative form like -10:-1 or a
  17. % cell array of date string of the from "YYYY-MM-DD"
  18. %
  19. if iscell(varargin{1})
  20. varargin=varargin{1};
  21. nargs=numel(varargin);
  22. else
  23. nargs=nargin;
  24. end
  25. %% parse inputs
  26. use_cellid=0;
  27. if nargs==1 && isnumeric(varargin{1})
  28. % Case 1, we've got a vector of cellids
  29. cellid=varargin{1};
  30. use_cellid=1;
  31. varargin=varargin(2:end);
  32. elseif nargs>=3
  33. % Case 2, we've got a ratname and experimenter
  34. ratname=varargin{1};
  35. experimenter=varargin{2};
  36. datein=varargin{3};
  37. if isnumeric(datein)
  38. %Case 2a, we've got relative dates (e.g. -10:0)
  39. for dx=1:numel(datein)
  40. dates{dx}=to_string_date(datein(dx));
  41. end
  42. elseif ischar(datein)
  43. %Case 2b, we've got a single date (e.g. '2009-05-01')
  44. dates{1}=datein;
  45. else
  46. %Case 2c, we've got a cell array of dates
  47. dates=datein;
  48. end
  49. % In future we might allow in extra parameters
  50. varargin=varargin(4:end);
  51. else
  52. S=[];
  53. warning('Failed to parse inputs.');
  54. extra_args=varargin;
  55. return
  56. end
  57. extra_args=varargin;
  58. %% get data from sql
  59. if ~use_cellid
  60. % If we are not in Case 1 (see above)
  61. % then transform the cell array of strings into a long comma separated
  62. % string.
  63. datestr='';
  64. for dx=1:numel(dates)
  65. datestr=[datestr ',"' dates{dx} '"'];
  66. end
  67. % Use the datestr for a select ... where sessiondate in (datestr) type sql command to get all the relevant sessions.
  68. sqlstr=['select c.cellid, recorded_on_right, region,ts, wave, c.sessid, single from cells k, spktimes as c, ratinfo.eibs e, sessions as s '...
  69. ' where s.sessid=c.sessid and k.cellid=c.cellid and c.eibid=e.eibid and s.ratname="{S}" and experimenter="{S}" and '...
  70. ' sessiondate in (' datestr(2:end) ') order by sessiondate'];
  71. [S.cellid, S.rr ,S.region, S.ts, S.wave, S.sessid, S.single]=bdata(sqlstr,ratname, experimenter);
  72. else
  73. % We have a list of cellids. Transform that into a comman seperated string
  74. cellstr='';
  75. for cx=1:numel(cellid)
  76. cellstr=[cellstr, ',' num2str(cellid(cx))];
  77. end
  78. [S.cellid, S.rr, S.region, S.ts, S.wave, S.sessid, S.single]=bdata(['select s.cellid, recorded_on_right, region, ts, wave, s.sessid, single from spktimes s, cells c, ratinfo.eibs e where s.cellid=c.cellid and c.eibid=e.eibid and s.cellid in (' cellstr(2:end) ') order by sessid']);
  79. end
  80. %% get some metadata