필터 지우기
필터 지우기

Separate numerical and txt string into two different strings

조회 수: 1 (최근 30일)
Alexandros
Alexandros 2011년 11월 16일
Hello people
I have a string M_b = '1000 km' and I want to separated to M_b1 = 1000 (a numerical string) and to M_b2 = km (a text string).
To tell the truth only M_b1 is of importance for me. But the data comes from an excel and by using xlsread I can only transfer the data to matlab as txt.
I tried multiple things without any success like: M_b1 = str2num('M_b') [M_1b,M_2b] = strread('M_b', %s) [M_1b,M_2b] = strread (sprintf('M_b'), %s) M_b1 = textscan(M_b,'%s')
I am new to matlab but I can learn fast Thank you for your time

채택된 답변

Alexandros
Alexandros 2011년 11월 16일
It looks better but it stills give me
??? Error using ==> textscan First input must be of type double or string.
  댓글 수: 4
Fangjun Jiang
Fangjun Jiang 2011년 11월 16일
I would not use the same variable 'M_b' for the two return arguments. It can be done but it's confusing. Maybe you just have a typo. [M_b, M_b] = xlsread ('1.xls', 2, D14).
Anyway, M_b most likely is a cell array, so you need to do M_b1 = textscan(M_b{1},'%f')
Alexandros
Alexandros 2011년 11월 17일
This is it man!!!!
Thank you very much

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

추가 답변 (2개)

Fangjun Jiang
Fangjun Jiang 2011년 11월 16일
You are close.
M_b = '1000 km';
M_b1 = textscan(M_b,'%f')

Alexandros
Alexandros 2011년 11월 17일
But now it gets more complecated
I have a string
A = xlsread('1.xls', 1, 'D12') A = 'Audi A4 Avant FTSI Flex Man 1984cc 132kw Euro5A [AI002]'
Now i want
A1 = 'Audi'
A2 = ' A4 Avant FTSI Flex Man'
A3 = '1984' without the cc
A4 = '132' without the kw
A5 = 'Euro5A'
Is this separation doable? and if it is how it can be done? I think is too complex
  댓글 수: 1
Alexandros
Alexandros 2011년 11월 17일
I did
B = strread (A{1}, '%s')
But it gives me
??? Error using ==> dataread
Badly formed format string.
Error in ==> strread at 51
[varargout{1:nlhs}]=dataread('string',varargin{:});
Normaly it should work. Any ideas?

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

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by