Output argument "..." (and maybe others) not assigned during call to "...".

1 次查看(过去 30 天)
I have this error. When I call a function in another function this error pull up:
Output argument "matrix" (and maybe others) not assigned during call to "ChargerMatrice".
Error in OptimiserChemin (line 10)
[matrix] = ChargerMatrice (choixOption);
However when I run the function called only, the Command Window display my matrix correctly.
My function where I call the other function is this one:
function [unChemin,cheminPossible] = OptimiserChemin ()
[tab,tf] = CreerTrajet ();
borne1 = tab(1);
borne2 = tab(2);
autonomie = tab(3);
choixOption = 1;
[matrix] = ChargerMatrice (choixOption); %this line causing the error
matrixDistance = matrix;
choixOption = 2;
[matrix] = ChargerMatrice (choixOption);
matrixDuree = matrix;
distance = matrixDistance(borne1,borne2);
duree = matrixDuree(borne1,borne2);
and the function called is :
function [matrix] = ChargerMatrice (choixOption)
if (choixOption == 1)
file = fopen('MatriceDistances.txt', 'rt');
if file ~= -1
maintable1 = readmatrix('MatriceDistances.txt');
maintable1(1,:) = [];
maintable1(:,1) = [];
matrix = maintable1; %the variable matrix is there
fclose(file);
elseif (choixOption == 2)
file = fopen('MatriceDurees.txt', 'rt');
if file ~= -1
maintable2 = readmatrix('MatriceDurees.txt');
maintable2(:,1) = [];
matrix = maintable2; %the variable matrix is there
fclose(file);
else
fprintf('Erreur! Veuillez choisir un nombre entre 1 et 2 correspondant à la matrice à afficher');
end
end
end
end

采纳的回答

Walter Roberson
Walter Roberson 2020-3-22
Your code does not assign anything to matrix in the case that the fopen fails.
Your elseif choixOption == 2 is on the else from file ~= -1, so it will only be executed in the case that choixOption is 1 but the fopen fails. If choixOption is passed in as 2 then there is no else or elseif for that case.
By the way, you can program using try/catch style around readmatrix() instead of testing the file open yourself.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by