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 个)

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 中查找有关 Graphics Object Identification 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by