Problem when using fprintf

4 ビュー (過去 30 日間)
David Fowler
David Fowler 2019 年 5 月 16 日
編集済み: madhan ravi 2019 年 5 月 16 日
I am very new to MATLAB and coding in general, and will try my best to ask clearly. I believe it's a simple solution to a simple problem. I wrote a small function for the pyhtagorean value:
function c = pyth(a,b)
%Takes two inputs of same size and returns the pythagorean of the 2 inputs
if size(a) == size(b) %checks for same size
c = sqrt(a.*a + b.*b); %calculates the pythagorean value
else
fprintf('Error: Input sizes are not equal'); %returns if sizes are not the same
end
It works correctly, but after it returns, the '>>' is on the same line as my output, rather than a fresh line beneath the output. This is only the case for the fprintf. Here:
>> pyth([1 2;3 4],[5 6;7 8])
ans =
5.0990 6.3246
7.6158 8.9443
>>
>> pyth([1 2],[1 2;3 4])
Error: Input sizes are not equal>>
Does anyone know the quick fix to this? Thanks

回答 (1 件)

madhan ravi
madhan ravi 2019 年 5 月 16 日
編集済み: madhan ravi 2019 年 5 月 16 日
Look at hypot()
if all(size(a) == size(b)) % change this too
fprintf('Error: Input sizes are not equal\n')
% ^^ this should solve the issue

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by