Split one array into two while splitting each cell

I have an array which needs to be split into two. It is in the format:
13008KT
08009KT
13009KT
13008KT
These new arrays need to split this into one array that contains the first 3 numbers, and a second array that contains the second two numbers and leaves out the KT.
Help is much appreciated

3 comentarios

José-Luis
José-Luis el 4 de Jun. de 2014
Are those strings?
Sara
Sara el 4 de Jun. de 2014
Yes, they are being read as strings
Please check your Using METAR data in Matlab thread.

Iniciar sesión para comentar.

Respuestas (2)

Cedric
Cedric el 4 de Jun. de 2014
Editada: Cedric el 4 de Jun. de 2014
Assuming that you have
data = {'13008KT', '08009KT', '13009KT', '13008KT'} ;
here one way to go:
>> data = cat( 1, data{:} ) ;
>> array1 = data(:,1:3) - '0'
array1 =
1 3 0
0 8 0
1 3 0
1 3 0
>> array2 = data(:,4:5) - '0'
array2 =
0 8
0 9
0 9
0 8

6 comentarios

Sara
Sara el 4 de Jun. de 2014
In your code, the 1:3 in array1 and the 4:5 in array 2 are trying to use columns 1-5 in the data, where there is actually only one. I need it to find the 1-3 and 4&5 characters in each column. Is there another way to solve for this?
José-Luis
José-Luis el 4 de Jun. de 2014
Well, you need to adapt this to your data. How is it structured? Cedric assumed a cell array of strings. What do you have?
Sara
Sara el 4 de Jun. de 2014
It is being imported from an excel document in one column with 200,000+ rows of data. It is being imported as a text file since there are numbers as well as alphabetic characters
I am sorry but that does not really answer the question. Please try
class(your_data)
size(your_data)
And tell us what it returns.
ans =
cell
ans =
264624 1
Cedric
Cedric el 4 de Jun. de 2014
Editada: Cedric el 4 de Jun. de 2014
Ok, your Excel file must have multiple columns. Assuming that the relevant column is column 5 of a variable named textData
[~,textData] = xlsread( 'myFile.xlsx' ) ;
you can just make the following update
data = cat( 1, textData{:,5} ) ; % Update with the relevant column #.
and then apply what is in my solution above.
array1 = data(:,1:3) - '0' ;
array2 = data(:,4:5) - '0' ;

Iniciar sesión para comentar.

José-Luis
José-Luis el 4 de Jun. de 2014
Building from Cedric's answer
Then your data probably looks like this:
data = [{'13008KT}'}, {'08009KT'}, {'13009KT'}, {'13008KT'}] ;
first_three_as_string = cellfun(@(x) x(1:3),data,'uniformoutput',false);
last_two_as_string = cellfun(@(x) x(4:5),data,'uniformoutput',false);
first_three_as_number = cellfun(@(x) str2double(x),first_three_as_string);
last_two_as_number = cellfun(@(x) str2double(x),last_two_as_string);
Please accept an answer if it helped you.

Preguntada:

el 4 de Jun. de 2014

Editada:

el 4 de Jun. de 2014

Community Treasure Hunt

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

Start Hunting!

Translated by