error in table2array - unable toconcatenate the specified table of arrays

4 views (last 30 days)
Hi,
I am trying to develop a small MATLAB program that, in the end, would:
  1. Load a worksheet form a large workook
  2. select 2 columins
  3. compoute and plot a k-means cluster
the kernel of the code is:
vectorESIA = table2array(ESIAS25)
intensity = vectorESIA(:,6)
etnia = vectorESIA(:,7)
intensityCluster = kmeans(vectorESIA,4)
gscatter (intensity,etnia,intensityCluster)
xlabel('economic intensity')
ylabel ('ethnicity')
but it crashes on line 1 in the table2array
the table2array uses the table ESIAS25, which looks Ok, as per the screenshot below, the data is on column 6 & 7:
Can anyone help?

Answers (2)

Abderrahim. B
Abderrahim. B on 27 Jul 2022
Hi!
table2array Converts table to homogeneous array. Your table has mix data (numric, text, ). table2cell is an option that you may consider for what you are trying to do !
HTH

Steven Lord
Steven Lord on 27 Jul 2022
You cannot turn this table into an array with table2array. Luckily, you don't need to turn it into an array.
The reason you can't turn this table into an array is because you have table variables of mixed types. The vilCode and vilName variables contains strings. The latGPS, longGPS, intensidadeE, and codetnia variables are likely double arrays (though I suppose codetnia could be a categorical array.) The etniaPredominante variable looks to be a categorical array. How would MATLAB store all those different types in a numeric array? The answer is it doesn't, it throws an error.
But let's look at the lines of code that actually try to use that array you want to create.
You can extract the intensity variable from the table directly using table indexing, specifically with curly braces or with the name of the variable.
% Replace this
intensity = vectorESIA(:,6)
% with this
intensity = ESIAS25{:, 6} % or
intensity = ESIAS25{:, 'intensidadeE'} % or
intensity = ESIAS25.intensidadeE
You can do the same with etnia:
% Replace this
etnia = vectorESIA(:,7)
% with this
etnia = ESIAS25{:,7} % or
etnia = ESIAS25{:, 'codetnia'} % or
etnia = ESIAS25.codetnia
If you use the variable names in your indexing, this will insulate you against the ordering of the variables in your data changing.
Now for your kmeans call, if you want to only use the numeric data in the clustering process use table indexing. As an easy way to extract just the numeric data from the table, I'm going to use a vartype object.
numericData = ESIAS25{:, vartype('numeric')}
Now you should be able to pass that numeric array into kmeans. I haven't tested this but I expect it to work.
intensityCluster = kmeans(numericData, 4)
  1 Comment
João Serras
João Serras on 27 Jul 2022
dear Steven,
boy, what a great lesson for a rookie sych as myself.... a BIG THANKS!!!!!
Still I am missing part of your reccomendation and this may be because I am "wrongthinking" all.
My value pairs are (ethnicity, intensidadeE) and I have 213 of them,
What I want is to check if there are clusters of "intensity" and if they be clustered around "etnicity" or not. Of course,, "etnicity" are textual classes, which can't be done, so I have changed them to numbers 1..12.
But maybe that's a studip thing to do.
anyway I am modifing the code

Sign in to comment.

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!