How to store strings in a matrix or table?

19 次查看(过去 30 天)
Hi, I have some strings, say, 'a1',...,'a12', and I want to store them in a 3 by 4 matrix A so that
A(1,1)='a1',..., A(1,4)='a4', A(2,1)='a5',..., A(2,4)='a8',..., A(3,1)='a9',..., A(3,12)='a12'. So that then I have a matrix A of my strings, and I can copy its content into my excel table (this is really what I want to do in the end).
However, as I try to assign string value to matrix, I recieve the following warning:
Unable to perform assignment because the indices on the left side are not compatible with the size of
the right side.
Any suggestions? Thanks in advance!

采纳的回答

Ameer Hamza
Ameer Hamza 2020-5-31
编辑:Ameer Hamza 2020-5-31
If the end goal is to write these values in excel, the following will work (R2019a and later)
A = compose('a%d', reshape(1:12, 4, 3).');
writecell(A, 'file.xlsx')
However, in general, there is a difference in char array (single quotes) and string (double quotes) in MATLAB. So the correct syntax is
A(1,1) = "a1";
A(1,2) = "a2";
..

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Structures 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by