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

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

 채택된 답변

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

I had the exact same thought process looking through the code. To add to this, figure, hold, grid, and axes limit functions all go outside the loop as follows because they are only needed once:
% Initialise figure
figure()
hold on
for ii = 1:length(files)
data = readmatrix(fullfile(files(ii).folder,files(ii).name), 'NumHeaderLines', 1);
x = data(:,10);
y = data(:,4);
% Plotting data
plot(x,y,'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
Hi, thanks for pointing out the mistake and advice. Once plotted I want to save the plotted data in the new excel file, can you advice me how can I do that?
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
" 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...

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

질문:

2021년 5월 9일

댓글:

dpb
2021년 5월 9일

Community Treasure Hunt

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

Start Hunting!

Translated by