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

gtIndexAllGrainValues : now it works also with struct arrays, giving ind1 as...

gtIndexAllGrainValues : now it works also with struct arrays, giving ind1 as index for it; added argument 'cell2mat' to convert output to a matrix, if needed.

Signed-off-by: default avatarLaura Nervo <laura.nervo@esrf.fr>
parent 99dd7a1e
No related branches found
No related tags found
No related merge requests found
function val = gtIndexAllGrainValues(grain, field1, field2, ind1, ind2)
function val = gtIndexAllGrainValues(grain, field1, field2, ind1, ind2, cell2mat)
% val = gtIndexAllGrainValues(grain, field1, field2, ind1, ind2, cell2mat)
% ------------------------------------------------------------------------
% Returns a column vector containing the values with indices (ind1,ind2) in
% grain.field1.field2 from all the grains in 'grain'.
%
......@@ -27,20 +28,31 @@ function val = gtIndexAllGrainValues(grain, field1, field2, ind1, ind2)
nof_grains = length(grain);
if ~exist('field2','var')
field2 = '';
end
if ~exist('ind1','var')
ind1 = [];
end
if ~exist('ind2','var')
ind2 = [];
end
if ~exist('field2','var')
field2 = [];
if ~exist('cell2mat','var') || isempty(cell2mat)
cell2mat = true;
end
if isstruct(grain{1}.(field1)) && ~isempty(ind1)
tmp = cellfun(@(num) num.(field1)(ind1), grain, 'UniformOutput', false)';
grain = tmp;
ind1 = ind2;
ind2 = [];
field1 = field2;
field2 = [];
clear tmp
end
if isempty(ind1) && isempty(ind2)
form = 0; % all values will be concatenated
elseif isempty(ind2)
......@@ -57,34 +69,30 @@ if isempty(field2)
if islogical(grain{1}.(field1))
val = false(nof_grains, length(ind2));
elseif isstruct(grain{1}.(field1))
val = struct([]);
elseif iscell(grain{1}.(field1))
val = cell(nof_grains, length(ind2));
else
val = zeros(nof_grains, length(ind2), class(grain{1}.(field1)));
end
for ii = 1:nof_grains
val(ii,:) = grain{ii}.(field1)(ind1,ind2);
end
val = cellfun(@(num) num.(field1)(ind1,ind2), grain, 'UniformOutput', false)';
elseif (form == 1)
if islogical(grain{1}.(field1))
val = false(nof_grains, 1);
val = false(nof_grains, 1);
elseif isstruct(grain{1}.(field1))
val = struct([]);
elseif iscell(grain{1}.(field1))
val = cell(nof_grains, 1);
else
val = zeros(nof_grains, 1, class(grain{1}.(field1)));
end
for ii = 1:nof_grains
val(ii,:) = grain{ii}.(field1)(ind1);
end
val = cellfun(@(num) num.(field1)(ind1), grain, 'UniformOutput', false)';
else
val = [];
for ii = 1:nof_grains
val = [val, grain{ii}.(field1)];
end
val = cellfun(@(num) num.(field1), grain, 'UniformOutput', false)';
end
% If there is a second field
......@@ -94,38 +102,42 @@ else
if islogical(grain{1}.(field1).(field2))
val = false(nof_grains, length(ind2));
elseif isstruct(grain{1}.(field1))
val = struct([]);
elseif iscell(grain{1}.(field1))
val = cell(nof_grains, length(ind2));
else
val = zeros(nof_grains, length(ind2), class(grain{1}.(field1).(field2)));
end
for ii = 1:nof_grains
val(ii,:) = grain{ii}.(field1).(field2)(ind1,ind2);
end
val = cellfun(@(num) num.(field1).(field2)(ind1,ind2), grain, 'UniformOutput', false)';
elseif (form == 1)
if islogical(grain{1}.(field1).(field2))
val = false(nof_grains, 1);
val = false(nof_grains, 1);
elseif isstruct(grain{1}.(field1))
val = struct([]);
elseif iscell(grain{1}.(field1))
val = cell(nof_grains, 1);
else
val = zeros(nof_grains, 1, class(grain{1}.(field1).(field2)));
end
for ii = 1:nof_grains
val(ii,:) = grain{ii}.(field1).(field2)(ind1);
end
val = cellfun(@(num) num.(field1).(field2)(ind1), grain, 'UniformOutput', false)';
else
val = [];
for ii = 1:nof_grains
val = [val, grain{ii}.(field1).(field2)];
end
val = cellfun(@(num) num.(field1).(field2), grain, 'UniformOutput', false)';
end
end
if cell2mat
val = reshape([val{:}],size(val{1},2),[])';
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