How do I get the last element of a cell array from a function call?

29 ビュー (過去 30 日間)
Elizabeth Cisneros
Elizabeth Cisneros 2018 年 7 月 15 日
コメント済み: Image Analyst 2018 年 7 月 16 日
Say I am making a function call to a function that needs an index as one of its arguments, however, in this case, I only want the last element of the cell array. So far I have this:
twin_start = boundary_times(df,steplists,stepIdx,full_step_str,end);
using end as an argument, but I know you can't use end like that. Is there something I can do to indicate that I want the last element of the cell array when making a function call? Thank you :)
  2 件のコメント
Rik
Rik 2018 年 7 月 15 日
You mean you want only the last element of your output array? I have no idea what kind of input boundary_times is expecting, so I can't tell if you should use some call to numel or something totally different.
Elizabeth Cisneros
Elizabeth Cisneros 2018 年 7 月 15 日
For the input I am talking about in boundary_times (location) it is just expecting an index
function twin_time = boundary_times(df,steplists,stepIdx,step_str,location)
and this is the array from boundary_times where that index will go in
%split time window string to determine end time
twinSplit = strsplit(step_twin,{' ','->'});
step_BoundaryT = twinSplit{location};
However, there are sometimes where the size of this cell array may vary (this is in a loop by the way) so I am trying to figure out a way to call it where the location input will always be the last element. I've tried length(twinSplit) too but that doesn't work.

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

採用された回答

Stephen23
Stephen23 2018 年 7 月 15 日
編集済み: Stephen23 2018 年 7 月 15 日
Write the function to accept a special case, e.g. an empty array indicates end:
if isempty(location)
step_BoundaryT = twinSplit{end};
else
step_BoundaryT = twinSplit{location};
end
and then call it like this:
boundary_times(...,[]);
  2 件のコメント
Elizabeth Cisneros
Elizabeth Cisneros 2018 年 7 月 15 日
Thank you, Stephen! I never thought about actually changing the function.
Image Analyst
Image Analyst 2018 年 7 月 16 日
Well changing the function was what I suggested to you in my earlier answer, but you said "I wouldn't want to hardcode it though because I use this function for other purposes". Glad to see you changed your mind. Obviously if nothing changes in your code (or input data), then nothing will change in the results.

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

その他の回答 (1 件)

Image Analyst
Image Analyst 2018 年 7 月 15 日
Try this:
% Put contents of last cell of the twinSplit cell array into step_BoundaryT
step_BoundaryT = twinSplit{end};
  1 件のコメント
Elizabeth Cisneros
Elizabeth Cisneros 2018 年 7 月 15 日
I wouldn't want to hardcode it though because I use this function for other purposes

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

カテゴリ

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