필터 지우기
필터 지우기

How to properly use varargout and nargout?

조회 수: 8 (최근 30일)
Victoria Gonzalez Canalle
Victoria Gonzalez Canalle 2020년 3월 23일
편집: Sriram Tadavarty 2020년 3월 24일
Hi! I need to calculate the bits and nibbles from an input of bytes. If the functions expects two outputs, the it will calcualtion the nibles. But of not, it will calculate the bits. My code looks like this:
function [varargout] = bytes2bits(bytes)
% write your solution here:
if nargout == 2
varargout{1} = bytes./8;
varargout{2} = bytes./32;
else
varargout{1} = bytes./8;
end
end
and the error that I am getting (from the eudcational platform) is:
Function failed when calculating bits with one output.
  댓글 수: 3
per isakson
per isakson 2020년 3월 24일
No problems here
>> bytes2bits( 64 )
ans =
8
>> [a1,a2] = bytes2bits( 64 );
>> [a1] = bytes2bits( 64 );
>>
Sriram Tadavarty
Sriram Tadavarty 2020년 3월 24일
편집: Sriram Tadavarty 2020년 3월 24일
The code seems correct.
However, you can try this
if nargout == 2
varargout{1} = bytes./8;
varargout{2} = bytes./32;
elseif nargout == 1
varargout{1} = bytes./8;
end

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

답변 (0개)

카테고리

Help CenterFile Exchange에서 Argument Definitions에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by