How to read the specific colums from 12 excel files in a folder and then plot it

4 visualizaciones (últimos 30 días)
Hi,
I want to read column 4 and 10 from each excel file in the folder and then plot column 4 and 10 of each file in one graph. So far I tried the code below but it is only plotting one graph. Any help would be appreciated.
Code:
folder = fullfile('C:','Users','muhammad','Documents','PhD_1stYr','Experiments_2021','performance_110');
files = dir( fullfile(folder, '*.ods') );
% Reading and extracting data from 12 excel files hence plotting
for ii = 1:length(12)
data = readmatrix(fullfile(files(ii).folder,files(ii).name), 'NumHeaderLines', 1)
x= data(:,10)
y=data(:,4)
figure(1)
% Plotting data
plot(x,y,'x','LineWidth',0.5);
hold on
%Spacing and griding
xlim([0.237 5])
ylim([0.237 0.8])
hold on
grid on
end

Respuesta aceptada

dpb
dpb el 9 de Mayo de 2021
Other than superfluous stuff that doesn't need to be in the loop, nothing appears to be wrong in the basic code...
Oh! Now I see it--
for ii = 1:length(12)
"12" is a constant; length(12) is 1.
You intended to write
for ii = 1:length(files)
  4 comentarios
dpb
dpb el 9 de Mayo de 2021
Well, since we go off on to style/efficiency.. :)
Carrying on with Turlough's efforts--
% Initialise figure
figure()
hold on
for ii = 1:length(files)
data = readmatrix(fullfile(files(ii).folder,files(ii).name), 'NumHeaderLines', 1);
% no need to build temporaries; just reference the arrays
plot(data(:,10),data(:,4),'x','LineWidth',0.5);
end
% modifications to plot are only implemented once and hence go outside the loop
xlim([0.237 5])
ylim([0.237 0.8])
grid on
dpb
dpb el 9 de Mayo de 2021
" want to save the plotted data in the new excel file..."
What, specifically, do you want to save? Just the subset of columns extracted from the other files, or the plots themselves? The latter is COM exercise that would take knowing the VBA equivalents for in Excel that I don't know...
Presuming it's to just put the data into one location, the simplest would be to preallocate an array of [size(data,1) by 2*size(length(files))] elements to hold the x,y data each pass through the loop and then call writematrix with a new filename and that data after the loop completes.
That presumes each file is identically the same length; if that is not the case, then will have to use a cell array to hold the data and writecell instead.
It's not too bad, give it a go and if run into problems, show us your code and where you had a problem...

Iniciar sesión para comentar.

Más respuestas (0)

Community Treasure Hunt

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

Start Hunting!

Translated by