How to import multiple excel files and pull data

12 views (last 30 days)
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 Comments
Sindar
Sindar on 15 Dec 2020
Edited: Sindar on 15 Dec 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)

Sign in to comment.

Accepted Answer

Mathieu NOE
Mathieu NOE on 15 Dec 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)

More Answers (0)

Categories

Find more on Data Import from MATLAB in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!