str2num: Converting string to number (Matlab style)

View source: R/str2num.R

str2numR Documentation

Converting string to number (Matlab style)

Description

Functions for converting strings to numbers and numbers to strings.

Usage

str2num(S)
num2str(A, fmt = 3)

Arguments

S

string containing numbers (in Matlab format).

A

numerical vector or matrix.

fmt

format string, or integer indicating number of decimals.

Details

str2num converts a string containing numbers into a numerical object. The string can begin and end with '[' and ']', numbers can be separated with blanks or commas; a semicolon within the brackets indicates a new row for matrix input. When a semicolon appears behind the braces, no output is shown on the command line.

num2str converts a numerical object, vector or matrix, into a character object of the same size. fmt will be a format string for use in sprintf, or an integer n being used in '%.nf'.

Value

Returns a vector or matrix of the same size, converted to strings, respectively numbers.

See Also

sprintf

Examples

str1 <- " [1 2 3; 4, 5, 6; 7,8,9]  "
str2num(str1)
# matrix(1:9, nrow = 3, ncol = 3, byrow = TRUE)

# str2 <- " [1 2 3; 45, 6; 7,8,9]  "
# str2num(str2)
# Error in str2num(str2) : 
#   All rows in Argument 's' must have the same length.

A <- matrix(c(pi, 0, exp(1), 1), 2, 2)
B <- num2str(A, 2); b <- dim(B)
B <- as.numeric(B); dim(B) <- b
B
#      [,1] [,2]
# [1,] 3.14 2.72
# [2,] 0.00 1.00

pracma documentation built on Nov. 10, 2023, 1:14 a.m.