Azzera filtri
Azzera filtri

All the fieldnames are prefixed with 'x'

5 visualizzazioni (ultimi 30 giorni)
alice
alice il 6 Giu 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.

Risposte (1)

dpb
dpb il 6 Giu 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 Commenti
dpb
dpb il 6 Giu 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 il 7 Giu 2017
Correction, matlab.lang.makeUniqueStrings is used, not matlab.lang.makevalidname

Accedi per commentare.

Categorie

Scopri di più su Get Started with MATLAB in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by