立意
在本課題所涉及的實驗中,需要將2個拉線式位移傳感器中的數據收集并處理,在此基礎上求解相應的速度
主要功能
- 針對一個figure文件中僅包含一個plot,且該plot中包含指定數目的曲線,求這些曲線的RMS值;
- 針對一個figure文件中包含多個plot,且該plot中包含相同數目的曲線,求這些曲線的RMS值;
1figure1plot3curve的RMS值計算
% 讀取 .fig 文件
fig = openfig('yourfigurename.fig');% 獲取所有的 axes 對象
axes_handles = findobj(fig, 'Type', 'axes');% 假設所有曲線都在第一個 axes 上
if ~isempty(axes_handles)ax = axes_handles(1);% 獲取所有的 line 對象line_handles = findobj(ax, 'Type', 'line');% 獲取圖例對象legend_obj = findobj(fig, 'Type', 'legend');% 確保有三條曲線if length(line_handles) == 3% 獲取圖例的文本內容if ~isempty(legend_obj)legend_text = get(legend_obj, 'String');elselegend_text = cell(1, 3);for i = 1:3legend_text{i} = ['Curve ' num2str(i)];endendfor i = 1:3% 獲取第 i 條曲線的數據x = get(line_handles(i), 'XData');y = get(line_handles(i), 'YData');% 計算 RMS 值rms_value = rms(y);% 顯示結果fprintf('%s 的 RMS 值為: %.4f\n', legend_text{i}, rms_value);endelseerror('圖中曲線數量不為 3 條。');end
elseerror('未找到 axes 對象。');
end% 關閉圖形
close(fig);
1figure8plot4curve的RMS值計算
% 讀取 .fig 文件
fig = openfig('vehicle_dynamics_erformances2.fig');% 獲取所有的 axes 對象(即 subplot)
axes_handles = findobj(fig, 'Type', 'axes');% 確保有 8 個 subplot
if length(axes_handles) == 8% 獲取每個 axes 的位置信息positions = zeros(8, 4);for i = 1:8positions(i, :) = get(axes_handles(i), 'Position');end% 按照 4 行 2 列的布局排序% 先按行排序,同一行內按列排序[~, sort_indices] = sortrows([-positions(:, 2), positions(:, 1)]);sorted_axes_handles = axes_handles(sort_indices);for subplot_index = 1:8ax = sorted_axes_handles(subplot_index);% 獲取當前 subplot 中的所有 line 對象line_handles = findobj(ax, 'Type', 'line');% 確保當前 subplot 中有 4 條曲線if length(line_handles) == 4% 獲取當前 subplot 的圖例對象legend_obj = findobj(ax, 'Type', 'legend');% 獲取圖例的文本內容if ~isempty(legend_obj)legend_text = get(legend_obj, 'String');elselegend_text = cell(1, 4);for i = 1:4legend_text{i} = ['Curve ' num2str(i)];endendfprintf('Subplot %d 中各曲線的 RMS 值:\n', subplot_index);for line_index = 1:4% 獲取第 line_index 條曲線的數據line_handle = line_handles(line_index);x = get(line_handle, 'XData');y = get(line_handle, 'YData');% 過濾掉無效值(如 NaN)valid_indices = ~isnan(y);valid_y = y(valid_indices);% 計算 RMS 值rms_value = rms(valid_y);% 顯示結果fprintf(' %s 的 RMS 值為: %.4f\n', legend_text{line_index}, rms_value);endelseerror(['Subplot ' num2str(subplot_index) ' 中曲線數量不為 4 條。']);endend
elseerror('圖中 subplot 數量不為 8 個。');
end% 關閉圖形
close(fig);