calling unique function on a string column of a table failed

I have a simple table (say, T) one column (say C) of which is a string column. [v, u] = unique({T.C}) fails with the exception:
Cell array input must be a cell array of character vectors.

7 Commenti

[v, u] = unique({T.C})
% ^ ^ get rid of these
while removing {} works, but I am running an inherited script which used to work with earlier version of MATLAB. I'm using 2022Ra. Is 2022Ra different from earlier version of MATLAB in this respect? BTW, the existing script has T(u), but now it doesn't work anymore and I need to change it to T(u, :). Probably related, the existing script also has code like T(i).C. Now such code causes the error "Dataset array subscripts must be two-dimensional."
Matt J
Matt J il 2 Mag 2023
Modificato: Matt J il 2 Mag 2023
Probably related, the existing script also has code like T(i).C. Now such code causes the error "Dataset array subscripts must be two-dimensional."
You have the ability to demonstrate that for us here directly by attaching T in a .mat file to your post and running the line of code that produces the error. That will allow us all to examine exactly what is happening. It doesn't really make sense that you are getting error messages referring to T as a "dataset" variable when in your OP, you said T was a table.
The dataset class is a table-like class, the precursor for table arrays, introduced in Statistics and Machine Learning Toolbox in release R2007a. [We recommend using table arrays instead of dataset arrays and have for several releases.] It behaves much like a table array and so colloquially calling it a "table" seems reasonable (even though technically it's not a table.)
The equivalent error for a table array would be (using try / catch and warning so I can run further code in the comment):
T = array2table(magic(4));
try
y = T(1)
catch ME
warning(ME.message)
end
Warning: Subscripting into a table using one subscript (as in t(i)) is not supported. Specify a row subscript and a variable subscript, as in t(rows,vars). To select variables, use t(:,i) or for one variable t.(i). To select rows, use t(i,:).
I don't think this type of indexing operation ever worked for either table or dataset.
In general, if you're working with string data you should operate on string arrays rather than cell arrays containing string arrays.
nephews = ["Huey"; "Dewey"; "Louie"] % Preferred
nephews = 3×1 string array
"Huey" "Dewey" "Louie"
nephewsC = {"Huey"; "Dewey"; "Louie"}
nephewsC = 3×1 cell array
{["Huey" ]} {["Dewey"]} {["Louie"]}
The dataset is created from data returned from JDBC SQL query using
struct_obj = table2struct(fetch_results.Data, 'ToScalar', true)
and
dataset_obj = struct2dataset(struct_obj, 'AsScalar', true)
calls. It used to work with version R2018b.I need to correct myself, the version I am using now is R2023a (not 2022Ra, sorry for the confusion).
The existing code that worked with R2018b accesses a data element of dataset as the following:
dataset_obj(i).coupon
for the ith row's in the column named as 'coupon', e.g., Now R2023a complains the indexing (i).
Matt J
Matt J il 2 Mag 2023
Modificato: Matt J il 2 Mag 2023
@Enping this is now a very different question, having to do with dataset objects and nothing to do with table objects and unique() as you originally posted. Therefore, you should ask about dataset object indexing in a different thread, hopefully after accept-clicking the answer I have put below, which does address your original question.
@Matt Thanks for elaborating on the issue. I'll seek help in another thread.

Accedi per commentare.

 Risposta accettata

Why not as in the example below?
T=table(["a";"b"; "a"],'Var',"C")
T = 3×1 table
C ___ "a" "b" "a"
[v,u]=unique(T.C)
v = 2×1 string array
"a" "b"
u = 2×1
1 2

2 Commenti

Enping
Enping il 1 Mag 2023
Spostato: Matt J il 1 Mag 2023
After I removed {} and used u to get unique rows as T(u), I got this error: Dataset array subscripts must be two-dimensional
You need to have,
T.C(u)
Or if you want to extract all corresponding table rows, you must do,
T(u,:)

Accedi per commentare.

Più risposte (0)

Categorie

Richiesto:

il 1 Mag 2023

Commentato:

il 2 Mag 2023

Community Treasure Hunt

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

Start Hunting!

Translated by