How can I get the quiver arrows to 'line up'?

4 次查看(过去 30 天)
I am currently drawing a quiver graph using data from the ocean observation buoy at Incheon Port. The graph is a flow velocity vector graph for a specified period and uses the Cartesian coordinate system. I have a few concerns about drawing the quiver graph right now.
First, is it possible to make each quiver arrow line up like in 'Example.png'? With the current code, each quiver arrow appears scattered like 'Now.png'.
Second, I would like to display the size standard of the flow velocity vector on a graph, as shown in the red-outlined rectangle in 'Example.png'. (The standard value is not necessarily 0.1.)
I tried touching variables such as U and V, but the problem is currently not resolved... I would like to seek help from experts. The code and data files have been compressed and attached to the 'Incheon_Buoy.zip' file, and the png file has been attached as is, so please check it. The code is also written below.
% Reset everything
clear; clc; close all;
% Set input and figure directories
inp.dir = fullfile(pwd,'01__Data');
addpath(genpath(inp.dir));
fig.dir = fullfile(pwd,'02__Figure');
addpath(genpath(fig.dir));
% Create file names and assign them to the fileID array.
% fileID1 = cell(1, 2); % Create a cell array with 2 elements
fileID2 = cell(1, 2);
% Set start and end dates
% start_date1 = '2021/01/29 00:00'; % Start date for file 1
% end_date1 = '2021/02/02 00:00'; % End date for file 1
start_date2 = datetime('2021-01-29 00:00:00', 'InputFormat', 'yyyy-MM-dd HH:mm:ss'); % Start date for file 2
end_date2 = datetime('2021-02-02 00:00:00', 'InputFormat', 'yyyy-MM-dd HH:mm:ss'); % End date for file 2
for month = 1:2
% Create file names
% filename1 = sprintf('인천_2021년 %02d월_1시간 조위.txt', month);
% Incheon Port marine observation buoy data
filename2 = sprintf('2021년 %02d월 인천항 해양관측부이.txt', month);
% Assign file IDs by calling fopen with file paths
% fileID1{month} = fopen(fullfile(inp.dir, filename1), 'r');
fileID2{month} = fopen(fullfile(inp.dir, filename2), 'r');
end
% Define the format for reading data
% formatSpec1 = '%s%s%f';
formatSpec2 = '%{yyyy-MM-dd HH:mm:ss}D%f%*s%f%*[^\n]';
%% Read and process data from 'Incheon Port marine observation buoy file'
for i = 1:2
% Exclude Korean text parts
dataOG2{i} = textscan(fileID2{i}, formatSpec2, 'HeaderLines', 4, 'Delimiter', '\t', 'TreatAsEmpty','-');
fclose(fileID2{i}); % Close file after reading to release resources
end
SdateOG = [dataOG2{1}{1}; dataOG2{2}{1}].';
SflowOG = [dataOG2{1}{2}; dataOG2{2}{2}].';
SdegOG = [dataOG2{1}{3}; dataOG2{2}{3}].';
start_idx2 = find(SdateOG == start_date2);
end_idx2 = find(SdateOG == end_date2);
Sdate = SdateOG(start_idx2:end_idx2);
Sflow_0 = SflowOG(start_idx2:end_idx2);
Sdeg_0 = SdegOG(start_idx2:end_idx2);
Sflow = Sflow_0/100;
% The angle in the marine observation buoy's reference coordinate system is defined as 'alpha(°)'.
% The angle in the Cartesian coordinate system is defined as 'beta(°)'.
% pi = 180°
% 'alpha - pi' : Sdeg
for j = 1:length(Sdeg_0)
Sdeg_01(j) = Sdeg_0(j) - 180;
if Sdeg_01(j) < 0
Sdeg(j) = Sdeg_01(j) + 360; % Change the ‘negative angle’ to range from 0 to 360 degrees.
else
Sdeg(j) = Sdeg_01(j);
end
end
% 'Magnitude' : Sflow
U = -(Sflow.*sin(Sdeg));
V = -(Sflow.*cos(Sdeg));
% 'beta' : Sdeg_Car
% for j = 1:length(U)
% Sdeg_Car(j) = atan(V(j)/U(j));
% end
%% Figure
graphColor = 'k'; % Set the color for plotting
Sdnum = datenum(Sdate);
f4 = figure;
quiver(Sdnum, Sflow, U, V, graphColor)
xticks([Sdnum(1), Sdnum(49), Sdnum(97), Sdnum(145), Sdnum(193)])
xticklabels({'01-29','01-30','01-31','02-01','02-02'})
xlabel('Time in 2021', 'FontSize', 14);
set(gca, 'ytick', []) % Clear y-axis ticks and numbers
grid on; % Turn on the grid
% Set figure shape & size
set(gcf, 'Position', [10 10 1300 300]); % Set the figure position and size
title('Station: 인천항', 'FontSize', 14) % Set the title
% Save the figure as an image
fname = ['03__current_vector', '.png'];
% Output the created image as a .png file
print(['02__Figure\',fname],'-dpng');

采纳的回答

Voss
Voss 2024-3-13
For the first question:
Part of the reason for the quiver arrows being scattered is that the U and V calculation is incorrect. When the angles are in degrees, sind and cosd should be used; sin and cos are for angles in radians.
That corrects the U, V calculation but the arrows still have different starting coordinates in the y direction (which is Sflow in this case). To make them all start at the same y value, you can specify some constant or scalar Y, e.g., the average of Sflow (mean(Sflow,'omitnan')) or whatever other value you want.
For the second question:
You can create a text object to display this information. I'm not sure where the value comes from, so I'm using 0.1.
unzip('Incheon_Buoy.zip')
cd('Incheon_Buoy')
% Reset everything
clear; clc; close all;
% Set input and figure directories
inp.dir = fullfile(pwd,'01__Data');
addpath(genpath(inp.dir));
fig.dir = fullfile(pwd,'02__Figure');
addpath(genpath(fig.dir));
% Create file names and assign them to the fileID array.
% fileID1 = cell(1, 2); % Create a cell array with 2 elements
fileID2 = cell(1, 2);
% Set start and end dates
% start_date1 = '2021/01/29 00:00'; % Start date for file 1
% end_date1 = '2021/02/02 00:00'; % End date for file 1
start_date2 = datetime('2021-01-29 00:00:00', 'InputFormat', 'yyyy-MM-dd HH:mm:ss'); % Start date for file 2
end_date2 = datetime('2021-02-02 00:00:00', 'InputFormat', 'yyyy-MM-dd HH:mm:ss'); % End date for file 2
for month = 1:2
% Create file names
% filename1 = sprintf('인천_2021년 %02d월_1시간 조위.txt', month);
% Incheon Port marine observation buoy data
filename2 = sprintf('2021년 %02d월 인천항 해양관측부이.txt', month);
% Assign file IDs by calling fopen with file paths
% fileID1{month} = fopen(fullfile(inp.dir, filename1), 'r');
fileID2{month} = fopen(fullfile(inp.dir, filename2), 'r');
end
% Define the format for reading data
% formatSpec1 = '%s%s%f';
formatSpec2 = '%{yyyy-MM-dd HH:mm:ss}D%f%*s%f%*[^\n]';
%% Read and process data from 'Incheon Port marine observation buoy file'
for i = 1:2
% Exclude Korean text parts
dataOG2{i} = textscan(fileID2{i}, formatSpec2, 'HeaderLines', 4, 'Delimiter', '\t', 'TreatAsEmpty','-');
fclose(fileID2{i}); % Close file after reading to release resources
end
SdateOG = [dataOG2{1}{1}; dataOG2{2}{1}].';
SflowOG = [dataOG2{1}{2}; dataOG2{2}{2}].';
SdegOG = [dataOG2{1}{3}; dataOG2{2}{3}].';
start_idx2 = find(SdateOG == start_date2);
end_idx2 = find(SdateOG == end_date2);
Sdate = SdateOG(start_idx2:end_idx2);
Sflow_0 = SflowOG(start_idx2:end_idx2);
Sdeg_0 = SdegOG(start_idx2:end_idx2);
Sflow = Sflow_0/100;
% The angle in the marine observation buoy's reference coordinate system is defined as 'alpha(°)'.
% The angle in the Cartesian coordinate system is defined as 'beta(°)'.
% pi = 180°
% 'alpha - pi' : Sdeg
for j = 1:length(Sdeg_0)
Sdeg_01(j) = Sdeg_0(j) - 180;
if Sdeg_01(j) < 0
Sdeg(j) = Sdeg_01(j) + 360; % Change the ‘negative angle’ to range from 0 to 360 degrees.
else
Sdeg(j) = Sdeg_01(j);
end
end
% 'Magnitude' : Sflow
U = -(Sflow.*sind(Sdeg));
V = -(Sflow.*cosd(Sdeg));
% 'beta' : Sdeg_Car
% for j = 1:length(U)
% Sdeg_Car(j) = atan(V(j)/U(j));
% end
%% Figure
graphColor = 'k'; % Set the color for plotting
Sdnum = datenum(Sdate);
f4 = figure;
quiver(Sdnum, mean(Sflow,'omitnan'), U, V, graphColor)
val = 0.1;
text( ...
'Parent',gca(), ...
'Units','normalized', ...
'Position',[0.1 0], ...
'HorizontalAlignment','center', ...
'VerticalAlignment','bottom', ...
'String',sprintf('--- %g m/s',val), ...
'Interpreter','latex', ...
'EdgeColor','none', ... 'r' <- use 'EdgeColor','r' to draw the red rectangle
'LineWidth',2)
xticks([Sdnum(1), Sdnum(49), Sdnum(97), Sdnum(145), Sdnum(193)])
xticklabels({'01-29','01-30','01-31','02-01','02-02'})
xlabel('Time in 2021', 'FontSize', 14);
set(gca, 'ytick', []) % Clear y-axis ticks and numbers
grid on; % Turn on the grid
% Set figure shape & size
set(gcf, 'Position', [10 10 1300 300]); % Set the figure position and size
title('Station: 인천항', 'FontSize', 14) % Set the title
% Save the figure as an image
fname = ['03__current_vector', '.png'];
% Output the created image as a .png file
print(['02__Figure\',fname],'-dpng');
  5 个评论
Voss
Voss 2024-3-14
If you want to have the text String use the mean Sflow value:
val = mean(Sflow,'omitnan');
quiver(Sdnum, val, U, V, graphColor)
text( ...
'Parent',gca(), ...
'Units','normalized', ...
'Position',[0.1 0], ...
'HorizontalAlignment','center', ...
'VerticalAlignment','bottom', ...
'String',sprintf('--- %g m/s',val), ...
'Interpreter','latex', ...
'EdgeColor','none', ... 'r' <- use 'EdgeColor','r' to draw the red rectangle
'LineWidth',2)

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Environment and Settings 的更多信息

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by