How do I remove the Ans from the result

3 Ansichten (letzte 30 Tage)
Luiz Felipe am 15 Jan. 2023
Kommentiert: Walter Roberson am 16 Jan. 2023
I'm using If in my code, but every time the condition is passed on, an ans appears in the result.
if Alpha(1,1) == 1
disp('Ok')
else Alpha(1,1) == 0
disp("Not okay")
endif
if Alpha(1,2) == 1
disp("ok")
else Alpha(1,2) == 0
disp("Not Okay")
endif
0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

Antworten (3)

Torsten am 15 Jan. 2023
Bearbeitet: Torsten am 15 Jan. 2023
How do I remove the Ans from the result
By correctly coding the if-clause:
Alpha = [1,0];
if Alpha(1,1) == 1
disp('Ok')
elseif Alpha(1,1) == 0
disp("Not okay")
end
Ok
if Alpha(1,2) == 1
disp("ok")
elseif Alpha(1,2) == 0
disp("Not Okay")
end
Not Okay
0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

Sulaymon Eshkabilov am 15 Jan. 2023
You'd need to use either display or fprintf not to have ans, e.g.:
%% fprintf
if Alpha(1,1) == 1
fprintf('Ok \n')
else Alpha(1,1) == 0
fprintf('Not okay \n')
end
%% display
if Alpha(1,2) == 1
display("ok")
else Alpha(1,2) == 0
display("Not Okay")
end
1 Kommentar-1 ältere Kommentare anzeigen-1 ältere Kommentare ausblenden
Voss am 15 Jan. 2023
That's incorrect. Neither disp, display, nor fprintf show "ans".
disp('ok')
ok
display('ok')
ok
fprintf('ok\n')
ok
The "ans" comes from, e.g.,
else Alpha(1,1) == 0
which is the same as
else
Alpha(1,1) == 0
which prints the result of Alpha(1,1) == 0 to the command line, with "ans".

Melden Sie sich an, um zu kommentieren.

Walter Roberson am 15 Jan. 2023
Questions about a different programming language should be addressed to the appropriate support resources for that programming language. We cannot be expected to know or explain the behaviour of a project whose purpose for existence is to put Mathworks out of business. (Yes, seriously, that is the design goal for Octave. Read about the Free Software Foundation, which is a political movement not a technical movement.)
3 Kommentare1 älteren Kommentar anzeigen1 älteren Kommentar ausblenden
Voss am 16 Jan. 2023
I think it's the "endif" that gives it away as Octave code.
Walter Roberson am 16 Jan. 2023
MATLAB has never ever had "endif"

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Octave finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by