How to store strings in a matrix or table?

36 Ansichten (letzte 30 Tage)
Tmat
Tmat am 31 Mai 2020
Kommentiert: Tmat am 31 Mai 2020
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!

Akzeptierte Antwort

Ameer Hamza
Ameer Hamza am 31 Mai 2020
Bearbeitet: Ameer Hamza am 31 Mai 2020
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";
..
  1 Kommentar
Tmat
Tmat am 31 Mai 2020
Thank you very much! This solved my problem!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Structures finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by