EventVals=str2double(data_events(1:2:end,:));
EventCmnt=data_events(2,:); Answer from dpb on mathworks.com
MathWorks
mathworks.com › matlab › language fundamentals › data types › data type conversion
str2num - Convert character array or string to numeric array - MATLAB
X = str2num(txt) converts a character array or string scalar to a numeric matrix. The input can include spaces, commas, and semicolons to indicate separate elements.
MathWorks
mathworks.com › matlab › language fundamentals › data types › data type conversion
string, " " - String array - MATLAB
A string array that has only one ... using the + operator. If a string array represents numbers, then you can convert it to a numeric array using the double function....
MathWorks
mathworks.com › matlab › language fundamentals › data types › data type conversion
Convert Text to Numeric Values - MATLAB & Simulink
For example, create a character vector using single quotes and convert it to the number it represents. ... If the input argument is a string array or cell array of character vectors, then str2double converts it to a numeric array having the same size. You can create strings using double quotes.
Top answer 1 of 2
1
EventVals=str2double(data_events(1:2:end,:));
EventCmnt=data_events(2,:);
2 of 2
1
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],:) );
Top answer 1 of 3
25
str = '123';
num = str - '0';
% num = [1 2 3];
2 of 3
0
use the function str2num()
str = '123';
str = str2num(str);
Note: To verify that i'm correct, type in 'whos str' in the command window, and check the class. A string has a class, char, and numeric values have a class, double
MathWorks
mathworks.com › matlabcentral › answers › 744522-converting-a-string-array-into-a-number-array
Converting a string array into a number array - MATLAB Answers - MATLAB Central
February 13, 2021 - Converting a string array into a number array. Learn more about convert string to vector
Top answer 1 of 3
6
Hi Alex,
you cannot mix double values with char values, but you still have to use a cell array (or drop the header 'Names').
% Suppose your input is:
c = {'Name' '2/8' '3/7' '7/8'}
EDIT (from Walter Roberson syntax, didn't know str2num behavior with operators) :
% Then your output would look like this:
[c(1); cellfun(@str2num,c(2:end),'un',0).']
ans =
'Name'
[0.2500]
[0.4286]
[0.8750]
Note that the ' are just there tell you that you're using a cell array of strings, but the actual content of each cell doesn't have them at all.
For more info
Oleg
2 of 3
4
You asked to extract the name and put it in the first column and that the rest of the columns would be actual values. Because of your use of the verb "extract" for the name, it sounds to me as if you might not be aware that it is not possible to have an ordinary array that has a mix of characters and numeric values. In order to mix characters and numeric values, a cell array must be used.
If C is your cell array,
[C(:,1) cellfun(@str2num, C(:,2:end))]
MathWorks
mathworks.com › matlabcentral › answers › 2089381-how-can-i-encode-strings-in-a-array-to-numbers
How can i encode strings in a array to numbers? - MATLAB Answers - MATLAB Central
March 2, 2024 - I have different strings in a matrix and some of the coloums of this matrix is strings. they includes city names etc. so i would like to convert this city names into number i did below code. my mat...
MathWorks
mathworks.com › matlab › language fundamentals › data types
Data Type Conversion - MATLAB & Simulink
You can represent numbers as their hexadecimal or binary values. MATLAB provides functions for conversions between numeric arrays, strings and character arrays, and categorical, datetime, and duration arrays. Also, you can convert between the data types that group data in containers, such as cell arrays, structures, tables, and timetables.
MathWorks
mathworks.com › matlab › language fundamentals › data types › data type conversion
str2double - Convert strings to double precision values - MATLAB
Convert a cell array of character vectors to a numeric array. str = {'2.718','3.1416'; '137','0.015'}; X = str2double(str) ... You can convert text to numbers using the str2double function. Create a string that represents a number.
MathWorks
mathworks.com › matlabcentral › answers › 508324-converting-a-string-into-a-numerical-array
Converting a string into a numerical array - MATLAB Answers - MATLAB Central
March 1, 2020 - How to conver a string in the form "[1,2,3,4,5]" into a vector 1 2 3 4 5 I have used strrep command but here I have to remove each of the "[", "]", ",", sepreately and the convert it into a numa...
Top answer 1 of 2
1
Hi Nicle
you need str2double() (or str2num()), not double().
double() gives you the UTF(?) code corresponding to the input string. Eg:
double('a') % = 97 or
double('1') % = 49
Conversely:
char([97 49]) % = 'a1'
But:
str2double('1') % = 1
Also str2double() works on cellstrings, no need to for cell2mat():
str2double({'1' '2'}) % = 1 2
Best, Konrad
2 of 2
2
You can use "readmatrix" function:
% Generate the data file
a = [290729 123456
13984 654321
13272 111111];
writematrix(a, 'test.txt', 'Delimiter', ' ')
type test.txt
% Read the .txt file
b = readmatrix('test.txt')
MathWorks
mathworks.com › matlabcentral › answers › 761786-convert-string-array-to-numerical-vector
Convert string array to numerical vector - MATLAB Answers - MATLAB Central
March 3, 2021 - I have numbers as string array A that I'd like to convert to numerical vector B: A = ["01";"04";"12"]; B = [1; 4; 12]; I've tried with str2num but it seems to proceed only with string scalars ra...