How to assign a column of text string to another variable?

For example,
A = repmat('test', 5,1);
If I want to assign A to B, why the below doesn't work? What is the right way to do it?
B([20+1:20+5,1) = A
Many thanks,

6 件のコメント

Adam Danz
Adam Danz 2020 年 3 月 9 日
What is B? A char array? A string array? A cell array?
Leon
Leon 2020 年 3 月 9 日
編集済み: Leon 2020 年 3 月 9 日
It should be a N by 1 column of all the accumulated strings.
I would have A1, A2, A3, ..., etc. I want to combine them into one string B. So B should be identical to A in terms of what kind of array it is, e.g., string or char, etc. Sorry I'm not familiar with the difference between string vs char.
I defined B as
B = NaN (100000,1);
Is that problematic?
Adam Danz
Adam Danz 2020 年 3 月 9 日
You can use class(var) to determine the class of a variable var.
Adam Danz
Adam Danz 2020 年 3 月 9 日
編集済み: Adam Danz 2020 年 3 月 9 日
Yes, it's problematic. Your definition of B creates a double precision numeric vector. But A is a char array.
Perhaps you're looking for
% Cell array of chars
A = repmat({'test'}, 5,1);
B = cell(100000,1);
B([20+1:20+5]) = A;
or
% String array
A = repmat("test", 5,1);
B = repmat("", 100000,1)
B([20+1:20+5]) = A;
Leon
Leon 2020 年 3 月 9 日
Thank you! How do I define a N by 1 column of strings or chars?
Leon
Leon 2020 年 3 月 9 日
Thank you so much. It works
The string array is what I need!

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

 採用された回答

Guillaume
Guillaume 2020 年 3 月 9 日

0 投票

A = repmat('test', 5,1);
replicates the 1x4 char array 'test' 5 times along the rows, resulting in a 5x4 matrix of 20 characters
B([20+1:20+5,1) = A
attemps to store these 20 characters at indices 21 to 25 of row 1 of B. That's room for just 5 characters, so yes it's not going to work.
Note that you're working with char vectors, not string arrays. Perhaps you intended to work with string arrays. char vectors and string arrays work differently. Your code would work with string arrays. string arrays use ", not ' as the enclosing symbol:
%this would work. No idea if that's what you want
A = repmat("test", 5, 1);
B(20+(1:5), 1) = A;
However, note that you don't even need the repmat in the above. Assigning a scalar to a vector automatically replicates the scalar:
A = "test";
B(20+(1:5), 1) = A;

2 件のコメント

Leon
Leon 2020 年 3 月 9 日
Thanks!
Unfortunately, when I test it, my 21 to 25 rows are NaNs.
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
NaN
NaN
NaN
NaN
NaN
Guillaume
Guillaume 2020 年 3 月 9 日
If you're using strings, then B needs to be a string array to starts with. If it's starts as a numerical array then yes the strings will be converted to numbers and "test" not being a valid representation of a number gets converted to NaN (Not a Number).
B = strings(0);
%or
B = string.empty
would create an empty string array. Alternatively,
clear B
A = "test";
B(20+(1:5), 1) = A;
also works.

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File Exchange で Characters and Strings についてさらに検索

製品

リリース

R2019b

タグ

質問済み:

2020 年 3 月 9 日

コメント済み:

2020 年 3 月 9 日

Community Treasure Hunt

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

Start Hunting!

Translated by