Arrayfun() Doesn't Work with this Simple Function - Why?

6 visualizaciones (últimos 30 días)
R Stephens
R Stephens el 14 de Jul. de 2017
Comentada: R Stephens el 14 de Jul. de 2017
I'm trying to swap the lower 4 hex digits and the upper 4 hex digits in a array of hex values and I have a "swapIQ" function that does this. Example input = FFC20018 and Example output = 0018FFC2
When I try to use my function on an array of these values with arrayfun() it doesn't work even though arrayfun() works for another simple example. Root cause appears to have something to do with the interaction between my simple function and arrayfun(). Here's an example of a function that works and a function that does not along with the execution errors.
Function that works with arrrayfun():
function [aResult] = add1(x)
% this function works with arrayfun()
aResult = x+1;
end
s = [1; 2; 3];
a = arrayfun(@(x) add1(x), s);
>> a
a =
2
3
4
Function that doesn't work with arrayfun():
function [swapped] = swapIQ(anIQWord)
% this function does not work with arrayfun()
anIQWordHex = dec2hex(anIQWord);
swapping = anIQWordHex;
swapping(1:4) = anIQWordHex(5:8);
swapping(5:8) = anIQWordHex(1:4);
swapped = swapping;
end
C = [4.2909e+09 ; 4.2909e+09 ; 4.2909e+09];
b = arrayfun(@(y) swapIQ(y), C);
Error using arrayfun Non-scalar in Uniform output, at index 1, output 1. Set 'UniformOutput' to false. Error in SwapIQ (line 19) b = arrayfun(@(y) swapIQ(y), C);
And when setting 'UniformOutput' to false I get this error
C = [4.2909e+09 ; 4.2909e+09 ; 4.2909e+09];
b = arrayfun(@(y) swapIQ(y),UniformOutput,false, C);
Error using arrayfun All of the input arguments must be of the same size and shape. Previous inputs had size 13 in dimension 2. Input #3 has size 1
Error in SwapIQ (line 19) b = arrayfun(@(y) swapIQ(y), 'UniformOutput', false, C);
Is there some restriction on function implementation details when using arrayfun()?
Thanks

Respuesta aceptada

Steven Lord
Steven Lord el 14 de Jul. de 2017
You're missing a call to hex2dec in your swapIQ function. When I add that call it works.
function b = exampleForRStephens
C = [4.2909e+09 ; 4.2909e+09 ; 4.2909e+09];
b = arrayfun(@(y) swapIQ(y), C);
end
function [swapped] = swapIQ(anIQWord)
% this function does not work with arrayfun()
anIQWordHex = dec2hex(anIQWord);
swapping = anIQWordHex;
swapping(1:4) = anIQWordHex(5:8);
swapping(5:8) = anIQWordHex(1:4);
swapped = hex2dec(swapping); % Added hex2dec call here
end
Alternately if you want the output to be a cell array containing the char vectors containing the hex digits, using the 'UniformOutput' parameter correctly works. The parameter name/value pairs must occur at the end of the call, not in the middle.
function b = exampleForRStephens
C = [4.2909e+09 ; 4.2909e+09 ; 4.2909e+09];
% Specified 'UniformOutput' in the correct location in the arrayfun call below
b = arrayfun(@(y) swapIQ(y), C, 'UniformOutput', false);
end
function [swapped] = swapIQ(anIQWord)
% this function does not work with arrayfun()
anIQWordHex = dec2hex(anIQWord);
swapping = anIQWordHex;
swapping(1:4) = anIQWordHex(5:8);
swapping(5:8) = anIQWordHex(1:4);
swapped = swapping;
end
  1 comentario
R Stephens
R Stephens el 14 de Jul. de 2017
Thanks for clarifying syntax for the UniformOutput option - the function now works as intended. I also added the number of digits option to the dec2hex() function call so the function now handles leading 0's as well. Updated code below.
BR
function [swapped] = swapIQ(anIQWord)
% this function now works with
% b = arrayfun(@(y) swapIQ(y),C,'UniformOutput', false);
% and handles leading zeros for input
% C = [4.2909e+09 ; 4.2944e+09 ; 0.0030e+09];
anIQWordHex = dec2hex(anIQWord,8);
swapping = anIQWordHex;
swapping(1:4) = anIQWordHex(5:8);
swapping(5:8) = anIQWordHex(1:4);
swapped = swapping;
end

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Manual Performance Optimization en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by