How to store strings in a matrix or table?

39 vues (au cours des 30 derniers jours)
Tmat
Tmat le 31 Mai 2020
Commenté : Tmat le 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!

Réponse acceptée

Ameer Hamza
Ameer Hamza le 31 Mai 2020
Modifié(e) : Ameer Hamza le 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 commentaire
Tmat
Tmat le 31 Mai 2020
Thank you very much! This solved my problem!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Structures dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by