Import data from several .xlsx and different columns
3 views (last 30 days)
Show older comments
Hi. I would like to import data from several .xlsx files. Currently, I have a reference .xlsx file that includes the locations of the target .xlsx files (in Column A) and two columns (D, F) that include the column reference for the data in the target .xlsx files I would like to import.
example:
- reference .xlsx file includes 1 column with file locations and 2 columns with column letters (AJ, F, etc.).[Reference .xlsx: Column A "C:\...\1.xlsx" and Column C "S" and Column D "AP"]
- matlab associates the file to the column letters (by row)
- import of date from column of the according .xlsx[Target .xlsx: Column S "123.42" and Column AP "213.32"]
I hope this is somehow understandable. Do you have any idea on how to do this? The script so far:
[~, ~, raw] = xlsread('C:\...\reference.xlsx','Worksheet1','A2:F22');
raw(cellfun(@(x) ~isempty(x) && isnumeric(x) && isnan(x),raw)) = {''};
cellVectors = raw(:,[1,2,3,4,6]);
raw = raw(:,5);
R = cellfun(@(x) ~isnumeric(x) && ~islogical(x),raw); % Find non-numeric cells
raw(R) = {NaN}; % Replace non-numeric cells
data = reshape([raw{:}],size(raw));
A = cellVectors(:,1);
B = cellVectors(:,2);
C = cellVectors(:,3);
D = cellVectors(:,4);
E = data(:,1);
F = cellVectors(:,5);
finalData = xlsread({'A', 'C'); % this part is the issue
clearvars data raw cellVectors R i;
0 Comments
Accepted Answer
dpb
on 9 Oct 2015
Looks way too complicated -- something like
[~,txt]=xlsread('C:\...\reference.xlsx');
for i=1,size(txt,1)
C1C2=[txt(i,N1) ':' txt(i,N2)]; % build column references
data=xlsread(txt{i,1},C1C2); % read the range
... % do whatever with data here
end
should be about all that's needed. I assumed the file name is fully qualified and in first column to open the file and that the two column ranges are in columns N1 and N2, respectively. "Salt to suit..."
0 Comments
More Answers (0)
See Also
Categories
Find more on Spreadsheets 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!