Hi. I have some 3D figures that I would like to plot within a single graph using plot3.
I started with this code:
NM=10;
STEP=1;
color='rb';
iter=0;
for i=1:STEP:NM
nomefile=sprintf('C:\\Users\\Alberto\\Downloads\\object_%d.txt',i);
objAA=load(nomefile);
object(:,:,i)=objAA;
fg=sprintf('%s.',color(i));
figure(1)
plot3(object(:,1,i),object(:,2,i),object(:,3,i),fg)
hold on
end
But it gives me the following error:
Unable to perform assignment because the size of the left side is 29520-by-3 and the size of the right side is
43422-by-3.
Error in ...... (line ..)
object(:,:,i)=objAA;
Do you have to have the two 3D figures with the same nodes?

 Akzeptierte Antwort

Star Strider
Star Strider am 6 Dez. 2022
Save each loaded file to a cell array instead.
Try something like this —
a = randi(9, 10, 3);
writematrix(a,'Test1.txt');
b = randi(9, 15, 3);
writematrix(b,'Test2.txt');
figure
hold on
for i = 1:2
object{i} = load(sprintf('Test%d.txt',i));
plot3(object{i}(:,1), object{i}(:,2), object{i}(:,3), 'DisplayName',sprintf('Test%d.txt',i))
end
hold off
grid on
legend('Location','best')
Make appropriate changes to get the desired results.
.

6 Kommentare

Alberto Acri
Alberto Acri am 6 Dez. 2022
Thank you for your reply @Star Strider!
Could you tell me how to remove the lines from your graph and display only the nodes ?
My pleasure!
Either one of these —
plot3(object{i}(:,1), object{i}(:,2), object{i}(:,3), 'DisplayName',sprintf('Test%d.txt',i), 's')
or:
scatter3(object{i}(:,1), object{i}(:,2), object{i}(:,3), 'DisplayName',sprintf('Test%d.txt',i), 15, [], 's', 'filled')
a = randi(9, 10, 3);
writematrix(a,'Test1.txt');
b = randi(9, 15, 3);
writematrix(b,'Test2.txt');
figure
hold on
for i = 1:2
object{i} = load(sprintf('Test%d.txt',i));
plot3(object{i}(:,1), object{i}(:,2), object{i}(:,3), 's', 'DisplayName',sprintf('Test%d.txt',i))
end
hold off
grid on
axis('padded')
legend('Location','best')
figure
hold on
for i = 1:2
object{i} = load(sprintf('Test%d.txt',i));
scatter3(object{i}(:,1), object{i}(:,2), object{i}(:,3), 50, 's', 'filled', 'DisplayName',sprintf('Test%d.txt',i))
end
hold off
grid on
axis('padded')
legend('Location','best')
.
OK, good. If I wanted to have dots (.) of size 5 instead of squares how can I do it?
plot3(object{i}(:,1), object{i}(:,2), object{i}(:,3), 's','Markersize',5, 'DisplayName',sprintf('mandibola_%d.txt',i))
Yes, although 5 is fairly small. Change the 'MarkerSize' to a larger value if necessary.
a = randi(9, 10, 3);
writematrix(a,'mandibola_1.txt');
b = randi(9, 15, 3);
writematrix(b,'mandibola_2.txt');
figure
hold on
for i = 1:2
object{i} = load(sprintf('mandibola_%d.txt',i));
plot3(object{i}(:,1), object{i}(:,2), object{i}(:,3), '.', 'MarkerSize',5, 'DisplayName',sprintf('mandibola\\_%d.txt',i))
end
hold off
grid on
axis('padded')
legend('Location','best')
There are necessary changes to the sprintf call in the 'DisplayName' name-value pair that I added here to write the file name correctly (unless you want the numbers as subscripts, in which case leave out the ‘\\’ that I added).
.
Whereas if I wanted to set a certain color for each 'test_#.txt'?
I tried this way but it gives me error.
color='rb';
a = randi(9, 10, 3);
writematrix(a,'test_1.txt');
b = randi(9, 15, 3);
writematrix(b,'test_2.txt');
figure
hold on
for i = 1:2
object{i} = load(sprintf('test_%d.txt',i));
fg=sprintf('%s.',color(i));
plot3(object{i}(:,1), object{i}(:,2), object{i}(:,3), '.', 'MarkerSize',15, 'DisplayName',sprintf('test\\_%d.txt',i), fg)
end
hold off
grid on
axis('padded')
legend('Location','best')
Setting the individual DisplayName' colour doesn’t appear to be possible. I thought that the 'DisplayName' was a text object, and in that instance, it would be possible to change the text colours individually. It is not. It just appears to be a string array or character vector array, with no specific properties that can be set. It is possible to change the colours of the strings in the legend object, however not individually. They all have to be set to the same colour.
The dots however can be coloured specifically, and this code uses the ‘color’ vector to do that —
color='rb';
a = randi(9, 10, 3);
writematrix(a,'test_1.txt');
b = randi(9, 15, 3);
writematrix(b,'test_2.txt');
figure
hold on
for i = 1:2
object{i} = load(sprintf('test_%d.txt',i));
plot3(object{i}(:,1), object{i}(:,2), object{i}(:,3), '.', 'MarkerSize',15, 'DisplayName',sprintf('test\\_%d.txt',i), 'Color',color(i) )
end
hold off
grid on
axis('padded')
legend('Location','best')
That is likely the best it’s possible to do with respect to the colours.
.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Graph and Network Algorithms finden Sie in Hilfe-Center und File Exchange

Produkte

Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by