arrayfun with non uniform output

This commit is contained in:
Enrico Lumetti 2021-04-30 00:42:47 +02:00
parent ce66178d75
commit 2a76bf1dca
2 changed files with 18 additions and 4 deletions

View File

@ -28,4 +28,18 @@ cs = {1+3 2};
assert(isequal(cs, {4 2})); assert(isequal(cs, {4 2}));
% convert cell array to standard array % convert cell array to standard array
assert(isequal(cell2mat(cs), [4 2])); assert(isequal(cell2mat(cs), [4 2]));
% arrayfun can output a non uniform cell array
struct_array(1).data = [1 2];
struct_array(2).data = [1 2 3];
try
arrayfun(@(x) x.data, struct_array)
catch e
assert(isequal(e.identifier, 'MATLAB:arrayfun:NotAScalarOutput'));
end
assert(isequal( ...
arrayfun(@(x) x.data, struct_array, 'UniformOutput', false), ...
{[1 2], [1 2 3]}));

View File

@ -12,9 +12,9 @@ patient(2).test = [68, 70, 68; 118, 118, 119; 172, 170, 169];
% if the index is not specified, patient.field returns % if the index is not specified, patient.field returns
% the fields of all its elements as multiple return values % the fields of all its elements as multiple return values
[name1, name2] = patient.name; [patient1, patient2] = patient.name;
assert(isequal(name1, 'John Doe')); assert(isequal(patient1, 'John Doe'));
assert(isequal(name2, 'Ann Lane')); assert(isequal(patient1, 'John Doe'));
% creates a new records 'age'; this will be created in all the other % creates a new records 'age'; this will be created in all the other
% entries; fields not specified here are set to [] % entries; fields not specified here are set to []