How to read the indivual elements of a column data?

1 回表示 (過去 30 日間)
Hamza
Hamza 2012 年 9 月 24 日
Dear All
I have a data say,
x= '96318.74847837'
'96319.62211352'
'96319.62351606'
'96319.62356237'
'96320.05952563'
'96320.49676119'
I want to read all the elements individually. For e.g. '96318.74847837'. In this '96' is the year and '318.74847837' is day of year. I want to display the years and day of years separately and then plot them against another set of data say 'longitude' L= 19.1, 20,19.5,20.1,20.0,20.1
Please help. Thanks

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2012 年 9 月 24 日
編集済み: Azzi Abdelmalek 2012 年 9 月 24 日
year=cellfun(@(y) y(1:2),x,'uni',false)
day=cellfun(@(y) y(3:end),x,'uni',false)
  1 件のコメント
Hamza
Hamza 2012 年 9 月 24 日
This works great.Great use of cellfun in fact.

サインインしてコメントする。

その他の回答 (3 件)

Jan
Jan 2012 年 9 月 24 日
You can do this numerically or as string operation:
x = {'96318.74847837', ...
'96319.62211352', ...
'96319.62351606', ...
'96319.62356237', ...
'96320.05952563', ...
'96320.49676119'};
c = char(x);
year = cellstr(c(:, 1:2));
day = cellstr(c(:, 3:end));
% Or:
num = sscanf(sprintf('%s*', x{:}), '%g*');
day = rem(num, 1000);
year = round(num - day);
  1 件のコメント
Hamza
Hamza 2012 年 9 月 24 日
Thanks Jan. I like your solution. Working good.

サインインしてコメントする。


Rodrigo
Rodrigo 2012 年 9 月 24 日
It's not clear, but it seems like x is a cell array containing strings. If you want to extract the year and the day then you might do something like:
year=zeros([1,numel(x)]); day=year; for p=1:numel(x) year(p)=str2double(x{p}(1:2)); day(p)=str2double(x{p}(3:end)); end
if you have multiple years, it may make sense to do:
day=day+365.25*(year-min(year));
so that you don't get wrapping of the dates
  1 件のコメント
Hamza
Hamza 2012 年 9 月 24 日
Thanks Rodrigo. It is a bit too complex for a simple problem. Below two solutions worked great and did the job.

サインインしてコメントする。


Andrei Bobrov
Andrei Bobrov 2012 年 9 月 24 日
編集済み: Andrei Bobrov 2012 年 9 月 24 日
t = str2double(x)';
year1 = fix(t/1000);
day1 = t - year1*1000;
  3 件のコメント
Andrei Bobrov
Andrei Bobrov 2012 年 9 月 24 日
Thank you, Azzi! Corrected.
Hamza
Hamza 2012 年 9 月 24 日
Very simple and elegant. Great man! Thanks

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by