How to insert text in a matrix - If function

조회 수: 50 (최근 30일)
Pierre Lonfat
Pierre Lonfat 2017년 4월 19일
댓글: Pierre Lonfat 2017년 4월 20일
Dear All, In the following code I would like to insert 'good news', 'bad news', 'no news' instead of 1, 2, 3 in the "Announcement" matrix.
No idea how to insert text in a matrix (20X28 in this case) !
Many thanks in advance !
Pierre
  댓글 수: 1
Stephen23
Stephen23 2017년 4월 19일
편집: Stephen23 2017년 4월 19일
Numeric matrices contain numeric data only.
You could:
  • store the index in the numeric array, or
  • use another kind of array, e.g. a cell array, table, etc.
But it is not possible to put a string (1xN character vector) into one element of a numeric matrix.

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

채택된 답변

Geoff Hayes
Geoff Hayes 2017년 4월 19일
Pierre - since you are inserting strings of different lengths, your Announcements array will need to be come a cell array
Announcements = cell(N,28);
You will updates it simply as
Announcements{j,i} = 'good news';
etc.
Though you may want to keep your previous implementation and just manage an array of values between 1-3 and then (when you need to display your message) just access a map that converts the number to a string
mapIdToString = {'good news'; 'bad news' ; 'no news'};
The equivalent to the 1 (id) would then be
mapIdToString(1)
  댓글 수: 1
Pierre Lonfat
Pierre Lonfat 2017년 4월 20일
Thank you from the bottom of my heart !

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

추가 답변 (1개)

Andrei Bobrov
Andrei Bobrov 2017년 4월 19일
D is Divident
F is Forecasted_DPS
A = {'good news','bad news','no news'};
ii = (D >= 1.03*F) + (D <= .97*F)*2;
ii(ii == 0) = 3;
Announcements = A(ii);
  댓글 수: 1
Pierre Lonfat
Pierre Lonfat 2017년 4월 20일
This solution works as well ! Thanks !

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

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by