フィルターのクリア

"str2mat is not recommended", but what is recommended?

5 ビュー (過去 30 日間)
Thomas Ligon
Thomas Ligon 2015 年 12 月 8 日
コメント済み: Guillaume 2015 年 12 月 10 日
I am looking for the recommended solution for this piece of code. The Matlab documentation says to use char instead of str2mat, but there is no example. Here, paramDisplay is an array of parameter names of varying length, are used as tick marks in a figure.
ticklabels=char(paramDisplay(1));
for paramNum=2:size(paramDisplay,2)
ticklabels= str2mat(ticklabels, char(paramDisplay(paramNum)));
end
set(gca,'XTick',1:1:size(paramDisplay,2),'XTickLabel',ticklabels);

採用された回答

Guillaume
Guillaume 2015 年 12 月 8 日
You can just replace str2mat by char and it should work:
for ...
ticklabels = char(ticklabels, char(paramDisplay(paramNum)));
end
If paramDisplay is already a string, you don't need to wrap it into char. You probably don't need the loop either, however I'm puzzled by your loop. The for line implies that that paramDisplay is a 2D array (since you're using size to get the number of columns), but in the body of the loop you're using linear indexing. With linear indexing, you're iterating over the rows first. So it doesn't make sense.
I'm also confused about what paramDisplay is. You use matrix indexing, yet it can be a matrix as that can't be an "array of parameter names". Is it a class?
If paramDisplay were a 1D cell array of strings of varying lengths:
ticklabels = char(paramDisplay{:}) %no need for loop
  3 件のコメント
Thomas Ligon
Thomas Ligon 2015 年 12 月 10 日
Thanks again for the help. Now I have a solution that avoids the loop.
ticklabels=arrayfun(@char,model.Par,'UniformOutput',false);
Guillaume
Guillaume 2015 年 12 月 10 日
Sorry, I didn't have access to a computer yesterday.
The arrayfun solution is exactly what I was going to suggest.

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

その他の回答 (0 件)

カテゴリ

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

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by