Convert String Array to Numeric matrix and string array

138 次查看(过去 30 天)
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.

回答(2 个)

per isakson
per isakson 2019-6-30
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 个评论
per isakson
per isakson 2019-6-30
+1
I had missed "Unlike the char function, string does not treat numbers as ASCII or Unicode® code points."
dpb
dpb 2019-6-30
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.

请先登录,再进行评论。


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

类别

Help CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by