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

조회 수: 20 (최근 30일)
tiwwexx
tiwwexx 2019년 7월 23일
편집: Adam Danz 2025년 3월 19일
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
tiwwexx
tiwwexx 2019년 7월 23일
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 2025년 3월 19일
% 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.

댓글을 달려면 로그인하십시오.

채택된 답변

Adam Danz
Adam Danz 2019년 7월 23일
편집: Adam Danz 2025년 3월 19일
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
tiwwexx
tiwwexx 2019년 7월 24일
편집: tiwwexx 2019년 7월 24일
Perfect! Just needed the convertStringsToChars / convertCharstoStrings. Thanks for the help.
Adam Danz
Adam Danz 2019년 7월 24일
Nice! Thanks for the feedback!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by