MATLAB: Confirm the number of dimensions

ifndimsnumber of dimensions

Hey
I have a short question. In my excel file (Test) I have defined coordinates for 4 cities.
City x1 x2 x3
1 2 5 4
2 7 2 5
3 12 5 6
4 9 10 5
In this case each city is defined by 3 coordinates (3d). Now I would like to introduce an if condition to plot the coordinates. My goal is to plot the coordinates. When two coordinates are used the script look like this:
data = xlsread('Test.xlsx',1)
dist = dist(data(:,2:3)') % how many coordinates are necessary to describe the location from one city

dist(dist==0) = inf
x1 = data(:,2)
x2 = data(:,3)
x3 = data(:,4)
if ndims(data) == 2
plot(x1, x2, 'x')
else
ndims(data) == 3
plot3(x1, x2, x3, 'x')
end
And when 3 coordinates are used the script looks like this:
data = xlsread('Test.xlsx',1)
dist = dist(data(:,2:4)') % how many coordinates are necessary to describe the location from one city
dist(dist==0) = inf
x1 = data(:,2)
x2 = data(:,3)
x3 = data(:,4)
if ndims(data) == 2
plot(x1, x2, 'x')
else
ndims(data) == 3
plot3(x1, x2, x3, 'x')
end
The problem in my code is that ndims(data) in both cases is 2.
I hope someone can help to solve my problem. In the second case the number of ndims(data) should be 3.
Best

Best Answer

Try this:

cityNumber = data(:, 1);
numCoordinates = size(data, 2) - 1; % should be 2 or 3
x1 = data(:,2);
x2 = data(:,3);
if numCoordinates >= 3
	x3 = data(:,4);
end
if numCoordinates == 2
	plot(x1, x2, 'x', 'MarkerSize', 10);
	caption = '2 Coordinates';
elseif numCoordinates == 3
	plot3(x1, x2, x3, '*', 'MarkerSize', 10);
	caption = '3 Coordinates';
end
grid on;
xlabel('x1', 'FontSize', 20);
ylabel('x2', 'FontSize', 20);
if numCoordinates == 3
	zlabel('x3', 'FontSize', 20);
end
title(caption, 'FontSize', 20);