MATLAB Answers

0

First colum of CSV-imported table has "x___" added to its name

z8080 님이 질문을 제출함. 21 Oct 2019
최근 활동 z8080 님이 댓글을 추가함. 22 Oct 2019
I have a bunch of automatically-generated CSV files with headers, which I'd like to import into Matlab as a table. I used code such as
T = readtable('d:\test.csv', 'readvariablenames', true);
However, even though the name of the CSV's first column is runNr, the first column in the Matlab table gets named "x___runNr"
This clearly has something to do with the CSV files being in a slightly format different from that expected by Matlab. For instance, if I manually change the name of that first cell in Excel to something else, then reopen the CSV, the cell contents are all merged into a cell, on every row.
Still, I am not sure what to do to fix this, since I cannot change the format of the CSVs.
Any help?

  댓글 수: 1

It imports correctly for me in R2019b (Update 1):
T =
runNr repNr trialNr file
_____ _____ _______ ____________________
NaN NaN NaN {0×0 char }
NaN NaN NaN {0×0 char }
NaN NaN NaN {0×0 char }
1 1 1 {'stimuli/12C1.wav'}
(Posting only a relevant subset of the columns.)

로그인 to comment.

제품


릴리스

R2016b

답변 수: 1

Answer by Jeremy Hughes on 21 Oct 2019

The file probably has a Byte Order Mark in the beginning.
If you read the raw bytes using fopen/fread you can see the mark at the beginning.

  댓글 수: 8

표시 이전 댓글 수: 5
Makes sense Steven, thanks. I now used the most recent version of that software (psychoPy), and the same problem occurs, thus it is not a question of the version of it. Beyond that, I'm not sure how much I can find about the structure of these CSVs, and have to just import them as they are. For now I will just hard-code "x___VariableName" as an acceptable variable name...
You could post-process the imported table to change the variable name. Something like this should work, though I haven't tried it:
mytable.Properties.VariableNames{1} = ...
strrep(mytable.Properties.VariableNames{1}, 'x___', '');
Perfect, thank you!

로그인 to comment.



Translated by