필터 지우기
필터 지우기

All the fieldnames are prefixed with 'x'

조회 수: 5 (최근 30일)
alice
alice 2017년 6월 6일
댓글: Walter Roberson 2017년 6월 7일
I'm new to Matlab and I'm sure there's an obvious reason why this isn't working, but I can't figure it out. I've seen this happen in R and SAS sometimes but Google isn't turning up anything.
M = readtable('RRS_with_Chl.csv')
M =
x400 x401 x402
__________ __________ __________
0.0047333 0.0021455 0.0036116
The csv file that I'm reading from has fieldnames of 400, 401, 402, etc. There are no leading spaces or symbols before them (when I open it in e.g. Notepad++), at least as far as I can tell.
How can i fix this? I would like to extract the fieldnames and use them in a numeric array, but it's hard to do when they have been turned into weird strings.
Edit: I have matlab Version 2015a, so I can't use the strip() function.

답변 (1개)

dpb
dpb 2017년 6월 6일
table names must be valid Matlab variable names and (just like SAS and R and virtually all other programming languages) that means they must start with a nonnumeric character. readtable has prefixed the letter 'x' to meet that requirement. You can't "fix" that, but you can retrieve the names and obtain the numeric values therefrom simply enough--
N=cellfun(@(s) sscanf(s,'x%d'),M.Properties.VariableNames).';
  댓글 수: 3
dpb
dpb 2017년 6월 6일
I'm not sure how readtable does the decision on what the renaming is, if one doesn't have a header row then it uses VarN where N=1,2,...
Whether x is unique for numeric always dunno', a perhaps more robust would be
N=cellfun(@s) sscanf(s(~isletter(s)),'%d',M.Properties.VariableNames).';
although if used an underscore or something would fail on it. Why isn't a builtin in isnumeral dunno', but that'd probably be most robust for OP here.
Walter Roberson
Walter Roberson 2017년 6월 7일
Correction, matlab.lang.makeUniqueStrings is used, not matlab.lang.makevalidname

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

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by