How do we plot graphs from excel file?

13 ビュー (過去 30 日間)
Anu
Anu 2021 年 12 月 1 日
コメント済み: Anu 2021 年 12 月 3 日
I have 100 output files in a folder. The output files are named as output1.xlsx, output2.xlsx, output3.xlsx, and so on. There are two sheets, the second sheet (final graph) is the one that contains data. Please see attached the file. I need to plot for 100 such files.The x axis is the time points i.e., from 0 to 100%. The y axis is the frequency distribution for each variable, img1_F, img2_F, img3_F, img4_M and so on. This frequency distribution over the x axis needs to be plotted for image1 and image2 in the same plot. The title of the plot is the name of the file, in this case it is output1. I need to plot each of them and save as .png in a folder. May I know how should I do it?
Thanks in advance!

採用された回答

Mathieu NOE
Mathieu NOE 2021 年 12 月 1 日
hello
this is my suggestion, hope it helps
clc
clearvars
fileDir = pwd;
sheet = 2; % data must be retrieved from sheet #2
S = dir(fullfile(fileDir,'output*.xlsx')); % get list of data files in directory
S = natsortfiles(S); % sort file names into natural order (what matlab does not) , see FEX :
%(https://fr.mathworks.com/matlabcentral/fileexchange/47434-natural-order-filename-sort)
legstr = [{'img1 F'};{'img2 F'};{'img3 F'};{'img4 M'};{'img5 M'};{'img6 M'};{'img8 F'};{'img9 M'};{'img10 F'}];
for k = 1:length(S)
data = xlsread(fullfile(fileDir, S(k).name),sheet); % or use a structure (S(k).data ) to store the full data structure
% data array must be splitted in two (fig 1 and fig 2 ) by checking the
% NaN position
ind_nan = find(isnan(data(1,:)));
cols = ind_nan-1;
x1 = data(1,1:cols);
y1 = data(2:end,1:cols);
x2 = data(1,end-cols+1:end);
y2 = data(2:end,end-cols+1:end);
% plot
filename_fig = S(k).name;
ind = findstr(filename_fig,'.');
filename_fig = filename_fig(1:ind-1);
% figure(k); % will create k figures
figure(1); % will create 1 figure only (updated after each new data) => faster batch processing
sg = sgtitle(filename_fig);
sg.FontSize = 20;
sg.FontWeight = 'bold';
subplot(121),plot(x1*100,y1);
xlabel('Percentage');
ylabel('f0 (Hz)');
xtickformat('percentage');
subplot(122),plot(x2*100,y2);
ll= legend(legstr,'Location','South','Orientation','horizontal');
set(ll,'Position',[0.2119 0.015 0.6332 0.0323])
xlabel('Percentage');
ylabel('f0 (Hz)');
xtickformat('percentage')
print(filename_fig, '-dpng')
end
  8 件のコメント
Mathieu NOE
Mathieu NOE 2021 年 12 月 3 日
My pleasure !
would you mind accepting my answer ?
all the best
Anu
Anu 2021 年 12 月 3 日
It's my pleasure too @Mathieu NOE! Thanks!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by