how to change a cell in a string to a string in a table

21 views (last 30 days)
Hey everyone, I'm trying to use the 'unique' matlab function.
unique(table.row)
When I try this I get the error "Cell array input must be a cell array of character vectors.". I think this is because my table is in the form
table.row(1) = {'string'}
But it needs to be in the form
table.row(1) = 'string'.
I tried to fix this by using the for loop below
for n= 1:numel(table.row)
table.row(n)=table.row{n};
end
but doing this I get the output error "Conversion to cell from char is not possible."
  3 Comments
tiwwexx
tiwwexx on 23 Jul 2019
Jon, I changed up the wording of the question so it has a more concrete example of the problem. Does this help clairify?
Adam Danz
Adam Danz on 19 Mar 2025
% This reproduces your error
T = table(num2cell(string(['a':'z']')),'VariableNames',{'row'})
T = 26x1 table
row _______ {["a"]} {["b"]} {["c"]} {["d"]} {["e"]} {["f"]} {["g"]} {["h"]} {["i"]} {["j"]} {["k"]} {["l"]} {["m"]} {["n"]} {["o"]} {["p"]}
unique(T.row) % ERROR!
Error using matlab.internal.math.uniqueCellstrHelper
Cell array input must be a cell array of character vectors.

Error in cell/unique (line 86)
[varargout{1:nlhs}] = matlab.internal.math.uniqueCellstrHelper(A,[false true false true false]);
I've updated my answer to include 3 ways to convert the data type of table variables.

Sign in to comment.

Accepted Answer

Adam Danz
Adam Danz on 23 Jul 2019
Edited: Adam Danz on 19 Mar 2025
Three ways to convert data types within a table or timetable
1. Use convertvars (R2018b) to convert table or timetable variables to another datatype.
Example: Convert the first and last columns from a cellstring to string array.
T1 = readtable('outages.csv');
head(T1,3)
Region OutageTime Loss Customers RestorationTime Cause _____________ ________________ ______ __________ ________________ ________________ {'SouthWest'} 2002-02-01 12:18 458.98 1.8202e+06 2002-02-07 16:50 {'winter storm'} {'SouthEast'} 2003-01-23 00:49 530.14 2.1204e+05 NaT {'winter storm'} {'SouthEast'} 2003-02-07 21:15 289.4 1.4294e+05 2003-02-17 08:14 {'winter storm'}
T2 = convertvars(T1,["Region","Cause"],"string");
head(T2,3)
Region OutageTime Loss Customers RestorationTime Cause ___________ ________________ ______ __________ ________________ ______________ "SouthWest" 2002-02-01 12:18 458.98 1.8202e+06 2002-02-07 16:50 "winter storm" "SouthEast" 2003-01-23 00:49 530.14 2.1204e+05 NaT "winter storm" "SouthEast" 2003-02-07 21:15 289.4 1.4294e+05 2003-02-17 08:14 "winter storm"
2. Set the VariableTypes property of the table or timetable (R2024b)
Example: Convert the last names to strings and the Gender to categorical.
T1 = readtable("patients.xls");
head(T1,3)
LastName Gender Age Location Height Weight Smoker Systolic Diastolic SelfAssessedHealthStatus ____________ __________ ___ _____________________________ ______ ______ ______ ________ _________ ________________________ {'Smith' } {'Male' } 38 {'County General Hospital' } 71 176 true 124 93 {'Excellent'} {'Johnson' } {'Male' } 43 {'VA Hospital' } 69 163 false 109 77 {'Fair' } {'Williams'} {'Female'} 38 {'St. Mary's Medical Center'} 64 131 false 125 83 {'Good' }
T1.Properties.VariableTypes([1,2]) = ["string","categorical"];
head(T1,3)
LastName Gender Age Location Height Weight Smoker Systolic Diastolic SelfAssessedHealthStatus __________ ______ ___ _____________________________ ______ ______ ______ ________ _________ ________________________ "Smith" Male 38 {'County General Hospital' } 71 176 true 124 93 {'Excellent'} "Johnson" Male 43 {'VA Hospital' } 69 163 false 109 77 {'Fair' } "Williams" Female 38 {'St. Mary's Medical Center'} 64 131 false 125 83 {'Good' }
Tip: To replace all "cell" types to "string", use:
T1.Properties.VariableTypes = strrep(T1.Properties.VariableTypes,'cell','string');
3. Reassign the table variable
Example: Convert the "Cause" variable from cellstring to categorical.
T1 = readtable('outages.csv');
head(T1,3)
Region OutageTime Loss Customers RestorationTime Cause _____________ ________________ ______ __________ ________________ ________________ {'SouthWest'} 2002-02-01 12:18 458.98 1.8202e+06 2002-02-07 16:50 {'winter storm'} {'SouthEast'} 2003-01-23 00:49 530.14 2.1204e+05 NaT {'winter storm'} {'SouthEast'} 2003-02-07 21:15 289.4 1.4294e+05 2003-02-17 08:14 {'winter storm'}
T1.Cause = categorical(T1.Cause);
head(T1,3)
Region OutageTime Loss Customers RestorationTime Cause _____________ ________________ ______ __________ ________________ ____________ {'SouthWest'} 2002-02-01 12:18 458.98 1.8202e+06 2002-02-07 16:50 winter storm {'SouthEast'} 2003-01-23 00:49 530.14 2.1204e+05 NaT winter storm {'SouthEast'} 2003-02-07 21:15 289.4 1.4294e+05 2003-02-17 08:14 winter storm
Example, convert between character vectors or cellstrings and string arrays using convertStringsToChars or convertCharsToStrings (R2017b)
T1.Region = convertCharsToStrings(T1.Region);
head(T1,3)
Region OutageTime Loss Customers RestorationTime Cause ___________ ________________ ______ __________ ________________ ____________ "SouthWest" 2002-02-01 12:18 458.98 1.8202e+06 2002-02-07 16:50 winter storm "SouthEast" 2003-01-23 00:49 530.14 2.1204e+05 NaT winter storm "SouthEast" 2003-02-07 21:15 289.4 1.4294e+05 2003-02-17 08:14 winter storm
  2 Comments
tiwwexx
tiwwexx on 24 Jul 2019
Edited: tiwwexx on 24 Jul 2019
Perfect! Just needed the convertStringsToChars / convertCharstoStrings. Thanks for the help.

Sign in to comment.

More Answers (0)

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!