I'm trying to compare a cell array to a single vector.

I'm trying to compare each cell in the first column of an array and then I'm trying to slice the rest of the row, but it says I can't use '==' with cells, I already tried strcmpi(A,B) and that didn't work either. Finally I'm trying to put it in a new matrix as you can see.
DestinationData = {'ATL','DAL','FFT','JBU','SWA';...
'EWR','UAL','FFT','JBU','NKS';...
'PHL','AAL','FFT','JBU','SWA';...
'ORD','AAL','FFT','SWA','NKS';...
'CLT','AAL','FFT','NKS','III'}
Destionation = 'PHL'
Airlines = []
for col= 1:1:size(DestinationData,1)
if DestinationData(col,1) == Destination
Airlines = DestinationData(col, 2:end)
end
end

 採用された回答

Walter Roberson
Walter Roberson 2023 年 4 月 10 日

0 投票

DestinationData = {'ATL','DAL','FFT','JBU','SWA';...
'EWR','UAL','FFT','JBU','NKS';...
'PHL','AAL','FFT','JBU','SWA';...
'ORD','AAL','FFT','SWA','NKS';...
'CLT','AAL','FFT','NKS','III'}
DestinationData = 5×5 cell array
{'ATL'} {'DAL'} {'FFT'} {'JBU'} {'SWA'} {'EWR'} {'UAL'} {'FFT'} {'JBU'} {'NKS'} {'PHL'} {'AAL'} {'FFT'} {'JBU'} {'SWA'} {'ORD'} {'AAL'} {'FFT'} {'SWA'} {'NKS'} {'CLT'} {'AAL'} {'FFT'} {'NKS'} {'III'}
Destination = 'PHL'
Destination = 'PHL'
strcmpi(Destination, DestinationData(:,1))
ans = 5×1 logical array
0 0 1 0 0

その他の回答 (1 件)

Torsten
Torsten 2023 年 4 月 10 日

0 投票

DestinationData = {'ATL','DAL','FFT','JBU','SWA';...
'EWR','UAL','FFT','JBU','NKS';...
'PHL','AAL','FFT','JBU','SWA';...
'ORD','AAL','FFT','SWA','NKS';...
'CLT','AAL','FFT','NKS','III'};
Destination = 'PHL';
Airlines = [];
for col= 1:1:size(DestinationData,1)
if DestinationData{col,1} == Destination
Airlines = DestinationData(col, 2:end);
end
end
Airlines
Airlines = 1×4 cell array
{'AAL'} {'FFT'} {'JBU'} {'SWA'}

カテゴリ

ヘルプ センター および File ExchangeResizing and Reshaping Matrices についてさらに検索

製品

リリース

R2022b

質問済み:

2023 年 4 月 10 日

回答済み:

2023 年 4 月 10 日

Community Treasure Hunt

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

Start Hunting!

Translated by