% TGA_STATUS Convert the decimal TGA status flag to a binary value. % [BIT0 BIT1 BIT2 BIT3 BIT4 BIT5 BIT6 BIT7 BIT8] = TGA_STATUS(FLAGS) % Outputs the 9 different error flags where FLAGS is the array of % decimal values from the TGA. Any flags greater than 511 (2^9) will % be output as NaN's. % % [BIT0 BIT1] = TGA_STATUS(FLAGS,N) Where N is the number of error bits. % The number of outputs is however many are within the square brackets % even if the number is less than or exceeds the number of error bits. % % [BIT3 BIT7 BIT4] = TGA_STATUS(FLAGS,N,[3 7 4]) If N and a numeric % array are specified the outputs will be only the specified bits. % % Jeremiah Smith % 7/11/2008 % Last Edit: 7/11/2008 function varargout = tga_status(flags,varargin) % flags = TGA status flags, base 10 decimal value % varargin = any additional function parameters % Parse Inputs if nargin == 1 n = 9; % 9 possible faults, 9-bit error message elseif nargin == 2 n = varargin{1}; % n possible faults, n-bit error message elseif nargin == 3 n = varargin{1}; % n possible faults, n-bit error message nout = varargin{2}; % Columns to output else error('Too many inputs.') end % Create the conversion array C = zeros(2^n,1); % Preinitialize the conversion array, all zeros for i=0:1:n-1 for j=0:1:2^i-1 % Create a numeric array of binary equivalents. 34 in binary = % C(34+1). C(2^i+j+1:2^(i+1):end) = C(2^i+j+1:2^(i+1):end) + 10^i; end end clear i j % Find Flags uflags = unique(flags); % Find unique flags uflags(uflags == 0) = []; % Don't need zero for i=1:1:length(uflags) if uflags(i) <= 2^n - 1 flags(flags == uflags(i)) = C(uflags(i)+1); % Overwrite the decimal value with the binary value else flags(flags == uflags(i)) = NaN; % Out of range error flag end end clear uflags i C n % Format Output if nargin < 3 for i=1:1:nargout varargout{i} = floor((flags - 10^i.*floor(flags./10^i))./10^(i-1)); % Output just each binary column end elseif nargin == 3 for i=1:1:length(nout) varargout{i} = floor((flags - 10^nout(i).*floor(flags./10^nout(i)))./10^(nout(i)-1)); % Output just each binary column end end