# Convert String Array to Numeric matrix and string array

조회 수: 132(최근 30일)
Jochen Petersen 30 Jun 2019
Commented: dpb 30 Jun 2019
Hi! I have a 3 by 15 string matrix. I wish to extract the first and third row as numbers to be assigned to a numerical matrix, while extracting the second row as a row of strings, which are to be assigned to another matrix.
Here is the info:
data_events=
"1" "60" "" "" "" "" "" "" "" "" "" ""
"-" "" "" "" "" "" "" "" "" "" "" ""
"60" "50" "2" "25" "0" "0.2" "0.2" "10" "1000000000" "100000" "100000" "1000000000"
Columns 13 through 15
"" "" ""
"" "" ""
"100000" "100000" "1080"
The idea is to create a matrix Event_Vals, which consists of the stuff in the first and third rows, while another matrix, Event_Comments is to contain the contents of the second row.

로그인 to comment.

### 답변 수 (2)

per isakson 30 Jun 2019
Try this
%%
data_events = [
"1" "60" "" "" "" "" "" "" "" "" "" ""
"-" "" "" "" "" "" "" "" "" "" "" ""
"60" "50" "2" "25" "0" "0.2" "0.2" "10" "1000000000" "100000" "100000" "1000000000"
];
%%
Event_Vals = str2double( data_events([1,3],:) );

#### 댓글 수: 3

Guillaume 30 Jun 2019
Note that you with string arrays (not char arrays), you can simply use double instead of str2double
Event_Vals = double(data_events([1, 3], :))
The same way that to convert numbers to string, you can simply use string:
>> string([1, 2, 3, 4])
ans =
1×4 string array
"1" "2" "3" "4"
per isakson 30 Jun 2019
+1
I had missed "Unlike the char function, string does not treat numbers as ASCII or Unicode® code points."
dpb 30 Jun 2019
Me neither. Having MATLAB act similar to BASIC is not something we're used to...I've yet to play that much with the string class so still discovering such things, too.

로그인 to comment.

dpb 30 Jun 2019
EventVals=str2double(data_events(1:2:end,:));
EventCmnt=data_events(2,:);

#### 댓글 수: 0

로그인 to comment.

이 질문에 답변하려면 로그인을(를) 수행하십시오.

Translated by