reading the data from a csv file with headers
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
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
2018 年 6 月 11 日
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
2018 年 6 月 12 日
That works! Thanks a lot
Star Strider
2018 年 6 月 12 日
As always, my pleasure!
Hello, regarding this discussion I encounter a problem. for the parameters of numeric array I use
t=readtable('file.csv')
c=t.serialnum
and some parameters contain characters so I use :
c=char(t.Name)
there is a parameter which can be array of char OR numeric array . what can I use to read from the table (and later I will write in the medadata of wav file) to adopt with both cases?
Star Strider
2018 年 7 月 24 日
I do not understand.
What are you doing, and what is the file you are importing?
I have a csv file and I need to read that file and I use in the title of wav file which I write using audiowrite.
Tb = readtable('configuration.csv');
TypeCapteur=char(Tb.TypeCapteurR);
Nom=char(Tb.NomR);
NumeroSerie=char(Tb.NumeroSerieR);
ConversionFactor=Tb.CorrectionCalibrationR;
filename=[TypeCapteur,'_', Nom,'_', NumeroSerie,'_', num2str(ConversionFactor),'.wav'];
infotitle=filename
audiowrite(filename,X,48000,'BitsPerSample',24,'Title',infotitle);
Some of the parameters are numeric array like Conversionfactor and some others contain numbers and letters that I read using 'char'. For the parameter "NumeroSerie" it can happen both cases (numeric array, string array or characters and numbers). If I have characters and numbers that works but if it was a number and read it using char(...) I got an error and vice versa.
Star Strider
2018 年 7 月 24 日
I do not have your file, and I still do not undersrtand the problem.
I have the parameters in csv file. 'Name' and 'Serialnum' and 'Cfactor are the headers.
'Serialnum' can be number or number/letter. 'Name' contains only letters and 'Cfactor' is a number. I read them and I write in the filename of wav file.
Tb = readtable('configuration.csv');
c=char(Tb.Name)
b=Tb.Cfactor
filename=[Name,'_', serialnum,'_',num2str(Cfactor),'.wav'];
audiowrite(filename,X,48000,'BitsPerSample',24);
my question is that what I have to use for 'serialnum' that can be numeric array or char array to work in both cases.
I hope it was clear.
Try this:
snstr = num2str(serialnumber);
cfstr = num2str(Cfactor);
filename = sprintf('%s_%s_%s.wav', Name, snstr, cfstr)
I have no idea what your data are, so I cannot experiment with this.
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.
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 件)
カテゴリ
ヘルプ センター および File Exchange で Operations on Strings についてさらに検索
タグ
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
