Hello to all!
I want to extract the numeric values of a mixed data that I have in a cell of an array. This is what I have done:
T = readtable('file.dat','Format','%s'); %Which creates a table with 1 column and 525606 rows.
latitude = T{1,1} %which creates an array with the next output:
latitude =
'12.967 [deg N]'
only one char value.
What I want to do now is to get only the numerical value from that array, I've tried char2string and sscanf but it only prompts errors and no conversion. Please help :) I know that it seems to be a simple procedure but I'm stuck here.
Thanks in advance!

댓글 수: 1

Hello. Thanks for the reply. Apparently that code prompts this error:
>> latitude = T{1,1}
latitude =
'12.967 [deg N]'
>> C = strsplit(T{1,1}) Error using strsplit (line 76) First input must be a string.
>>

댓글을 달려면 로그인하십시오.

 채택된 답변

Jorge Alberto Fuentes Casillas
Jorge Alberto Fuentes Casillas 2016년 12월 16일

0 개 추천

Thanks for your support. This is another way to solve it and that I used in the version 2014b:
coordinates = readtable('Bangalore-20161215_min.dat','Format','%s');
%% We split in this part the tables' content that we need (latitude, longitude, time, etc).
expression = ' '; %Helps to divide the cell everytime we find a space. splitStr_lat = regexp(coordinates{1,1},expression,'split'); %we split the string in one row with 3 columns or cells. Every time that the code finds a ' ' (space) character, it will split the character and put it in one cell.
num_lat = splitStr_lat{1,1};%variable that has the numerical value of latitude. latitude = cell2mat(num_lat(1,1)); %Convert from cell to numerical value.
%% We do the same for longitude. splitStr_long = regexp(coordinates{2,1},expression,'split'); %now we get the longitude in the row 2 column 1. num_long = splitStr_long{1,1}; longitude = cell2mat(num_long(1,1));
I will try with your code, Star Strider.
Thanks.

추가 답변 (1개)

José-Luis
José-Luis 2016년 12월 16일

0 개 추천

C = strsplit(T{1,1})
your_val = str2num(C{1});

댓글 수: 7

José-Luis
José-Luis 2016년 12월 16일
편집: José-Luis 2016년 12월 16일
Try replacing
T{1,1}
by
[T{:}]
No. It says this (I added another option that I tried)
>> C = strsplit(T{:}) You can not subscript a table using only one subscript. Table subscripting requires both row and variable subscripts.
>> C = strsplit(T{1:1,1:1}) Error using strsplit (line 76) First input must be a string.
José-Luis
José-Luis 2016년 12월 16일
You'll have to show what you actually have in T
This is what I have in T:
And what I want to do, is to get only the numerical values of the first 4 rows (12.967, 77.583, etc) individually.
José-Luis
José-Luis 2016년 12월 16일
And what does T{1} return?
The same error:
C = strsplit(T{1}) You can not subscript a table using only one subscript. Table subscripting requires both row and variable subscripts.
The following is missing something T{1,1}{1} should work, for future reference

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Cell Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by