Save char array from for loop into a matrix

36 ビュー (過去 30 日間)
Devarshi Patel
Devarshi Patel 2019 年 6 月 5 日
編集済み: Adam Danz 2019 年 6 月 6 日
names=4;
for i = 1:names
totalnames = filename{indx};
end
where filename is a cell array of a list of 4 file names and indx is the location of the files that the user selected from the list dialog box. I am trying to store the names of the files that the user chose through the for loop into a matrix. Can someone please help me out with this?

採用された回答

TADA
TADA 2019 年 6 月 5 日
編集済み: TADA 2019 年 6 月 5 日
Unless you can guarantee that all file names have the exact same length, you need to use something other than a char matrix. Either a cell array, or a string array:
names=4;
totalnames = cell(1,names);
for i = 1:names
totalnames{i} = filename{indx};
end
  2 件のコメント
Devarshi Patel
Devarshi Patel 2019 年 6 月 5 日
I am getting the same name in all the cells in the totalnames array, would you know any solution to that?
Adam Danz
Adam Danz 2019 年 6 月 5 日
編集済み: Adam Danz 2019 年 6 月 5 日
That's because you're using the same indx on each loop iteration. The indx value doesn't change in this for-loop. If indx is a vector, you don't need a loop (try out my answer). If indx is not a vector, you should tell us more about that variable.

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

その他の回答 (1 件)

Adam Danz
Adam Danz 2019 年 6 月 5 日
編集済み: Adam Danz 2019 年 6 月 6 日
No need to use a loop. If filenames is a cell array of strings and indx is a logical vectory (or linear index vector), you just need to do this:
totalnames = filename(indx);
If you need it to be a char array, run the line above and then this line:
totalnamesArray = strjoin(filenames,'\n');

カテゴリ

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

製品


リリース

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by