How do we plot graphs from excel file?

5 views (last 30 days)
Anu
Anu on 1 Dec 2021
Commented: Anu on 3 Dec 2021
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!

Accepted Answer

Mathieu NOE
Mathieu NOE on 1 Dec 2021
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 Comments

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by