Convert a numerical variable in a table to string
显示 更早的评论
I have a table T with 5 variables/columns
The 3rd variable (pic), with 75 rows, is basically pic = [70, 40, 60, 70, 50, 65...] How can I convert this one variable in the able to a string? I need to do that so I can carry out strrep later on.
T = string (T(:,3)) is not working. Any ideas where I am going wrong?
Thanks heaps!
3 个评论
Guillaume
2018-5-3
Wrong indexing! {} is used to extract data from a table, not (). Or as per Barak answer, use the .nameofvariable indexing.
However, I need to do that so I can carry out strrep later on. Converting from number to string and back is slow. What is it you want to replace? It can probably be done with going through strings.
Boushra Dalile
2018-5-3
It seems odd to want to replace numerical value with text. Nonetheless, all this replacement can be done a lot more easily and certainly a lot faster without having to convert any of the numbers to text beforehand.
To show you how to do that, you need to be a bit clearer on what is replaced by what. Give an example using valid matlab syntax.
edit: see Walter's answer for what I mean, using a lookup table will be a lot faster than converting numbers to strings
采纳的回答
更多回答(2 个)
As the MATLAB documentation explains, you need to use curly braces to access the contents of a table:
string(T{:,3})
CARLOS RIASCOS
2018-5-3
Starting from the fact that you have the variable 'pic = [70, 40, 60, 70, 50, 65 ...]' defined you could convert that row vector like this:
g = num2str(pic)
The variable g will contain the string produced by pic.
1 个评论
Walter Roberson
2018-5-3
Be sure to pass in a column vector.
The result will be a char array. To convert to string objects, cellstr() and string() the results.
类别
在 帮助中心 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!