How to use fprintf statements after a function

5 visualizaciones (últimos 30 días)
Serena Simpson
Serena Simpson el 9 de Oct. de 2020
Comentada: Asad (Mehrzad) Khoddam el 9 de Oct. de 2020
I have a function. For my class, we have to put result output at the bottom of our code. However, functions have to be at the bottom. Is there a way I can use fprintf statements under the function?
%% Section 2: Processing
function y=vorder(x)
N=length(x);
y=x;
for i=2:N-1
if y(i-1)>y(i)
tmp=y(i);
y(i-1)=y(i);
y(i)=tp;
end
end
for i=N-1:2
if y(i)<y(i-1)
tm=y(i);
y(i)=y(i-2);
y(i-1)=tmp;
end
end
end
%% Section 3: Result Output ERRORS HERE
fprintf('Input and output\n');
fprintf('%d ',x); fprintf('\n');
fprintf('%d ',y); fprintf('\n');
  1 comentario
Asad (Mehrzad) Khoddam
Asad (Mehrzad) Khoddam el 9 de Oct. de 2020
You can use another function that includes frprintf statements.
Also, the order of functions are nor important. It is not necessary to use fprintf after the function.

Iniciar sesión para comentar.

Respuestas (2)

Star Strider
Star Strider el 9 de Oct. de 2020
Yes.
Try something like this:
x = ...;
y=vorder(x); % Call Function
fprintf('Input and output\n');
fprintf('%d ',x); fprintf('\n');
fprintf('%d ',y); fprintf('\n');
function y=vorder(x)
. . .
end
You need to call the function to get the output from it. Then write the output. The function itself is at the end of the script file.
I did not run this, so I will leave it to you to experiment with it.
.

KSSV
KSSV el 9 de Oct. de 2020
Let x be your vector.
fmt = [repmat('%g, ', 1, numel(x)-1), '\n'] ;
fprintf(fmt, x)

Categorías

Más información sobre Startup and Shutdown en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by