Info

La pregunta está cerrada. Vuélvala a abrir para editarla o responderla.

Why can't I call the first function inside the second one? I keep getting an error as the function was not defined.

1 visualización (últimos 30 días)
I have these two functions, the first one inside a sumNumbersInNFiles.m and the second on inside a sumNumbersInFile.m
However, I cannot figure out why the function is not correctly being called.
I keep getting an error when using the following line of code:
partial_sum = sumNumbersInFile(file_name);
function sum = sumNumbersInNFiles();
numberOfFiles = input('Enter the number of files: ');
total_sum = 0;
for i = 1:numberOfFiles
fileID = -1;
errmsg = 'The file was not opened!';
file_name = '';
while fileID < 0
file_name = input('Open file: ', 's');
%remember to write the file extension .txt
[fileID,errmsg] = fopen(file_name);
partial_sum = sumNumbersInFile(file_name);
total_sum = total_sum + partial_sum;
end
end
end
function sum = sumNumbersInFile(file_name);
%characters array
numbers = ['0' '1' '2' '3' '4' '5' '6' '7' '8' '9'];
%initialize sum to zero
sum = 0;
fileID = -1;
%error message in case the file doesn't open
errmsg = 'This is an error message';
while fileID < 0
file_name = input('Open file: ', 's');
%remember to write the file extension .txt
[fileID,errmsg] = fopen(file_name);
end
%checks if end of file was achieved
while ~feof(fileID)
%reads one line of the text file
tline = fgetl(fileID);
%traverses the string obtained from the previous command
for i = 1:strlength(tline)
%compares the current character with the numbers
logical_array = (tline(i) == numbers);
%there are 10 numbers in the list
for j = 1:10
%checks if any of the numbers match the current character
if logical_array(j)==1
%sums the value if true
sum = sum + str2double(tline(i));
end
end
end
end
end
  5 comentarios
Tommy
Tommy el 6 de Abr. de 2020
What does
which sumNumbersInFile
return? Is the file containing sumNumbersInFile() definitely called sumNumbersInFile.m? You don't see a warning like this?

Respuestas (0)

Community Treasure Hunt

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

Start Hunting!

Translated by