필터 지우기
필터 지우기

How to store strings in a matrix or table?

조회 수: 14 (최근 30일)
Tmat
Tmat 2020년 5월 31일
댓글: Tmat 2020년 5월 31일
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";
..
  댓글 수: 1
Tmat
Tmat 2020년 5월 31일
Thank you very much! This solved my problem!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by