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

조회 수: 9 (최근 30일)
R Stephens
R Stephens 2017년 7월 14일
댓글: R Stephens 2017년 7월 14일
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

채택된 답변

Steven Lord
Steven Lord 2017년 7월 14일
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
R Stephens
R Stephens 2017년 7월 14일
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

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Manual Performance Optimization에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by