Arrayfun() Doesn't Work with this Simple Function - Why?
4 views (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
Steven Lord
on 14 Jul 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
More Answers (0)
See Also
Categories
Find more on Manual Performance Optimization in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!