필터 지우기
필터 지우기

reading the data from a csv file with headers

조회 수: 493 (최근 30일)
hoda kazemzadeh
hoda kazemzadeh 2018년 6월 11일
댓글: Star Strider 2018년 7월 26일
Hi, I have a csv file similar to this:
a b c d
12 35 86 4
How can I read the data for one of the headers? Knowing the name of the file and the headers, is it possible to read the number associated with that header?

채택된 답변

Star Strider
Star Strider 2018년 6월 11일
If you have R2013b or later, use the readtable (link) function.
Example
To get the data in ‘c’:
Tbl = readtable('YourFile.csv');
cv = Tbl.c; % Easiest, Most Direct
Alternatively, you can recover the column associated with each header (column name) by using ‘Properties.VariableNames’ to retrieve them. Then use strcmp to get the logical vector of matching column names, and find to get the column number. :
VarNames = Tbl.Properties.VariableNames; % Recover Variable Names (Column Titles)
ColIdx = find(strcmp(VarNames, 'c')); % Return Column Index For Variable Name ‘c’
Then use ‘ColIdx’ to access the column data in variable ‘c’.
Experiment to get the result you want.
  댓글 수: 10
hoda kazemzadeh
hoda kazemzadeh 2018년 7월 26일
This is the csv file. serialnumber is not always array of numbers. it may contains letters (as it has in the file) so num2str does not work for that. my problem is that serialnumber could be as both types (only numbers or numbers/letters) and it depends on the type of the sensor that may change.
Star Strider
Star Strider 2018년 7월 26일
The ‘serialnumber’ is a hexadecimal number that is read as a string in the cell array. You can retrieve it as a string easily, and you do not have to do any conversions:
Tb = readtable('configuration.csv');
Vars = Tb.Properties.VariableNames;
serialnumidx = find(contains(Vars,'SerialNumber'));
serialnumv = Tb{1,serialnumidx};
serialnum = serialnumv{1};
serialnum =
'231A'
I have no idea how you are getting the data from your table, or which serial number you want (there are 2 in this file). This is how I would do it.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

태그

Community Treasure Hunt

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

Start Hunting!

Translated by