how to import file?

How to import all these csv files and extract only the 3 column from all these files and plot 3 column with respect to 2nd column data
T = readtable('3ghs_datafiles.csv','NumHeaderLines',3);%i have to eliminate 1 rows which is having header names a,b, r
a=T(:,3);
T1 = readtable('3.1ghs_datafiles.csv','NumHeaderLines',3);%i have to eliminate 1 rows which is having header names a,b, r
b=T1(:,3);
T2 = readtable('3.2ghs_datafiles.csv','NumHeaderLines',3);%i have to eliminate 1 rows which is having header names a,b, r
c=T2(:,3);
T3 = readtable('3.3ghs_datafiles.csv','NumHeaderLines',3);%i have to eliminate 1 rows which is having header names a,b, r
d=T3(:,3);
T4 = readtable('3.4ghs_datafiles.csv','NumHeaderLines',3);%i have to eliminate 1 rows which is having header names a,b, r
d=T4(:,3);
xaxis=120:0.1:120.5;
Final_T=cat(a,b,c,d);%i have to collect 3rd column data from all the files and store in 1 variable
Error using tabular/cat
Dimension must be 1 or 2 for a 2-D table.
plot(xaxis,Final_T)

Answers (2)

You were pretty close. Here is one approach, converting your third columns to arrays instead of extracting them as tables with only one column.
Adjust as desired.
T = readtable('3ghs_datafiles.csv','NumHeaderLines',3);%i have to eliminate 1 rows which is having header names a,b, r
a = table2array(T(:,3));
T1 = readtable('3.1ghs_datafiles.csv','NumHeaderLines',3);%i have to eliminate 1 rows which is having header names a,b, r
b = table2array(T1(:,3));
T2 = readtable('3.2ghs_datafiles.csv','NumHeaderLines',3);%i have to eliminate 1 rows which is having header names a,b, r
c = table2array(T2(:,3));
T3 = readtable('3.3ghs_datafiles.csv','NumHeaderLines',3);%i have to eliminate 1 rows which is having header names a,b, r
d = table2array(T3(:,3));
T4 = readtable('3.4ghs_datafiles.csv','NumHeaderLines',3);%i have to eliminate 1 rows which is having header names a,b, r
e = table2array(T4(:,3)); % <<< don't overwrite d
% xaxis=120:0.1:120.5;
whos
Name Size Bytes Class Attributes T 22x3 1943 table T1 22x3 1943 table T2 22x3 1943 table T3 22x3 1943 table T4 23x3 1967 table a 22x1 176 double ans 1x43 86 char b 22x1 176 double c 22x1 176 double cmdout 1x33 66 char d 22x1 176 double e 23x1 184 double
% Final_T=cat(a,b,c,d);%i have to collect 3rd column data from all the files and store in 1 variable
xaxis = linspace(120, 120.5, numel(a)); % xaxis with the same number of elements as your other variables
e = e(1:22,:); % trim off the extra row from e
Final_T = [a b c d/1e64 e/1000]; % scale d and e so you can see that a, b, and c aren't all zeros
plot(xaxis, Final_T)
legend({'a', 'b', 'c', 'd/1e64', 'e/1000'}, 'Location', 'northwest')
grid on
Here's one way:
f = [3, 3.1, 3.2, 3.3, 3.4];
Nf = numel(f);
C = cell(1,Nf);
filenames = compose('%gghs_datafiles.csv',f);
for ii = 1:Nf
C{ii} = readtable(filenames{ii});
end
T = vertcat(C{:});
plot(T.b,T.r,'.')
At least one of the files has huge values (~1e64) that prevent you from seeing what the other files' data looks like, so let me plot each file's data in a separate figure:
for ii = 1:Nf
figure
plot(C{ii}.b,C{ii}.r,'.')
title(filenames{ii},'Interpreter','none')
end

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Products

Release

R2022a

Tags

Asked:

on 12 Nov 2023

Answered:

on 16 Nov 2023

Community Treasure Hunt

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

Start Hunting!