Want to delete ans from the output ou a function

Whenever I use this function, it always spits back an ans in the command window, how do I prevent this from happening?
function [matriz, k, file] = carregar_dados (file)
file = input('Introduza o nome do ficheiro > ', 's');
fid = fopen(file);
if fid ~= -1
fprintf('O ficheiro foi aberto com sucesso! \n');
else
while fid == -1;
fprintf('O ficheiro não existe. \n');
file = input('Introduza o nome do ficheiro > ', 's');
fid = fopen(file);
end
end
k = 0;
matriz = [];
while ~feof(fid);
k = k + 1;
i = 0;
linha = fgetl(fid);
for i = 1:4;
linha(1) = [];
[valor, linha] = strtok(linha, ',)');
matriz(k,i) = str2num(valor);
end
end
fclose(fid);
end

4 comentarios

James Tursa
James Tursa el 18 de En. de 2022
When you call the function, end that line with a semi-colon. Also, why do you pass in the variable 'file' as an input argument if you immediately overwrite it inside the function?
Pedro Almeida
Pedro Almeida el 19 de En. de 2022
Editada: DGM el 19 de En. de 2022
Thanks for the file thing, already fixed it! But still, I put a semicolon after calling the function and it still spits back an ans into the command window, anything else I could try?
function [matriz] = carregar_dados_teste2();
file = input('Introduza o nome do ficheiro > ', 's');
fid = fopen(file);
if fid ~= -1
fprintf('O ficheiro foi aberto com sucesso! \n');
else
while fid == -1
fprintf('O ficheiro não existe. \n');
file = input('Introduza o nome do ficheiro > ', 's');
fid = fopen(file);
end
end
k = 0;
matriz = [];
while ~feof(fid)
k = k + 1;
i = 0;
linha = fgetl(fid);
for i = 1:4
linha(1) = [];
[valor, linha] = strtok(linha, ',)');
matriz(k,i) = str2num(valor);
end
end
fclose(fid);
end
Stephen23
Stephen23 el 19 de En. de 2022
Editada: Stephen23 el 19 de En. de 2022
" I put a semicolon after calling the function..>"
No, you added a semi-colon in the function signature line. That will do nothing.
"and it still spits back an ans into the command window..."
Yes, because you did not put a semi-colon after the function when you call it.
"anything else I could try?"
You could put a semi-colon after the function when you call it.
But given that your function imports some data and then you assign this to ANS which you now want to ignore... I suspect that you should actually assign the function output to a variable in order to use that imported data for something.
Thank you so much! It's working perfectly now.

Iniciar sesión para comentar.

Respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Productos

Versión

R2021b

Preguntada:

el 18 de En. de 2022

Editada:

el 19 de En. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by