separating scatterplots, and rounding numbers for fprintf
1 次查看(过去 30 天)
显示 更早的评论
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.'])
fprintf('The potential PIW bears %f from the CG Asset at a range of %f yards.\n', [piw_asset.'])
%make some mf Graphs
c= "CGA";
p="PIW";
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 个评论
dpb
2022-10-20
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...
回答(1 个)
Nirupama Nagarathinam
2022-10-27
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Distribution Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!