Hey all, I have a 1 x 3 cell, which contains tables. In each table there are a lat and lon columns, I want to extract the first row of these columns from each table and store them below each other in an array. I thought about unique function but since there is just an example and my original data has many points with the same latitude and different longitude and vice versa it's not applicable in this case.
BTW here is my try:
for i = 1:numel(test)
lat(i) = test{i}(1,3);
lon(i) = test{i}(1,4);
LATandLON = (lat(i), lon(i))
end
Again since the original data set is really huge and maybe different column's number, I would like to avoid hard coding but I don't know how to do this in this case.
Thank you

 채택된 답변

the cyclist
the cyclist 2020년 4월 11일
편집: the cyclist 2020년 4월 11일

1 개 추천

The following will give a 3-element cell array, where each cell has a 360x2 numeric array with the latitude and longitude vectors:
latAndLon = cellfun(@(x)[x.lat x.lon],test,'UniformOutput',false);
This code gives the same result as
for i = 1:numel(test)
latAndLon{i} = [test{i}.lat, test{i}.lon]
end
which might be easier to understand.

댓글 수: 6

Really thank you but after that, I have the same issue too, I need an array with 2 columns (lat and lon) and 3 rows that fill like this:
test{1, 1}(1,3:4)
test{1, 2}(1,3:4)
test{1, 3}(1,3:4)
I can do this manually but since data set are more than 100 elements I would like to do this automatically and avoid hard coding.
Thanks again
Ah, I misread some of your question. Is this what you want?
tmp = cellfun(@(x)[x.lat(1) x.lon(1)],test,'UniformOutput',false);
firstLatAndLon = reshape(cell2mat(tmp),3,2)
Note that those could be combined into one statement:
firstLatAndLon = reshape(cell2mat(cellfun(@(x)[x.lat(1) x.lon(1)],test,'UniformOutput',false)),3,2);
oh thanks actually this part is okay:
tmp = cellfun(@(x)[x.lat(1) x.lon(1)],test,'UniformOutput',false);
And give me this, that is correct:
Now I want an array like this:
But after using this:
firstLatAndLon = reshape(cell2mat(tmp),3,2)
the order of tmp has changing and give me some thins like this:
36.7500 45.0000
45.0000 38.7500
37.7500 45.0000
Thank you.
I found that I used this:
tmp = cellfun(@(x)[x.lat(1) x.lon(1)],test,'UniformOutput',false);
then:
cell2mat(tmp')
Am I right?
the cyclist
the cyclist 2020년 4월 11일
If that's the output you want, I guess it's right. :-)
You could test on a different input to be sure.
BN
BN 2020년 4월 11일
Thank you so much

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

추가 답변 (0개)

카테고리

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

제품

릴리스

R2020a

태그

질문:

BN
2020년 4월 11일

댓글:

BN
2020년 4월 11일

Community Treasure Hunt

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

Start Hunting!

Translated by