필터 지우기
필터 지우기

How can I replace [#] entries in a .Csv file so that Matlab can read the first row as variable names?

조회 수: 9 (최근 30일)
Hi,
I have seven very large .Csv data files (about 3000 columns). Whoever made the file named each variable with "[0] Label1", "[1] Label2", etc, with square brackets and numbers preceding the variable names. Of course, this means that Matlab's data importer can't use the given variable names since they don't start with a letter.
To avoid having to manually type in the variable names, is there any way I can edit the .Csv file to find and replace all of the [*] values with nothing? I think I need to use textscan and strmatch, but I am not sure of the syntax to make this work, or how to just edit the file without erasing or changing the rest of it. Any help is appreciated! I also don't know whether something like csvwrite or dlmwrite is needed here.
.Csv file format is like this:
[0] Label 1,[1] Label 2, [2] Label 3, ...
  댓글 수: 2
K
K 2014년 11월 18일
Hi again,
So I was able to write a new header line using code found on this Answers site, and now I have a header line that has variables names like "D0Label1" which should be read in just fine. But when I try to use the importer tool, it still leaves that variable names as "VarName1" etc. Any ideas?
Image Analyst
Image Analyst 2014년 11월 18일
If you can't use readtable() like I suggested, then attach your original csvfile with the paper clip icon.

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

답변 (2개)

Star Strider
Star Strider 2014년 11월 18일
There are a number of options. If it’s an Excel file, use xlsread. Otherwise, if all the labels are on one row, use textscan with 'HeaderLines',1 to skip them.
  댓글 수: 1
K
K 2014년 11월 18일
No, I don't want to skip them, I want to edit them. It's not an Excel file, it's .csv, and if I try to go through Excel, it is too large to easily get back.

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


Image Analyst
Image Analyst 2014년 11월 18일
Try
t = readtable('yourfile.csv')
It tries to "fix up" the column headers into valid variable names. Tables are only in R2013b and later.
  댓글 수: 1
K
K 2014년 11월 18일
Ok, that kiiind of works, now I have all the data in a ginormous table. I'm not very familiar with tables, so I'll see if I can work with that. Thanks, K

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

카테고리

Help CenterFile Exchange에서 Text Files에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by