Read the column 2 and 11 from 45 csv files in 45 subfolder hence plot them.

조회 수: 1 (최근 30일)
Hi,
I am using the code below to plot the diatance over U from 45 csv files but as soon as I am running it matlab is crashing.
What I want from the code. is to read all 45 csv files, extract 45 (column 2 and column 11 from the files and plot all in one graph)
Code:
close all; clear all; clc;
projectdir = 'F:\3-PIV_Experimental_Data\Outlet_110\Data_LaserSheet_D\Data_PIV\6-VectorSatistics\PlotOverLine';
files = dir( fullfile(projectdir, '*', '*.csv') );
filenames = fullfile({files.folder}, {files.name});
num_files = length(filenames);
Distance = zeros(num_files,1);
U = zeros(num_files,1);
for i = 1:numel(files);
filename = filenames{i};
data = readtable(filename);
Distance(i) = table2array (data(:,2));
U(i) = table2array (data(:,11));
end
Plot(Distance,U)
Error:
Unable to perform assignment because the left and right sides have a different number of elements.
Error in BONO1_LSD (line 22)
Distance(i) = table2array (data(:,2));
  댓글 수: 1
Aman Banthia
Aman Banthia 2022년 7월 6일
Hello,
Can you try removing semi colon at the end of for statement
for i = 1:numel(files)
filename = filenames{i};
data = readtable(filename);
Distance(i) = table2array (data(:,2));
U(i) = table2array (data(:,11));
end
Try to use it like this.

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

채택된 답변

KSSV
KSSV 2022년 7월 6일
편집: KSSV 2022년 7월 6일
Save the data into cells. If each file has different dimension.
close all; clear all; clc;
projectdir = 'F:\3-PIV_Experimental_Data\Outlet_110\Data_LaserSheet_D\Data_PIV\6-VectorSatistics\PlotOverLine';
files = dir( fullfile(projectdir, '*', '*.csv') );
filenames = fullfile({files.folder}, {files.name});
num_files = length(filenames);
Distance = cell(num_files,1);
U = cell(num_files,1);
figure
hold on
for i = 1:numel(files);
filename = filenames{i};
data = readtable(filename);
Distance{i} = table2array (data(:,2));
U{i} = table2array (data(:,11));
plot(Distance{i},U{i})
end
If the number of rows in each file is same and you know the number of rows.
close all; clear all; clc;
projectdir = 'F:\3-PIV_Experimental_Data\Outlet_110\Data_LaserSheet_D\Data_PIV\6-VectorSatistics\PlotOverLine';
files = dir( fullfile(projectdir, '*', '*.csv') );
filenames = fullfile({files.folder}, {files.name});
num_files = length(filenames);
N = 100 ; % where N is number of rows in each file
Distance = zeros(N,num_files);
U = zeros(N,num_files);
for i = 1:numel(files);
filename = filenames{i};
data = readtable(filename);
Distance(:,i) = table2array (data(:,2));
U(:,i) = table2array (data(:,11));
end
plot(Distance,U)
  댓글 수: 2
muhammad choudhry
muhammad choudhry 2022년 7월 6일
that works! what if I want to analyze specific files let say file 1 , 13 and 45. what should I do ?
KSSV
KSSV 2022년 7월 6일
Pick only those files from filenames by indexing.

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

추가 답변 (2개)

Karim
Karim 2022년 7월 6일
Distance(i) = table2array(data(:,2));
Here you are trying to store an entire array into a single placehoder. Unless data(:,2) is a scalar, this wont work.
If the size is the same for all files you can initialize Distance as a matrix:
Distance = zeros(num_files, num_rows_data);
in the loop you need to change the indexing to
Distance(i,:) = table2array(data(:,2));
If the files have different sizes, then you can save the data into a cell array. In this case allocate as
Distance = cell(num_files, 1);
and save data as
Distance{i} = table2array(data(:,2));

Walter Roberson
Walter Roberson 2022년 7월 6일
Distance(i) = table2array (data(:,2));
table2array() of a table with a single numeric variable, is going to produce one row of output for each row in data . You are expecting to store that entire row of output in the single numeric location Distance(i)
I suggest
Distancec = cell(num_files,1);
Uc = cell(num_files,1);
for i = 1:numel(files);
filename = filenames{i};
data = readtable(filename);
Distancec{i} = table2array (data(:,2));
Uc{i} = table2array (data(:,11));
end
Distance = cell2mat(Distancec);
U = cell2mat(Uc);
plot(Distance, U)
... Except that I suspect you will find that confusing, as there will be no breaks between the files.
Do you want one line per file? If so then try
hold on
cellfun(@(d,u) plot(d,u), Distancec, Uc);
legend(filenames);
hold off
xlim auto; ylim auto

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by