Different variable type when using function

7 次查看(过去 30 天)
I thought I managed to make everything working decently but after searching for any error one thousand times, I found one in one of my functions.
So, I have this function:
function [path, amountofpaths, dist_parc] = caminhos(origem, prev, valores_adj_final)
clc;
amountofpaths = length(prev);
path = cell(1, amountofpaths);
dist_parc = cell(1,amountofpaths);
for i = 1:amountofpaths
if ~(origem == i)
a = prev(i);
path{i}(1) = i;
b=2;
while a ~= 0
path{i}(b) = a;
a = prev(a);
b = b+1;
end
end
end
for i = 1:amountofpaths
path{i} = flip(path{i});
end
for i = 1:amountofpaths
if length(path{i}) == 2
for j = 1
dist_parc{i} = valores_adj_final(path{i}(j), path{i}(j+1));
end
end
if length(path{i}) > 2
for j = 1:length(path{i})-1
dist_parc{i}(j) = valores_adj_final(path{i}(j), path{i}(j+1));
end
end
end
end
The variable dist_parc should be a cell, if I run this as a script ( with the same variable values), it is. If I run the function it ends up being a double. Any idea why?

回答(1 个)

Walter Roberson
Walter Roberson 2016-12-17
Do not use path as a variable name, as MATLAB might misinterpret it as the key variable path that MATLAB uses to figure out what functions are available to be called.

类别

Help CenterFile Exchange 中查找有关 Data Distribution Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by