How can I plot multiple Matlab files onto one boxplot?

5 ビュー (過去 30 日間)
Aman Kumar
Aman Kumar 2024 年 4 月 9 日
コメント済み: Voss 2024 年 4 月 10 日
Hi guys, I am currently doing an assignment where I have been tasked to filter frequency data for 4 different frequencies and compute FFT’s on them. I have filtered them and computed the FFTs but now I need to create a boxplot with the ffts of all 4 frequencies on one plot to compare. The FFT results are saved as Matlab files. The variable name for the first FFT results Matlab file is fft_results, the second is fft_results2 then fft_results3 and finally fft_results4. The FFT Matlab files are saved as fft_7_results, fft_8_results, fft_9_results and fft_10_results. Any help would be appreciated.
  7 件のコメント
Voss
Voss 2024 年 4 月 9 日
A box plot of the amplitudes and locations of the peaks of each FFT result? I'm not sure what that would look like. Do you have an example? Either a sketch or a link to an image?
Aman Kumar
Aman Kumar 2024 年 4 月 9 日
Upon further inspection I realised the boxplot is solely meant to be the amplitude of the FFT results for 7 Hz, 8 Hz, 9 Hz, 10 Hz. I have attached an example of how it should look.

サインインしてコメントする。

採用された回答

Voss
Voss 2024 年 4 月 9 日
One problem is that you are assuming the list of files returned by dir is in a particular order (7, 8, 9, 10), but in fact the order is 10, 7, 8, 9. So you'd get an error trying to access a field that doesn't exist on the first mat-file you load. To get around that and have the files in sorted order, you can download and use the File Exchange submission natsortfiles.
Also, since each mat-file has only one variable, you can store it in fft_results_all, regardless of its name (so you don't have to check for a particular variable name for a particular mat file).
If you have the Signal Processing Toolbox, you can use findpeaks to get the peaks and their locations for each FFT result. Here's an example, and you can mess with the parameters findpeaks uses to get the result you want.
I really don't know what the boxplot you have in mind should look like for this. Note that the peak locations are not used in the boxplot, only their amplitudes.
folderPath = '.'; % here in Answers the files are in the current directory
fileList = natsortfiles(dir(fullfile(folderPath, 'fft*.mat')));
N = numel(fileList);
fft_results_all = cell(1,N);
for i = 1:N
data = load(fullfile(folderPath, fileList(i).name));
% store the first (and only) variable in each mat-file:
fn = fieldnames(data);
fft_results_all{i} = data.(fn{1})(1:end/2); % use only the 1st half of the data, because of symmetry
end
% plot the data and the peaks
figure
pks = cell(1,N);
locs = cell(1,N);
for ii = 1:N
subplot(2,2,ii)
semilogy(fft_results_all{ii})
hold on
[pks{ii},locs{ii}] = findpeaks(fft_results_all{ii},'MinPeakHeight',100);
semilogy(locs{ii},pks{ii},'.r')
title(fileList(ii).name,'Interpreter','none')
end
pks
pks = 1x4 cell array
{28x1 double} {21x1 double} {23x1 double} {28x1 double}
% append NaNs to each pks as necessary to make them the same length for
% plotting in boxplot
pks_plot = pks;
n = cellfun(@numel,pks_plot);
n_max = max(n);
for ii = 1:N
P = n_max-n(ii);
pks_plot{ii} = [pks_plot{ii}; NaN(P,1)];
end
figure;
boxplot([pks_plot{:}], 'Notch', 'on', 'Labels', {'7 Hz', '8 Hz', '9 Hz', '10 Hz'});
xlabel('Frequency (Hz)');
ylabel('FFT Amplitude');
title('Comparison of FFT Results for Different Frequencies');
  10 件のコメント
Aman Kumar
Aman Kumar 2024 年 4 月 10 日
Thank you for clarifying this, and again thank you for all the help and advice!
Voss
Voss 2024 年 4 月 10 日
You're welcome!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSignal Generation, Analysis, and Preprocessing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by