separating scatterplots, and rounding numbers for fprintf

1 visualización (últimos 30 días)
Brennan
Brennan el 19 de Oct. de 2022
Respondida: Nirupama Nagarathinam el 27 de Oct. de 2022
I wanna have the scatter plots all seperate, so in this case 3 seperate scatter plots, all have one of the CGA points, PIW points, and Rescue Vessel point. Obously, I want the ones realated to eachother together, but say there was 5 rows, I want it to do 5, say the excel had 2 more rows. also, for the fprint f, is there a way to round that to the whole numbers place without a ton of zeros after? below is code and excel...
data=readmatrix("test_SAR_1.xlsx"); %loads data
xCGA= data(:,1);
yCGA= data(:,2);
xAsset= data(:,3);
yAsset= data(:,4);
Bearing= data(:,5);
Range= data(:,6);
%calculations:
xCGA_PIW=Range.*cosd(90-Bearing);
yCGA_PIW=Range.*sind(90-Bearing);
xPIW=xCGA_PIW+xCGA;
yPIW=yCGA_PIW+yCGA;
xAsset_PIW=xPIW-xAsset;
yAsset_PIW=yPIW-yAsset;
%RANGE!!
RangeAsset_PIW= sqrt(yAsset_PIW.^2+xAsset_PIW.^2);
%TBearing
TBearingAsset_PIW= atan2d(xAsset_PIW,yAsset_PIW);
TBearingAsset_PIW= mod(TBearingAsset_PIW, 360);
TBearingPIW_Asset= mod((TBearingAsset_PIW+180), 360);
%matrix for fprintf
asset_piw= [TBearingAsset_PIW RangeAsset_PIW];
piw_asset= [TBearingPIW_Asset RangeAsset_PIW];
fprintf('The potential rescue vessel bears %f from PIW at a range of %f yards.\n', [asset_piw.'])
The potential rescue vessel bears 302.377909 from PIW at a range of 635.580865 yards. The potential rescue vessel bears 54.070223 from PIW at a range of 704.930001 yards. The potential rescue vessel bears 332.514736 from PIW at a range of 656.636786 yards.
fprintf('The potential PIW bears %f from the CG Asset at a range of %f yards.\n', [piw_asset.'])
The potential PIW bears 122.377909 from the CG Asset at a range of 635.580865 yards. The potential PIW bears 234.070223 from the CG Asset at a range of 704.930001 yards. The potential PIW bears 152.514736 from the CG Asset at a range of 656.636786 yards.
%make some mf Graphs
c= "CGA";
p="PIW";
a="Rescue Vessel"
a = "Rescue Vessel"
%now for the plots...
scatter(xCGA,yCGA,"filled",'Marker','o');
text(xCGA,yCGA,c,'VerticalAlignment','bottom','HorizontalAlignment','left');
hold on
scatter(xPIW,yPIW,"filled",'Marker','o');
text(xPIW,yPIW,p,'VerticalAlignment','bottom','HorizontalAlignment','left');
scatter(xAsset,yAsset,"filled",'Marker','o');
text(xAsset,yAsset,a,'VerticalAlignment','bottom','HorizontalAlignment','left');
hold off
  4 comentarios
Brennan
Brennan el 19 de Oct. de 2022
I attached the other excel files so you can see what other data would look like
dpb
dpb el 20 de Oct. de 2022
So, you've got more rows. What's the problem? The above code will just put more points on the scatter plot.
It would still be simpler coding to loop over the column indices by twos than to create all those named variables, but if there are still only the three sets, it's not too bad and since it's already done...
Dunno what it is you're asking for, sorry...

Iniciar sesión para comentar.

Respuestas (1)

Nirupama Nagarathinam
Nirupama Nagarathinam el 27 de Oct. de 2022
In order to have 3 separate scatter plots in the same figure, use "tiledlayout" or "subplot" functions.
tiledlayout(3,1)
ax1=nexttile;
scatter(ax1,xCGA,yCGA,"filled",'Marker','o');
text(xCGA,yCGA,c,'VerticalAlignment','bottom','HorizontalAlignment','left');
ax2=nexttile;
scatter(ax2,xPIW,yPIW,"filled",'Marker','o');
text(xPIW,yPIW,p,'VerticalAlignment','bottom','HorizontalAlignment','left');
ax3=nexttile;
scatter(ax3,xAsset,yAsset,"filled",'Marker','o');
text(xAsset,yAsset,a,'VerticalAlignment','bottom','HorizontalAlignment','left');
subplot(3,1,1)
scatter(xCGA,yCGA,"filled",'Marker','o');
text(xCGA,yCGA,c,'VerticalAlignment','bottom','HorizontalAlignment','left');
subplot(3,1,2)
scatter(xPIW,yPIW,"filled",'Marker','o');
text(xPIW,yPIW,p,'VerticalAlignment','bottom','HorizontalAlignment','left');
subplot(3,1,3)
scatter(xAsset,yAsset,"filled",'Marker','o');
text(xAsset,yAsset,a,'VerticalAlignment','bottom','HorizontalAlignment','left');
You can also create 3 scatter plots in 3 separate figure windows by altering your code to:
figure(1)
scatter(xCGA,yCGA,"filled",'Marker','o');
text(xCGA,yCGA,c,'VerticalAlignment','bottom','HorizontalAlignment','left');
figure(2)
scatter(xPIW,yPIW,"filled",'Marker','o');
text(xPIW,yPIW,p,'VerticalAlignment','bottom','HorizontalAlignment','left');
figure(3)
scatter(xAsset,yAsset,"filled",'Marker','o');
text(xAsset,yAsset,a,'VerticalAlignment','bottom','HorizontalAlignment','left');
To acess only certain elements of the array, use array indexing.
For example:
xCGA = [0;333;747;500;0;250;550];
xCGA = xCGA(1:5); % first 5 elements of xCGA
The same array indexing format can be used when you want to plot only 5 out 7 rows of the column vector.
scatter(xCGA(1:5),yCGA(1:5),"filled",'Marker','o');
text(xCGA(1:5),yCGA(1:5),c,'VerticalAlignment','bottom','HorizontalAlignment','left');
To display the floating-point number as a whole number, modify the code to:
fprintf('The potential rescue vessel bears %.0f from PIW at a range of %.0f yards.\n', [asset_piw.'])
fprintf('The potential PIW bears %.0f from the CG Asset at a range of %.0f yards.\n', [piw_asset.'])
"%.0f" means there will be no digits after decimal point. If you want to round off to 2 decimal places use "%.2f" instead.

Categorías

Más información sobre Data Distribution Plots en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by