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

1 回表示 (過去 30 日間)
muhammad choudhry
muhammad choudhry 2022 年 7 月 6 日
コメント済み: KSSV 2022 年 7 月 6 日
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 Center および File ExchangeGraphics Object Identification についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by