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

21 Ansichten (letzte 30 Tage)
tiwwexx
tiwwexx am 23 Jul. 2019
Bearbeitet: Adam Danz am 19 Mär. 2025
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 Kommentare
tiwwexx
tiwwexx am 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 am 19 Mär. 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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Adam Danz
Adam Danz am 23 Jul. 2019
Bearbeitet: Adam Danz am 19 Mär. 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 Kommentare
tiwwexx
tiwwexx am 24 Jul. 2019
Bearbeitet: tiwwexx am 24 Jul. 2019
Perfect! Just needed the convertStringsToChars / convertCharstoStrings. Thanks for the help.
Adam Danz
Adam Danz am 24 Jul. 2019
Nice! Thanks for the feedback!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Data Type Conversion finden Sie in Help Center und File Exchange

Produkte


Version

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by