フィルターのクリア

Convert a numerical variable in a table to string

166 ビュー (過去 30 日間)
Boushra Dalile
Boushra Dalile 2018 年 5 月 3 日
編集済み: Guillaume 2018 年 5 月 4 日
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 件のコメント
Boushra Dalile
Boushra Dalile 2018 年 5 月 3 日
I am trying to piece together a series of replacements if a separate condition is satisfied, specifically:
If Table x (table with variables, and variable 3 (pic) contains numbers such as 10, 30, 40, 60, 30, 70) contains 35, then - replace all '60' with 'z1' - replace all '40' with 'z2' - replace all '30' with 'z3'
If not, - replace all '60' with 'y1' - replace all '40' with 'y2' and so forth.
Guillaume
Guillaume 2018 年 5 月 3 日
編集済み: Guillaume 2018 年 5 月 4 日
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

サインインしてコメントする。

採用された回答

Walter Roberson
Walter Roberson 2018 年 5 月 3 日
zlookup = {'z1', 'z2', 'z3'};
T3 = T{:,3};
[tf, idx] = ismember(T3, [60, 40, 30]);
out = cell(length(tf),1);
out(tf) = zlookup(idx(tf));
out(~tf) = sprintfc('%g', T3(~tf));
T{:,3} = out;

その他の回答 (2 件)

Stephen23
Stephen23 2018 年 5 月 3 日
編集済み: Stephen23 2018 年 5 月 3 日
As the MATLAB documentation explains, you need to use curly braces to access the contents of a table:
string(T{:,3})

CARLOS RIASCOS
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
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.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by