How to import multiple excel files and pull data

I am trying to pull multiple files from excel and extract data to create a plot, however I am not getting the plot. Below is my code:
clear all; clc
[file_list, path_n] = uigetfile ('.xlsx','Grab the files you want to proocess', 'Multiselect', 'on');
if iscell(file_list) == 0
file_list = (file_list);
end
for i = 1:length(file_list)
data_in{i} = xlsread([path_n file_list{i}]);
p1{i} = data_in{i}(:,2);
p2{i} = data_in{i}(:,1);
p3{i} = data_in{i}(:,3);
p_avg{i} = ((p1{i}+p2{i}))./2;
cp_a{i} = (p3{i}-p2{i})./(p1{i}-p_avg{i});
if rem(i,2) == 0.
cp_avg_max{i} = max(cp_a{i});
else cp_avg_max{i} = min(cp_a{i});
end
figure
alpha = -10:1:10
plot (alpha, cp_avg_max{i});
end

4 comentarios

Mathieu NOE
Mathieu NOE el 14 de Dic. de 2020
hello
do you have a couple of excel files to share , to test your code ?
I just attached the documents. I am trying to plot alpha vs. cp_a_min and cp_a_max. Each alpha angle is the name if the excel file, i.e -10.xlsx represents alpha= -10. Also, here is an updated code.
clear all;
clc
% It is important to grab the files in ascending order
file_list = uigetfile('*.xlsx', 'Grab the files you want to process', 'MultiSelect', 'on');
if iscell(file_list) == 0
file_list = {file_list};
end
for i = 1:length(file_list)
filename = file_list{i};
data_in{i} = xlsread(filename);
p1{i} = data_in{i}(:,2);
p2{i} = data_in{i}(:,1); % Pressure at hole 2
p3{i} = data_in{i}(:,3); % pressure at hole 3
P_avg{i} = (p2{i}+p3{i})./2;
Cp_a{i} = ((p3{i}-p2{i})./(p1{i}-P_avg{i}));
cp_a_min(i) = min(Cp_a{i});
cp_a_max(i) = max(Cp_a{i});
end
x = -10:1:10;
plot(x,cp_a_min)
I got it to run, but noticed a few bugs I would love to be fixed.
  1. I noticed I have to select each file in the oder that I want (ascending order from -10 to 10) for the plot to be correct, but I would love for the code to automatically sort it.
  2. I hard coded by x-axis to be -10:1:10, but I would love for it to be more dynamic and be based off of the excel file the user selects. Lets say the user only selects -10.xlsx,-9.xlsx, and -8.xlsx then the orginal x-axis I defined won't work beccause that is a 1x21 array and this 'new' cp_a_min/cp_a_max array is a 1x3 and x should then be -10:1:-8. is there a way to do that in Matlab?
Sindar
Sindar el 15 de Dic. de 2020
Editada: Sindar el 15 de Dic. de 2020
You could read the x values from the file names:
...
end
% trim the extensions, then convert to array of doubles
x = str2double(extractBefore(file_list,'.x'));
plot(x,cp_a_min)
Thank you for your help! :)

Iniciar sesión para comentar.

 Respuesta aceptada

Mathieu NOE
Mathieu NOE el 15 de Dic. de 2020
hello again
so , this is my code , it also includes the suggestion of Sindar along the two other options i implemented
IMHO, regexp is the best and most robust / verstaile way to go (even if here the two other options work also in this specific case)
it will continue to work with regexp, even if you change tomorrow your filenames significantly , like filename = ' blabla -10 trucmuch . txt' - no need to modify the code
% It is important to grab the files in ascending order
file_list = uigetfile('*.xlsx', 'Grab the files you want to process', 'MultiSelect', 'on');
if iscell(file_list) == 0
file_list = {file_list};
end
% sort file list in ascending order
% ind = sscanf([file_list{:}],'%d.xlsx') % my first idea (it works unless you add strings also in the file name)
ind = str2double(regexp([file_list{:}],'[+-]?\d+\.?\d*', 'match')); % my second idea , returns positive and negative index included in the file name
% ind = str2double(extractBefore(file_list,'.xls')); % another good idea from Sindar
[x,ii] = sort(ind,'ascend');
file_list = file_list(ii);
for i = 1:length(file_list)
filename = file_list{i};
data_in{i} = xlsread(filename);
p1{i} = data_in{i}(:,2);
p2{i} = data_in{i}(:,1); % Pressure at hole 2
p3{i} = data_in{i}(:,3); % pressure at hole 3
P_avg{i} = (p2{i}+p3{i})./2;
Cp_a{i} = ((p3{i}-p2{i})./(p1{i}-P_avg{i}));
cp_a_min(i) = min(Cp_a{i});
cp_a_max(i) = max(Cp_a{i});
end
plot(x,cp_a_min)

2 comentarios

Thank you, Matieu!! I implemented your code and it works!!!
Mathieu NOE
Mathieu NOE el 15 de Dic. de 2020
Glad it helped !
good luck

Iniciar sesión para comentar.

Más respuestas (0)

Preguntada:

el 14 de Dic. de 2020

Comentada:

el 15 de Dic. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by