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

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.

 Réponse acceptée

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 commentaires

Hi Star Sider and thanks for the quick answer.
I would like to import those data into Matlab in order to make some comparsion of values and as output should be: Unit match with existing one or doesnt match.
Ill try what you told me.
I found somewhere yours comment and recommendation to use "readcell" here is what I am getting in both cases:
readcell:
Columns 1 through 9
{'freq'} {'Unit1' } {'Unit2' } {'Unit3' } {'Unit4' } {'Unit5' } {'Unit6' } {'Unit7' } {'Unit8' }
{[ 50]} {[2.0614]} {[4.5103]} {[3.9180]} {[3.4831]} {[1.5600]} {[4.8840]} {[1.6210]} {[3.9072]}
{[ 150]} {[1.8110]} {[3.9070]} {[2.4978]} {[3.3853]} {[2.4200]} {[3.3970]} {[3.7612]} {[2.1432]}
{[ 250]} {[1.4037]} {[0.6774]} {[1.4482]} {[0.7491]} {[0.6451]} {[2.5750]} {[0.6273]} {[0.9544]}
Columns 10 through 11
{'Unit9' } {'Unit10'}
{[1.6264]} {[3.9601]}
{[0.6735]} {[1.5660]}
{[0.5350]} {[2.2526]}
and VN = DB.Properties.VariableNames;
Columns 1 through 9
{'freq'} {'Unit1'} {'Unit2'} {'Unit3'} {'Unit4'} {'Unit5'} {'Unit6'} {'Unit7'} {'Unit8'}
Columns 10 through 11
{'Unit9'} {'Unit10'}
Can I use that for the purpose of my code?
My pleasure!
I didn’t specifically mention readcell here, although I have in answers to other questions. The first row of the readcell result should produce the same as the ‘VN’ variable with readtable.
I would use readtable for this, however I’m still not certain that I understand what you want to do. If you want to extract the data from the ‘DB’ table use the table2array function, although it is relatively straightforward to address the individual variables (columns) of the table by using the variable names, as well as referencing them as cells with numerical indices.
With respect to the loop in your original post, the element references in the if block (and elsewhere as necessary) should likely be to ‘DB{r,c}’ using curly brackets instead of parentheses, since ‘DB’ is a table.
EDIT — (24 Aug 2022 at 15:48)
I didn’t see the file you posted until just now.
DB = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1106800/DBF.csv')
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
VN = DB.Properties.VariableNames
VN = 1×11 cell array
{'freq'} {'Unit1'} {'Unit2'} {'Unit3'} {'Unit4'} {'Unit5'} {'Unit6'} {'Unit7'} {'Unit8'} {'Unit9'} {'Unit10'}
FirstVariable = VN{1}
FirstVariable = 'freq'
LastVariable = VN{end}
LastVariable = 'Unit10'
So if you need the variable names as character arrays, just use cell addressing to return them as such.
.
aaaa great!!! {r,c} this is what I needed! Now it reading elements as numbers, great!
Now next point is:
if low < NU{r,2} && NU{r,2} < high
in the loop where I am getting error: Operands to the || and && operators must be convertible to logical scalar values.
for c=2:ncols
det=0;
for r=1:nrows
low=LM*C{r,c}; high=HM*C{r,c};
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', C{1,c});
end
end
end
if det==0
fprintf('Newly connected unit cannot be rcognised! %d\n');
end
Also how I can acess now and use the names, strings Unit1,2,3.. after VN = DB.Properties.VariableNames; ?
Many many thanks to both of you!
Ths ‘short circuit’ operators only operate on logical scalars.
Try this —
DB = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1106800/DBF.csv')
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
VN = DB.Properties.VariableNames
VN = 1×11 cell array
{'freq'} {'Unit1'} {'Unit2'} {'Unit3'} {'Unit4'} {'Unit5'} {'Unit6'} {'Unit7'} {'Unit8'} {'Unit9'} {'Unit10'}
% FirstVariable = VN{1}
% LastVariable = VN{end}
LM = 1; % Create These
HM = 10;
[nrows,ncols] = size(DB)
nrows = 3
ncols = 11
for c=2:ncols
det=0;
for r=1:nrows
low=LM*DB{r,c};
high=HM*DB{r,c};
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
Undefined variable 'NU'.
if det==0
fprintf('Newly connected unit cannot be rcognised! %d\n');
end
I have no idea what ‘NU’ is, however its absence now appears to be the only problem with the code.
.
Mike Pierie
Mike Pierie le 24 Août 2022
Modifié(e) : Mike Pierie le 24 Août 2022
Once more again, thanks to the sky!
Almost done and here is the code:
clc;
clear all;
close all;
filename1 = 'DBF.csv'; % parameters for all recorded units
filename2 = 'PNU.csv'; % parameters of new unit
LM=0.8; % Margin 1 - 20% below
HM=1.2; % Margin 2 - 20% above
% C = readcell(filename1);
% disp(C);
DB = readtable(filename1,'ReadVariableNames', true); % parameters of all recorded units
VN = DB.Properties.VariableNames;
[nrows,ncols] = size(DB);
NU = readtable(filename2,'ReadVariableNames', true); % parameters of newly connected unit
VNNU = NU.Properties.VariableNames; % is this correct?
for c=2:ncols
det=0;
for r=1:nrows
low=LM*DB{r,c};
high=HM*DB{r,c};
newunitH=NU{r,2};
if low < newunitH & newunitH < high
det=det+1;
else det=4
end
disp(det);
if det==3
% at 1st pass det is 3 but fprint doesnt print it well. disp(VN{c}) showing correct header.
% Most likely I put in
% wrong way VN{c} in fprint as is showing following:
% Newly connected unit is 85
% Newly connected unit is 110
% Newly connected unit is 105
% Newly connected unit is 116
% Newly connected unit is 49
% instead "Newly connected unit is Unit1"
disp(c);
disp(VN{c});
fprintf('Newly connected unit is %d\n', VN{c});
end
end
end
if det==0
fprintf('Newly connected unit cannot be rcognised! %d\n');
end
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!
As always, my pleasure!

Connectez-vous pour commenter.

Plus de réponses (1)

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 commentaires

Hi Cris and manz thanks for quick response!
I attached the file.
I found that readtable is somehow easiest but I cant get
Unit1 Unit2 Unit3 Unit4 Unit5 Unit6 Unit7 Unit8 Unit9 Unit10 as strings and the rest as numbers.
Ill check the link you provided.
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
% Capturing variable names
varNms = string(DB.Properties.VariableNames)
varNms = 1×11 string array
"freq" "Unit1" "Unit2" "Unit3" "Unit4" "Unit5" "Unit6" "Unit7" "Unit8" "Unit9" "Unit10"
% Extract data from one column (all values vs specific values)
DB.Unit1
ans = 3×1
2.0614 1.8110 1.4037
DB.Unit2(2:3)
ans = 2×1
3.9070 0.6774
Hi Cris, I ovelooked your 2nd comment and thanks again for it.
It seams with varNms = string(DB.Properties.VariableNames) I can get var names and hopefully connect with the evaluation of values. Once when Il detect correct case to print the name of appropriate Unit.
Can I use index to acces to particular elemet of DB like DB.Unit1(1,1) as input for evaluation maybe? Ill try immediatelly when Ill be home.
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.
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

Connectez-vous pour commenter.

Catégories

Produits

Version

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by