How to set table VariableNames from a cell?
Afficher commentaires plus anciens
I have a peculiar frustrating situation: am trying to set the headers of a 1x19 table from an excel table to provide as an input to a Machine learning struct. As per the documentation of the struct i need to provide a MATLAB Table with 19 rows of numeric data with the following headers.
gender seniorcitizen partner dependents tenure phoneservice multiplelines internetservice onlinesecurity onlinebackup deviceprotection techsupport streamingtv streamingmovies contract paperlessbilling paymentmethod monthlycharges totalcharges
10 0 200 200 16 100 200 200 300 300 300 300 300 300 502 200 630 18.95 326.8
However, the problem is, for some reason, i get the dreaded, unintelligible error message:
Function 'subsindex' is not defined for values of class 'table'.
1. I tried importing the excel as a variable using the import facility. Does not work. 2. I tried creating the table from a cell array of headers using the cell2table. That gives another of the infamous cryptic errors. (seriously, wonder who writes those error messages in MATLAB? they seem to have a special training for making it as unfriendly as possible)
I found that the Properties of the table in MATLAB workspace look like below:
description: ''
userdata: []
dimensionnames: {'row' 'variables'}
variablenames: {1×19 cell}
variabledescriptions: {}
variableunits: {}
rownames: {}
I need to set the VariableNames directly as values instead of a 1x19 cell. How do i do it? (And pls avoid referring me to MATLAB table documentation. Does not work)
7 commentaires
dpb
le 14 Oct 2018
Show EXACT code IN CONTEXT with error messages; table does "work" so you've got a syntax error somewhere.
Use readtable directly to load the spreadsheet.
It never hurts to attach the spreadsheet.
Images are essentially useless for folks to use for helping; there's nothing we can do with them.
Anandasubramanian Pranatharthy
le 14 Oct 2018
The question is confusing: the title is "How to set table VariableNames from a cell?" and you tagged the question with "table", but then in the main question body you talk about structures... so do you actually have a table or a structure?
Then although you ask about using a cell array later you ask "I need to set the VariableNames directly as values instead of a 1x19 cell. How do i do it?"
So do you want to use a cell array (like your title states) or not (like you write later)?
dpb
le 14 Oct 2018
We can't debug what we can't see...post the code you're trying to execute that has the problem, not vague descriptions of something.
What specific structure and what specific function needs this structure and where's the documentation for that function if it's from somewhere other than Matlab?
As Stephen says, it's very confusing since we have no idea what you're trying to do--we don't have the acquaintanceship with the problem you have so we just only know what you tell us.
If is true that there are some restrictions on storing into a table based on addressing modes when one tries to make conversions to/from one data class to another or if you try to store a given type of data into another object that expects it to be something else...but you don't show us what you're doing that causes the error so we can only guess what the solution might be.
The last error might be resolved if you wrote
categorical(variable)
in place of where you had whatever variable was in the line that caused the error--then again, it might not, depending, or it might "work" but still not be the right answer for the problem.
Image Analyst
le 14 Oct 2018
Not sure what you mean.
filename = 'Telco Customer Churn2.xlsx'
data = xlsread(filename, 'Numeric Data');
works fine for me. Again, do you want/expect a table, a cell array, a double matrix, or a structure?
Anandasubramanian Pranatharthy
le 14 Oct 2018
Modifié(e) : dpb
le 14 Oct 2018
dpb
le 14 Oct 2018
What you DON'T show us is how you created
willCustomerChurn
so we can figure out what it is, specifically.
I'm guessing you use one of the ML prepackaged applications and Exported its results to the workspace?
If so, which app? Then we can look at the doc for it and follow along. Without, we're still guessing...
Réponse acceptée
Plus de réponses (2)
Walter Roberson
le 14 Oct 2018
Function 'subsindex' is not defined for values of class 'table'.
That means you cannot use a table as a subscript.
I need to set the VariableNames directly as values instead of a 1x19 cell. How do i do it?
The VariableNames property of a table may be set to either a string array or a cell array of character vectors; if a string array is used then it will be converted to a cell array of character vectors.
aaa = array2table(randi(10,5,3));
aaa.Properties.VariableNames = {'gender', 'seniorcitizen', 'partner'};
"Attached is the excel itself. The 'Numeric Data' tab is the one am trying to use."
data = readtable('Telco Customer Churn2.xlsx', 'Sheet', 'Numeric Data');
data(1:4, :)
ans =
4×21 table
customerID gender SeniorCitizen Partner Dependents tenure PhoneService MultipleLines InternetService OnlineSecurity OnlineBackup DeviceProtection TechSupport StreamingTV StreamingMovies Contract PaperlessBilling PaymentMethod MonthlyCharges TotalCharges Churn
____________ ______ _____________ _______ __________ ______ ____________ _____________ _______________ ______________ ____________ ________________ ___________ ___________ _______________ ________ ________________ _____________ ______________ ____________ _____
'7590-VHVEG' 20 0 100 200 1 200 700 400 200 100 200 200 200 200 500 100 600 29.85 29.85 200
'5575-GNVDE' 10 0 200 200 34 100 200 400 100 200 100 200 200 200 501 200 610 56.95 1889.5 200
'3668-QPYBK' 10 0 200 200 2 100 200 400 100 100 200 200 200 200 500 100 610 53.85 108.15 100
'7795-CFOCW' 10 0 200 200 45 200 700 400 100 200 100 100 200 200 501 200 620 42.3 1840.75 200
"cannot automatically convert a double variable to categorical values."
Is it possible that you have an existing table object that you are trying to add more data to?
1 commentaire
dpb
le 14 Oct 2018
"Is it possible that you have an existing table object that you are trying to add more data to?"
I think it's clear OP does have some sort of other object is trying to set/update, but just what seems more than reluctant to share...out of not understanding what it takes to ask a question that can be answered and not thinking about what the readers are able to know/infer from posting alone vis a vis his knowledge of everything associated with the problem on his end is the likely cause. Pushing for that so far hasn't yielded what we really need to know...
Guillaume
le 14 Oct 2018
Right, this is one case of a badly explained problem and one case of a user not really understanding the error message he's given or the code is using.
As stated by the by the HowToPredict field of the willCustomerChurn structure, the code requires a table with
- variable names that are identical to those in WillCustomerChurn.RequiredVariables. Now if we look at the variable names of the table, they match. So that's not the problem and asking how to change the variable names (trivial to do b.t.w) is barking up the wrong tree.v
- "variable formats (e.g. matrix/vector, datatype) must match the original training data". Looking at the error, the code expects a variable to be categorical. Instead it is double. That's the problem that needs fixing.
As it is, we don't have enough information to know which variable should be categorical. The best thing to do would be to use the same code that was used to load the training data into a table to load the current file.
Failing that, we can take a guess. Loading the given excel file, I get a table with 21 variables (not 19) with only 4 variables that are of type double. Among these 'SeniorCitizen' is the most likely candidate to be converted to categorical, so
yourtable.SeniorCitizen = categorical(yourtable.SeniorCitizen);
may fix the problem. However, it's possible that some of the text variables may need converting to categorical as well. Looking at the training table and comparing the type of each variable would be the best way to know.
Note: "So i created a categorical of table2 into table3" What does that mean? You cannot convert a table to categorical. You can only convert variables into categorical.
3 commentaires
Anandasubramanian Pranatharthy
le 14 Oct 2018
Modifié(e) : Anandasubramanian Pranatharthy
le 14 Oct 2018
Walter Roberson
le 14 Oct 2018
"Loading the given excel file, I get a table with 21 variables (not 19) with only 4 variables that are of type double."
data = readtable('Telco Customer Churn2.xlsx', 'Sheet', 'Numeric Data');
Everything except the first column comes out as double.
Guillaume
le 14 Oct 2018
Everything except the first column comes out as double
I just used a plain readtable and didn't check the excel file to see if there was more than one sheet. That's probably why it's different.
Catégories
En savoir plus sur Common Operations dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
