|
176 | 176 | dims = obj.dims; |
177 | 177 | rank = length(dims); |
178 | 178 | for i = 1:length(varargin) |
179 | | - if ischar(varargin{i}) |
| 179 | + ind = varargin{i}; |
| 180 | + if ischar(ind) || isempty(ind) |
180 | 181 | continue; |
181 | 182 | end |
182 | | - validateattributes(varargin{i}, {'numeric'}, {'vector', '<=', dims(i)}); |
| 183 | + validateattributes(ind, {'numeric'}, {'vector', '<=', dims(i)}); |
183 | 184 | end |
184 | 185 | shapes = getShapes(varargin, dims); |
185 | 186 |
|
|
208 | 209 | shapeInd(iterateInd) = shapeInd(iterateInd) + 1; |
209 | 210 | shapeInd(1:(iterateInd-1)) = 1; |
210 | 211 | end |
211 | | - |
212 | | - memSize = getMemSize(varargin, dims); |
| 212 | + memSize = zeros(1, rank); |
| 213 | + for i = 1:rank |
| 214 | + for j = 1:length(shapes{i}) |
| 215 | + Selection = shapes{i}{j}; |
| 216 | + if isa(Selection, 'types.untyped.datastub.shape.Point') |
| 217 | + memSize(i) = memSize(i) + 1; |
| 218 | + else |
| 219 | + memSize(i) = memSize(i) + Selection.length; |
| 220 | + end |
| 221 | + end |
| 222 | + end |
213 | 223 | memSid = H5S.create_simple(length(memSize), fliplr(memSize), []); |
214 | 224 | % read data. |
215 | 225 | fid = H5F.open(obj.filename); |
|
220 | 230 | H5S.close(memSid); |
221 | 231 | H5S.close(sid); |
222 | 232 |
|
| 233 | + expectedSize = dims; |
| 234 | + for i = 1:length(varargin) |
| 235 | + if ~ischar(varargin{i}) |
| 236 | + expectedSize(i) = length(varargin{i}); |
| 237 | + end |
| 238 | + end |
| 239 | + |
| 240 | + if ischar(varargin{end}) |
| 241 | + % dangling ':' where leftover dimensions are folded into |
| 242 | + % the last selection. |
| 243 | + selDimInd = length(varargin); |
| 244 | + expectedSize = [expectedSize(1:(selDimInd-1)) prod(dims(selDimInd:end))]; |
| 245 | + else |
| 246 | + expectedSize = expectedSize(1:length(varargin)); |
| 247 | + end |
| 248 | + |
| 249 | + if isscalar(expectedSize) |
| 250 | + expectedSize = [1 expectedSize]; |
| 251 | + end |
| 252 | + |
| 253 | + selections = varargin; |
| 254 | + openSelInd = find(cellfun('isclass', selections, 'char')); |
| 255 | + for i = 1:length(openSelInd) |
| 256 | + selections{i} = 1:dims(i); |
| 257 | + end |
| 258 | + data = reorderLoadedData(data, selections); |
| 259 | + data = reshape(data, expectedSize); |
| 260 | + |
| 261 | + function reordered = reorderLoadedData(data, selections) |
| 262 | + % dataset loading does not account for duplicate or unordered |
| 263 | + % indices so we have to re-order everything here. |
| 264 | + % we presume data is the indexed values of a unique(ind) |
| 265 | + if isempty(data) |
| 266 | + reordered = data; |
| 267 | + return; |
| 268 | + end |
| 269 | + |
| 270 | + indKey = cell(size(selections)); |
| 271 | + isSelectionNormal = false(size(selections)); % that is, without duplicates or out of order. |
| 272 | + for i = 1:length(indKey) |
| 273 | + indKey{i} = unique(selections{i}); |
| 274 | + isSelectionNormal = isequal(indKey{i}, selections{i}); |
| 275 | + end |
| 276 | + if all(isSelectionNormal) |
| 277 | + reordered = data; |
| 278 | + return; |
| 279 | + end |
| 280 | + indKeyIndMax = cellfun('length', indKey); |
| 281 | + if isscalar(indKeyIndMax) |
| 282 | + reordered = repmat(data(1), indKeyIndMax, 1); |
| 283 | + else |
| 284 | + reordered = repmat(data(1), indKeyIndMax); |
| 285 | + end |
| 286 | + indKeyInd = ones(size(selections)); |
| 287 | + while true |
| 288 | + selInd = cell(size(selections)); |
| 289 | + for i = 1:length(selections) |
| 290 | + selInd{i} = selections{i} == indKey{i}(indKeyInd(i)); |
| 291 | + end |
| 292 | + indKeyIndArgs = num2cell(indKeyInd); |
| 293 | + reordered(selInd{:}) = data(indKeyIndArgs{:}); |
| 294 | + indKeyIndNextInd = find(indKeyIndMax ~= indKeyInd, 1, 'last'); |
| 295 | + if isempty(indKeyIndNextInd) |
| 296 | + break; |
| 297 | + end |
| 298 | + indKeyInd(indKeyIndNextInd) = indKeyInd(indKeyIndNextInd) + 1; |
| 299 | + indKeyInd((indKeyIndNextInd+1):end) = 1; |
| 300 | + end |
| 301 | + end |
| 302 | + |
223 | 303 | function shapes = getShapes(selections, dims) |
224 | 304 | rank = length(dims); |
225 | 305 | shapes = cell(1, rank); % cell array of cell arrays of shapes |
|
240 | 320 | end |
241 | 321 | end |
242 | 322 | end |
243 | | - |
244 | | - function memSize = getMemSize(selections, dims) |
245 | | - % replace dims with number of selections |
246 | | - indexSelections = find(~cellfun('isclass', selections, 'char')); |
247 | | - vararginSizes = cellfun('length', selections); |
248 | | - dims(indexSelections) = vararginSizes(indexSelections); |
249 | | - |
250 | | - % case where varargin rank is smaller than actual data |
251 | | - % space rank. |
252 | | - selectionRank = length(selections); |
253 | | - fileSpaceRank = length(dims); |
254 | | - isOpenEnded = ischar(selections{end}); |
255 | | - if fileSpaceRank > selectionRank && ~isOpenEnded |
256 | | - % when there are no trailing ':' then the remainder |
257 | | - % dims are scalar. Otherwise, just load the rest of the |
258 | | - % data. |
259 | | - scalarDims = selectionRank + 1; |
260 | | - dims(scalarDims:end) = 1; |
261 | | - end |
262 | | - memSize = dims; |
263 | | - end |
264 | 323 | end |
265 | 324 |
|
266 | 325 | function refs = export(obj, fid, fullpath, refs) |
|
351 | 410 | 'Cannot index into %d dimensions when max rank is %d',... |
352 | 411 | selectionRank, rank); |
353 | 412 | data = obj.load_mat_style(CurrentSubRef.subs{:}); |
354 | | - expectedSize = dims; |
355 | | - for i = 1:length(CurrentSubRef.subs) |
356 | | - if ~ischar(CurrentSubRef.subs{i}) |
357 | | - expectedSize(i) = length(CurrentSubRef.subs{i}); |
358 | | - end |
359 | | - end |
360 | | - |
361 | | - if ischar(CurrentSubRef.subs{end}) |
362 | | - % dangling ':' where leftover dimensions are folded into |
363 | | - % the last selection. |
364 | | - selDimInd = length(CurrentSubRef.subs); |
365 | | - expectedSize = [expectedSize(1:(selDimInd-1)) prod(dims(selDimInd:end))]; |
366 | | - else |
367 | | - expectedSize = expectedSize(1:length(CurrentSubRef.subs)); |
368 | | - end |
369 | | - |
370 | | - if isscalar(expectedSize) |
371 | | - expectedSize = [1 expectedSize]; |
372 | | - end |
373 | | - |
374 | | - if ~isequal(size(data), expectedSize) |
375 | | - data = reshape(data, expectedSize); |
376 | | - end |
377 | | - |
378 | 413 | if isscalar(S) |
379 | 414 | B = data; |
380 | 415 | else |
|
0 commit comments