Add a row above row nb 'X' in a cell array

32 ビュー (過去 30 日間)
Alizee
Alizee 2013 年 4 月 22 日
回答済み: Vecta Matlab 2019 年 9 月 18 日
Hello,
I would like to know if there is an easy way in Matlab to add a row above another in a cell array? This would corresponds to the function 'insert' in Excel which shifts all rows below down.
Example:
-Initial cell array= {'a','1';'b','2';'c','3'};
-Add row above row nb 2...
-Final cell array= {'a','1';'','';'b','2';'c','3'};
I have tried to to do it by concatenating a new empty one row cell array but of course this raises exceptions if I want to add a row above the first one or above the last one.
Thank you in advance for your help.

採用された回答

Walter Roberson
Walter Roberson 2013 年 4 月 22 日
C(n+1:end+1,:) = C(n:end,:);
C(n,:) = {};
  2 件のコメント
Alizee
Alizee 2013 年 4 月 22 日
Thank you very much Walter, simple and elegant, couldn't find better. Have a good day.
Ariel
Ariel 2014 年 7 月 28 日
What is n in this case?

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

その他の回答 (1 件)

Vecta Matlab
Vecta Matlab 2019 年 9 月 18 日
In 2018a, this needs a bit change.
C(n+1:end+1,:) = C(n:end,:);
C(n,:) = {[]};
Or it throws an error.
Unable to perform assignment because the size of the left side
is 1-by-3 and the size of the right side is 0-by-0.

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by