Matlab read .csv file together with headlines and operations on matrix elements?
6 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
Star Strider
2022-8-24
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 个评论
更多回答(1 个)
Cris LaPierre
2022-8-24
移动:Cris LaPierre
2022-8-24
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)
If you are unfamiliar with tables, I recommend visiting the Access data in Tables page. Or you can use readmatrix instead.
DB = readmatrix(filename1)
5 个评论
Cris LaPierre
2022-8-24
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.
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)
width(DB)
height(DB)
DB.Unit1(2) % 2nd value in Unit 1 column
DB.Unit1(2,1) % 2nd value in Unit 1 column
DB{2,2} % 2nd value in 2nd column of DB (Unit 1)
DB.(DB.Properties.VariableNames{2})(2) % 2nd value in 2nd variable column (Unit 1)
DB.(string(DB.Properties.VariableNames{2}))(2) % 2nd value in 2nd variable column (Unit 1)
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(2,2) % 2nd row, 2nd column
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!