How to import multiple excel files and pull data

조회 수: 13 (최근 30일)
Christina Reid
Christina Reid 2020년 12월 14일
댓글: Mathieu NOE 2020년 12월 15일
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
Sindar
Sindar 2020년 12월 15일
편집: Sindar 2020년 12월 15일
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)
Christina Reid
Christina Reid 2020년 12월 15일
Thank you for your help! :)

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

채택된 답변

Mathieu NOE
Mathieu NOE 2020년 12월 15일
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
Christina Reid
Christina Reid 2020년 12월 15일
Thank you, Matieu!! I implemented your code and it works!!!
Mathieu NOE
Mathieu NOE 2020년 12월 15일
Glad it helped !
good luck

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Import from MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by