New variable for each xlsread from a different file

2 ビュー (過去 30 日間)
Harrison Schipke
Harrison Schipke 2018 年 10 月 30 日
編集済み: Stephen23 2018 年 10 月 31 日
First time using Matlab so I have no clue about this.
For my thesis, I have a number of excel files from collecting data. The tables in each file are the same dimensions representing the same data type (freq vs dB).
I want to import these to Matlab so I can graph them individually and also then average them together (since they are just test runs of the same set-up).
In the loop, I want it to spit out variables SPL_1, SPL_2, SPL_3 etc.
Right now this is my code. It gathers the file list fine. But I can't work out how to create a new SPL variable for each time it reads from an excel file.
%====IMPORT ACOUSTIC TEST DATA FROM N63 COMFORT EXCEL .CSV FILES====%
%State constant variables%
alpha = 1;
s_source = 2;
a_recieve = 3;
spl_IL_= 4;
%Frequency Column
freq = xlsread(freq_data,'A2:A102');
%Import Data - Excel Loop%
Files = dir('*.xlsx');
fnames = {Files.name};
filename = fnames';
%Determine Size for Loop
D = size(filename);
Numloop = D(1,1);
%Loop to Create Paths for each file
for i = 1:Numloop
run = filename(i,1)
excel_sheet = run{1}
SPL_1 = xlsread(excel_sheet,'B2:B102')
end
  3 件のコメント
Harrison Schipke
Harrison Schipke 2018 年 10 月 30 日
Thanks for your reply.
I don't understand how Processing a sequence of files will give me the data I need? I have already got by xlxs files. I just need to import a column from each file and then use that column to plot a line graph
Stephen23
Stephen23 2018 年 10 月 30 日
編集済み: Stephen23 2018 年 10 月 30 日
"I don't understand how Processing a sequence of files will give me the data I need?"
That link shows how to use a cell array to store the imported data.

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

採用された回答

Stephen23
Stephen23 2018 年 10 月 30 日
編集済み: Stephen23 2018 年 10 月 31 日
As shown in the MATLAB documentation
you could simply use cell array to store the imported data:
D = 'directory where the files are saved';
S = dir(fullfile(D,'*.xlsx'));
N = numel(S)
C = cell(1,N)
for k = 1:N
F = fullfile(D,S(k).name);
C{k} = xlsread(F,'B2:B102')
end
Or just use the structure returned by dir:
D = 'directory where the files are saved';
S = dir(fullfile(D,'*.xlsx'));
for k = 1:numel(S)
F = fullfile(D,S(k).name);
S(k).data = xlsread(F,'B2:B102')
end
Both of them are then trivially and efficiently accessed using indexing, or with comma-separated lists:
  2 件のコメント
Harrison Schipke
Harrison Schipke 2018 年 10 月 31 日
編集済み: Harrison Schipke 2018 年 10 月 31 日
Thanks for the advice. I appreciate it!
So now if I want to use my data (with cell array) I would just say C(1,1) and I could then use that column (100 rows) of data? How could I average the data for each row? So as an example C(1,1) is 101x1, C(1,2) is 101x1, and C(1,3) is 101x1. Can I make the each row of data be averaged using this cell aray?
This is what I have so far (with some name changes)
%Frequency Column
freq = xlsread('N63_C_Run_1.xlsx','A2:A102');
%Get File Names
S = dir(fullfile('*.xlsx'));
N = numel(S);
%State Variables and Obtain Excel Data
SPL_1_Runs = cell(1,N);
SPL_2_Runs = cell(1,N);
for k = 1:N
F = fullfile(S(k).name);
SPL_1_Runs{k} = xlsread(F,'B2:B102');
SPL_2_Runs{k} = xlsread(F,'C2:C102');
end
%Combine Cell Array to Matrix
SPL_1 = cell2mat(SPL_1_Runs);
SPL_2 = cell2mat(SPL_2_Runs);
Stephen23
Stephen23 2018 年 10 月 31 日
編集済み: Stephen23 2018 年 10 月 31 日
Each cell of SPL_1_Runs and SPL_2_Runs contains the data that you imported. You can access the contents using cell array indexing (which is why I gave you the links to read):
SPL_1_Runs{1} % data from 1st file
SPL_1_Runs{2} % data from 2nd file
SPL_1_Runs{3} % data from 3rd file
... etc
If they all have the same number of columns, then you can combine them into one numeric matrix (using a comma-separated list, see the links I gave you):
M = [SPL_1_Runs{:}]
which you can then simply plot like this:
plot(M)
To get the average just use mean:
mean(M,2)

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

その他の回答 (1 件)

madhan ravi
madhan ravi 2018 年 10 月 30 日

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by