Matlab read .csv file together with headlines and operations on matrix elements?

6 views (last 30 days)
Hi to everyone,
I need help regarding reading csv file where Ill be able to access and read headlines:
this is the csv file and I need first row, headline too.
freq Unit1 Unit2 Unit3 Unit4 Unit5 Unit6 Unit7 Unit8 Unit9 Unit10
50 2.0614 4.5103 3.918 3.4831 1.56 4.884 1.621 3.9072 1.6264 3.9601
150 1.811 3.907 2.4978 3.3853 2.42 3.397 3.7612 2.1432 0.67351 1.566
250 1.4037 0.67744 1.4482 0.74906 0.64507 2.575 0.6273 0.95441 0.535 2.2526
My code is:
filename1 = 'DBF.csv';
DB = readtable(filename1,'ReadVariableNames', false);
But when I am trying to read headline, I am getting this:
Unit1
_____
1.811
Now I am confused as I cant work with numbers nor with strings? How to get only values and strings from headline?
Second question is regarding IF statement where I am taking elements of matrix to test:
nrows=3; ncols=11;
for c=2:ncols
det=0;
for r=1:nrows
low=LM*DB(r,c); high=HM*DB(r:c); Here I am getting the error most likely as DB(r,c) is as I show.
if low < NU(r:2) && NU(r:2) < high
det=det+1;
else det=0;
end
if det==3
fprintf('Newly connected unit is %d\n', DB(c:1));
end
end
end
if det==0
fprintf('Newly connected unit cannot be rcognised! %d\n');
end
Hopefully someone can help in this.
Many thanks in advance.

Accepted Answer

Star Strider
Star Strider on 24 Aug 2022
The readtable function call may be in error if you want to import the variable names, however I don’t know from the code what you want to do.
I would do something like this —
DB = readtable(filename1);
VN = DB.Properties.VariableNames
Ths second line saves them to ‘VN’ as a cell array. You can do whatever you want to with them after that. (The current version of readtable also supports 'VariableNamingRule','preserve' to read variable names that are not vallid MATLAB variable names. A previous version of that exists as 'PreserveVariableNames',true , however I don’t remember when it was introduced. It may not be important here if the variable names are valid MATLAB variable names.)
  8 Comments
Mike Pierie
Mike Pierie on 24 Aug 2022
I realized that problem was in the %d which is for integers and % should be for the strings... Now working everyhing!!!
Guys I am so thankful to you, especially to Mr. Star Strider.
Step 2 is done.. There will be Step 3 too and I am working on it now.
Have an excellent time there where you are!

Sign in to comment.

More Answers (1)

Cris LaPierre
Cris LaPierre on 24 Aug 2022
Moved: Cris LaPierre on 24 Aug 2022
Please share your csv file. You can attach it using the paperclip icon.
A preliminary look would suggest the code you have shared is not the same code that is used to read the csv file, as the output shows that the variable names have been read in.
As an example, I saved the file contents you shared to a csv and loaded it using your code. As you can see, it read in all the data, so I suspect there are some missing details.
filename1 = 'DBF.csv';
DB = readtable(filename1,'ReadVariableNames', false)
DB = 3×11 table
Var1 Var2 Var3 Var4 Var5 Var6 Var7 Var8 Var9 Var10 Var11 ____ ______ _______ ______ _______ _______ _____ ______ _______ _______ ______ 50 2.0614 4.5103 3.918 3.4831 1.56 4.884 1.621 3.9072 1.6264 3.9601 150 1.811 3.907 2.4978 3.3853 2.42 3.397 3.7612 2.1432 0.67351 1.566 250 1.4037 0.67744 1.4482 0.74906 0.64507 2.575 0.6273 0.95441 0.535 2.2526
If you are unfamiliar with tables, I recommend visiting the Access data in Tables page. Or you can use readmatrix instead.
DB = readmatrix(filename1)
DB = 3×11
50.0000 2.0614 4.5103 3.9180 3.4831 1.5600 4.8840 1.6210 3.9072 1.6264 3.9601 150.0000 1.8110 3.9070 2.4978 3.3853 2.4200 3.3970 3.7612 2.1432 0.6735 1.5660 250.0000 1.4037 0.6774 1.4482 0.7491 0.6451 2.5750 0.6273 0.9544 0.5350 2.2526
  5 Comments
Mike Pierie
Mike Pierie on 24 Aug 2022
and also is it possible to automatize it in order to check entire file and not only one of the columns?
I mean Unit1 2 3 4... in DB.Unit expression.
Cris LaPierre
Cris LaPierre on 24 Aug 2022
I don't know what it is you are wanting to compare, but yes, the column number is the same for the data and the variables.
For a table, use the width and height methods to get the number of columns and rows, respectively.
The page on accessing data in tables I linked to previously is helpful, as there are multiple ways to access the data. You can find the one that works best for you.
filename = 'https://www.mathworks.com/matlabcentral/answers/uploaded_files/1106800/DBF.csv';
DB = readtable(filename)
DB = 3×11 table
freq Unit1 Unit2 Unit3 Unit4 Unit5 Unit6 Unit7 Unit8 Unit9 Unit10 ____ ______ _______ ______ _______ _______ _____ ______ _______ _______ ______ 50 2.0614 4.5103 3.918 3.4831 1.56 4.884 1.621 3.9072 1.6264 3.9601 150 1.811 3.907 2.4978 3.3853 2.42 3.397 3.7612 2.1432 0.67351 1.566 250 1.4037 0.67744 1.4482 0.74906 0.64507 2.575 0.6273 0.95441 0.535 2.2526
width(DB)
ans = 11
height(DB)
ans = 3
DB.Unit1(2) % 2nd value in Unit 1 column
ans = 1.8110
DB.Unit1(2,1) % 2nd value in Unit 1 column
ans = 1.8110
DB{2,2} % 2nd value in 2nd column of DB (Unit 1)
ans = 1.8110
DB.(DB.Properties.VariableNames{2})(2) % 2nd value in 2nd variable column (Unit 1)
ans = 1.8110
DB.(string(DB.Properties.VariableNames{2}))(2) % 2nd value in 2nd variable column (Unit 1)
ans = 1.8110
To check values across all variables, assuming all columns contain numeric data, you would use curly braces to extract the table data as an array (the same result you would get by using readmatrix).
db=DB{:,:} % all rows, all columns
db = 3×11
50.0000 2.0614 4.5103 3.9180 3.4831 1.5600 4.8840 1.6210 3.9072 1.6264 3.9601 150.0000 1.8110 3.9070 2.4978 3.3853 2.4200 3.3970 3.7612 2.1432 0.6735 1.5660 250.0000 1.4037 0.6774 1.4482 0.7491 0.6451 2.5750 0.6273 0.9544 0.5350 2.2526
db(2,2) % 2nd row, 2nd column
ans = 1.8110

Sign in to comment.

Categories

Find more on MATLAB in Help Center and File Exchange

Products


Release

R2016a

Community Treasure Hunt

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

Start Hunting!