I have a column in matlab with data written this way:
19970422
30980524
63098754
etc …
And I only want to have the first 4 numbers (preferably as a new variable), for instance like this:
1997
3098
6309
How can I do it? Thanks a lot for your help.

 채택된 답변

Apdullah YAYIK
Apdullah YAYIK 2014년 4월 27일
편집: Jan 2014년 4월 27일

1 개 추천

Thats easy
X=[19970422 30980524 63098754]
New_X=floor(X/1000)

댓글 수: 2

Azzi Abdelmalek
Azzi Abdelmalek 2014년 4월 27일
This gives the 5 first digits, use : floor(X/10000) . Also this works only for numbers with 8 digits.
Image Analyst
Image Analyst 2014년 4월 27일
And she said she had cells, not regular numerical arrays, though perhaps she didn't really mean cells - who knows.

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

추가 답변 (1개)

Azzi Abdelmalek
Azzi Abdelmalek 2014년 4월 26일
편집: Azzi Abdelmalek 2014년 4월 26일

1 개 추천

a=[19970422;30980524;63098754]
b=arrayfun(@num2str,a,'un',0)
out=cellfun(@(x) str2num(x(1:4)),b)
%or
a=[19970422;3098052422;630987541;0.23]
n=max(0,fix(log10(a)+1)-4)
out=fix(a./10.^n)

댓글 수: 2

Maria
Maria 2014년 6월 23일
@Azzi Abdelmalek by any chance do you know how to do exactly the same thing, but with a cell element? Thanks in advance for your help!
Maria
Maria 2014년 6월 24일
About this question, problem solved!

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

카테고리

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

질문:

2014년 4월 26일

댓글:

2014년 6월 24일

Community Treasure Hunt

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

Start Hunting!

Translated by