Is there the more elegant way to do this?

1 回表示 (過去 30 日間)
G A
G A 2019 年 6 月 14 日
編集済み: G A 2019 年 6 月 14 日
By trial and error I came to the following solution for choosing in my GUI a colormap and the number of colors to be used for my 3D surf plot:
%CM_3D is the value from uicontrol popupmenu: 'String',{ 'parula'; 'jet'; 'hsv'; 'hot'}
%NumC=5 is the str2double input from uicontrol edit: 'String','5'
%NumC can be empty matrix [], then the number of colors expected to be the default number
%colormap parula or colormap parula(5) or colormap parula() works; colormap parula(NumC) and colormap parula([]) does nor work
%colormap(parula) or colormap(parula(NumC)) or colormap(parula()) works; colormap(parula([])) does nor work
switch CM_3D
case 1
cm = @parula;
case 2
cm = @jet;
case 3
cm = @hsv;
case 4
cm = @hot;
end
if isempty(NumC)||isnan(NumC)||NumC==0
colormap(cm()), %colormap(cm) does not work
else
colormap(cm(NumC)),
end
Is there more elegant (or 'professional') solution?
  1 件のコメント
darova
darova 2019 年 6 月 14 日
works fine for me

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

採用された回答

Matt J
Matt J 2019 年 6 月 14 日
編集済み: Matt J 2019 年 6 月 14 日
You don't need the switch block. You can just do,
items = get(hObject,'String');
index_selected = get(hObject,'Value');
cm = str2func( items{index_selected} );
  1 件のコメント
G A
G A 2019 年 6 月 14 日
編集済み: G A 2019 年 6 月 14 日
Thanks!

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

その他の回答 (0 件)

カテゴリ

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

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by