Azzera filtri
Azzera filtri

How Do I Prevent The First Value in the Output of a Function From Showing?

2 visualizzazioni (ultimi 30 giorni)
When I run my code, despite having semicolons on each function, I am getting an answer dispayed to the command window. I have figured out that this answer displayed directly correlates with the first output in my function (I'm assuming the correct format is function [output] = example (input) ). I would really like to get this function to work without displaying any output. Please note that I have already tried placing a semicolon next to the function itself. Please try running this code yourself. It will make alot more sense! Thanks!
For this function I get an answer displayed in the command window that is equal to Ax:
function [ Ax, Ay, Bx, By, Cx, Cy ] = Input_Triangle_Points ()
Point_A = input ('Enter the the value for point A as a matrix: ');
disp('')
Point_B = input ('Enter the the value for point B as a matrix: ');
disp('')
Point_C = input ('Enter the the value for point C as a matrix: ');
disp('')
Ax = Point_A (1,1);
Ay = Point_A (1,2);
Bx = Point_B (1,1);
By = Point_B (1,2);
Cx = Point_C (1,1);
Cy = Point_C (1,2);
end
command window displays:
ans =
Ax (whatever Ax may be)

Risposte (1)

Walter Roberson
Walter Roberson il 23 Ott 2023
When you call a function and you do not have a semi-colon at the end of the call, then MATLAB interprets that as a request to display the first output of the function if the function had at least one output.
The solution is to put a semi-colon at the end of the call.
However, it is possible to code a function to detect whether it was called in a context that requires an output. You can do that by testing nargout . If nargout is 0 then the function was called without an explicit or implicit output requirements. When you detect that nargout is 0, you can choose to not assign anything to the first output variable, or you can choose to clear that output variable. If the first output variable is undefined at time of return, in a context that does not require an output, then nothing will be displayed.
experiment_one()
entered 1
ans = 123456
experiment_one();
entered 1
disp(experiment_one())
entered 1 123456
experiment_two()
entered 2
experiment_two();
entered 2
disp(experiment_two())
entered 2 56789
function out1 = experiment_one
disp('entered 1')
out1 = 123456;
end
function out1 = experiment_two
disp('entered 2')
out1 = 56789;
if nargout == 0; clear out1; end
end
  2 Commenti
Jackson
Jackson il 23 Ott 2023
Hey, thanks for the fast response! I have tried placing a semicolon next to the function but Matlab highlights it and states "the extra semicolon is unnecessary" and if I run it with the semicolon despite Matlab's warnings I still get an answer out in the command window. I am a beginner at Matlab so I am not sure how testing nargout would fit into my function.
To clarify, I am not calling a function, I am programming a function that I can call later. This makes the issue all the more confusing to me as I am not sure why this output is being generated.
Walter Roberson
Walter Roberson il 23 Ott 2023
When you press the green Run button, that is equivalent to going down to the command line and calling the function with no parameters and with no explicit outputs.
If that is what you are doing and it is important that Ax not be displayed when the user does that, then you need to use the nargout test.

Accedi per commentare.

Categorie

Scopri di più su Get Started with MATLAB in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by