Hi ,
I have this:
"10:58",297.98,0.25,0.15,66.5,1.194,-0.31,-0.27,71.1,0.966,""
it is of type char.
it contains the following numbers and one time value:
10:58 (time)
297.98
0.25
0.15
66.5
1.194
-0.31
-0.27
71.1
0.966
how can i convert this char type to individual numbers? I could not find it on internet i tried multiple things with char2[anything]
many thanks,
Paul

Antworten (4)

Robbin van Hoek
Robbin van Hoek am 12 Mai 2015

0 Stimmen

try str2num!
>> str2num('6.1')
ans =
6.1000

1 Kommentar

paul kaam
paul kaam am 12 Mai 2015
but it is of type char, if i do str2num i get the result : ans = [ ]

Melden Sie sich an, um zu kommentieren.

Walter Roberson
Walter Roberson am 12 Mai 2015

0 Stimmen

S = '"10:58",297.98,0.25,0.15,66.5,1.194,-0.31,-0.27,71.1,0.966,""';
C = textscan(S, '"%[^"]",%f,%f,%f,%f,%f,%f,%f,%f,%f,%*s', 'CollectOutput', 1);
thetimestr = C{1}{1};
thenums = C{2};

9 Kommentare

paul kaam
paul kaam am 12 Mai 2015
S is of type char, so is get the following error:
Error using textscan
First input must be of type double or string.
if it was of typ str it would work!
Walter Roberson
Walter Roberson am 12 Mai 2015
A string is a char array, not a different datatype.
I tested the code, so if you copy and paste it, it should work
paul kaam
paul kaam am 12 Mai 2015
true it works! Thanks.. but this is what i get:
S = fgetl(board)
S =
"12:11",43.00,-0.05,0.00,64.4,0.918,0.04,-0.22,73.4,0.989,""
>> C = textscan(S, '"%[^"]",%f,%f,%f,%f,%f,%f,%f,%f,%f,%*s', 'CollectOutput', 1); Caught "std::exception" Exception message is: invalid string position
greets
Walter Roberson
Walter Roberson am 12 Mai 2015
When I assign that text into a string it works. I deduce that there must be some extra invisible characters. Please run again and when it errors out, show both S (the visible string) and
S+0
which will show the character codes associated with each string position
paul kaam
paul kaam am 12 Mai 2015
S = fgetl(board)
S =
"12:15",323.98,0.06,0.07,64.5,0.997,0.04,0.03,72.2,1.001,""
>> C = textscan(S, '"%[^"]",%f,%f,%f,%f,%f,%f,%f,%f,%f,%*s', 'CollectOutput', 1); Caught "std::exception" Exception message is: invalid string position
>> S
S =
"12:15",323.98,0.06,0.07,64.5,0.997,0.04,0.03,72.2,1.001,""
>> S+0
ans =
Columns 1 through 13
34 49 50 58 49 53 34 44 51 50 51 46 57
Columns 14 through 26
56 44 48 46 48 54 44 48 46 48 55 44 54
Columns 27 through 39
52 46 53 44 48 46 57 57 55 44 48 46 48
Columns 40 through 52
52 44 48 46 48 51 44 55 50 46 50 44 49
Columns 53 through 60
46 48 48 49 44 34 34 13
Walter Roberson
Walter Roberson am 12 Mai 2015
You did not open the file with 'rt' to read in text mode.
Walter Roberson
Walter Roberson am 12 Mai 2015
That is, the carriage return on the end was telling textscan that there was another line to process with the format, but then there was no text to process so the match against '"' could not take place.
paul kaam
paul kaam am 12 Mai 2015
i do not understand what u mean with that? rt?
paul kaam
paul kaam am 12 Mai 2015
Now it is between '' with cellstr:
>> S
S =
"12:15",333.98,0.06,0.06,65.3,0.975,0.02,0.10,72.2,1.002,""
>> cellstr(S)
ans =
'"12:15",333.98,0.06,0.06,65.3,0.975,0.02,0.10,72.2,1.002,""'
>> C = textscan(ans, '"%[^"]",%f,%f,%f,%f,%f,%f,%f,%f,%f,%*s', 'CollectOutput', 1); Error using textscan First input must be of type double or string.

Melden Sie sich an, um zu kommentieren.

omax ali
omax ali am 5 Nov. 2020

0 Stimmen

i just write this for new ones who searching:
to change some value like a=["1","2.2","3",......] to number format we can do as:
b=char(a);
c=str2num(b);
c gives you the numbers.

1 Kommentar

Walter Roberson
Walter Roberson am 6 Nov. 2020
Bearbeitet: Walter Roberson am 6 Nov. 2020
a=["1","2.2","3"]
a = 1×3 string array
"1" "2.2" "3"
double(a)
ans = 1×3
1.0000 2.2000 3.0000
This relies upon a being datatype string and not cell array of character vector:
a = {'1', '2.2', '3'}
a = 1x3 cell array
{'1'} {'2.2'} {'3'}
str2double(a)
ans = 1×3
1.0000 2.2000 3.0000

Melden Sie sich an, um zu kommentieren.

Philipp Prestel
Philipp Prestel am 24 Jun. 2023
Bearbeitet: Philipp Prestel am 24 Jun. 2023

0 Stimmen

Again for someone stumbling over this:
if this is your char array: ' "10:58 ", 297.98, 0.25, 0.15, 66.5, 1.194, -0.31, -0.27, 71.1, 0.966,"" '
and these are your values:
val1 (1,1) datetime = 10:58
val2 (1,1) double = 297.98
...
val10 (1,1) double = 0.966
First of all, i want to say something about the array itself; you don't need any qotation marks around the time, it's arbitrary since you already have a char array. Next is the "" at the end, if it is supposed to be a empty string it's fine, otherwise remove it.
This applies if you're indexing stays the same for other arrays you may want to decode, if the indexing varries for some reasons there are better ways of doing dynamic identification.
Generally, I would advise against using char arrays to pass around data. If you have to do it or read from a text file make sure what you save is compatible with sscanf, fscanf, readtable or anything using Matlabs format spec. Json works as well with jsonencode and jsondecode.
if you can't get around it, I found the following to work:
value_char = '"10:58",297.98,0.25,0.15,66.5,1.194,-0.31,-0.27,71.1,0.966,""';
value_char = erase(value_char,'"');
value_cellstr = split(value_char,',');
time_str = string(value_cellstr{1});
value_time = datetime(time_str,InputFormat='HH:mm')
value_time = datetime
24-Jun-2023 10:58:00
num_cellstr = value_cellstr(2:end-1);
nums = cellfun(@str2num,num_cellstr)
nums = 9×1
297.9800 0.2500 0.1500 66.5000 1.1940 -0.3100 -0.2700 71.1000 0.9660
last_str = erase(value_cellstr{end},'"');
last_str = string(last_str)
last_str = ""
This isn't the best implementation but it shows what you can do. Split the array into cell string using ',' as a delimiter then convert the individual cellstirngs to the format you want.
If you want it more adaptive you can also write a small function to use for cellfun, which identifies what type you want a value in and then does the conversion. Which might look something like this:
value_char = '"10:58",297.98,0.25,0.15,66.5,1.194,-0.31,-0.27,71.1,0.966,""';
value_cellstr = split(value_char,',');
nums = cellfun(@convert2type,value_cellstr,UniformOutput=false)
nums = 11×1 cell array
{["10:58" ]} {[297.9800]} {[ 0.2500]} {[ 0.1500]} {[ 66.5000]} {[ 1.1940]} {[ -0.3100]} {[ -0.2700]} {[ 71.1000]} {[ 0.9660]} {["" ]}
function A_convert = convert2type(A)
arguments
A char {mustBeTextScalar}
end
if contains(A,'"')
A_convert = erase(A,'"');
A_convert = string(A_convert);
else
A_convert = str2num(A);
end
end
If you want the time as a datetime just add a condition with ':' to the function in the function.
Bit of along answer but I hope it helps someone having to work with these things.
Edit: By default datetime automatically determines the display format if you only want daytime use Format='preserveinput'

Kategorien

Tags

Gefragt:

am 12 Mai 2015

Bearbeitet:

am 24 Jun. 2023

Community Treasure Hunt

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

Start Hunting!

Translated by