Filter löschen
Filter löschen

All the fieldnames are prefixed with 'x'

5 Ansichten (letzte 30 Tage)
alice
alice am 6 Jun. 2017
Kommentiert: Walter Roberson am 7 Jun. 2017
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.

Antworten (1)

dpb
dpb am 6 Jun. 2017
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 Kommentare
dpb
dpb am 6 Jun. 2017
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 am 7 Jun. 2017
Correction, matlab.lang.makeUniqueStrings is used, not matlab.lang.makevalidname

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by