Distribute colormap matrice for n values where colormap name comes from GUI

조회 수: 7 (최근 30일)
Hi,
I have a GUI where I'd like to define different colormaps:
GUI 1.JPG
I create a variable name in the GUI:
% --- Executes on selection change in OutputColormap.
function OutputColormap_Callback(hObject, eventdata, handles)
% some code
contents = cellstr(get(handles.OutputColormap,'String'));
OutputColormap = contents{get(handles.OutputColormap,'Value')};
Then I read it into my script:
cmap1 = eval(OutputColormap);
I know eval seems not to be a good way to do it, but like this it has been the only way so far I've got the colormap matrice the way you would get if you would type this:
cmap1 = hsv;
Then I want to distribute it like this:
% Working code if I do not get the colormap matrice from GUI
n = 1000;
cmap = hsv (n);
% Code which does not work so far
cmap = cmap1 (n);
Error:
index exceeds array bounds
I don't know why it does not work as I intend, is it possible that this way (e.g. cmap = hsv (n); ) does only work for the predefined colormap names?

채택된 답변

Stephen23
Stephen23 2019년 11월 22일
편집: Stephen23 2019년 11월 22일
"I don't know why it does not work as I intend, is it possible that this way (e.g. cmap = hsv (n); ) does only work for the predefined colormap names?"
Actually it does not work because colormap functions return an Nx3 matrix of data, and when called without an input argument the MATLAB colormap functions (and many available on FEX too) use the size of the default colormap (some third-party functions use other default sizes, so read their documentation carefully). In any case, your code
cmap1 = eval(OutputColormap); % this already calls the functions and returns a matrix
cmap = cmap1(n); % you are trying to index into a matrix.
does not make much sense because you are trying to index into that Nx3 matrix of RGB values (possibly you are thinking that cmap1 is a function that you can call, but this is not the case).
The best solution would be to use function handles, which you can also generate quite simply from that string/char vector:
fun = str2func(OutputColormap);
then you can call that function handle exactly like you would call any colormap function (e.g. hsv, etc.), with zero or one input argument:
fun()
fun(23)
Or you could easily generate all of the function handles and names at once, and then all you need is indexing to select the appropriate function handle:
>> F = {@hsv,@cool,@parula};
>> C = cellfun(@func2str,F,'uni',0); % use this for the drop-down menu
>> F{2}(5) % 2 == cool
ans =
0 1 1
0.25 0.75 1
0.5 0.5 1
0.75 0.25 1
1 0 1
"I know eval seems not to be a good way to do it..."
Using eval is the wrong way to do this.
  댓글 수: 1
Guillaume
Guillaume 2019년 11월 22일
Another option would be to use
cmap = feval(OutputColormap, n);
Despite the name similarity feval is considerably safer than eval and works completely differently.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Daniel Rohrer
Daniel Rohrer 2019년 11월 24일
Hi,
This works exactly as I wanted:
cmap = feval(OutputColormap, n);
Thank you!

카테고리

Help CenterFile Exchange에서 Colormaps에 대해 자세히 알아보기

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by