フィルターのクリア

How to import multiple excel files and pull data

4 ビュー (過去 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 Center および File ExchangeData Import from MATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by