For Loop only showing data for last file within Array

I am trying to create a script that will load multiple xlsx files into one variable. When I run the code, I am able to see the correct number of files being processed however the variable I created to store files only shows the last files processed in the loop. *** I'm a beginner*** please let me know what I need to do so that the variable I created to store the files ( data_in) will no longer read as a 1x73 double rather a 3x73 double.
clear all; clc
[file_list, path_n] = 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 = xlsread([path_n filename]);
end

 採用された回答

Voss
Voss 2022 年 6 月 9 日
編集済み: Voss 2022 年 6 月 9 日

0 投票

Make data_in a cell array, and store the contents of each file in a different cell of data_in:
clear all; clc
[file_list, path_n] = uigetfile('.xlsx', 'Grab the files you want to process', 'MultiSelect', 'on');
if ~iscell(file_list)
file_list = {file_list};
end
n_files = numel(file_list);
data_in = cell(n_files,1)
for ii = 1:n_files
filename = file_list{ii};
data_in{ii} = xlsread([path_n filename]);
end
Then vertically concatenate the contents of all the cells of data_in. This will only work if all the cells' contents have the same number of columns.
data_in = vertcat(data_in{:});
% if each file contains a single row of data with 73 columns,
% then data_in is now a n_files-by-73 matrix

2 件のコメント

Donovyn Murray
Donovyn Murray 2022 年 6 月 9 日
That seems to have worked. All my data will populate in 73 columns so as I continue to add more files this should work out. Thank you.
Voss
Voss 2022 年 6 月 9 日
You're welcome!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeGet Started with MATLAB についてさらに検索

質問済み:

2022 年 6 月 9 日

コメント済み:

2022 年 6 月 9 日

Community Treasure Hunt

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

Start Hunting!

Translated by