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

8 views (last 30 days)
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?

Accepted Answer

Stephen23
Stephen23 on 22 Nov 2019
Edited: Stephen23 on 22 Nov 2019
"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 Comment
Guillaume
Guillaume on 22 Nov 2019
Another option would be to use
cmap = feval(OutputColormap, n);
Despite the name similarity feval is considerably safer than eval and works completely differently.

Sign in to comment.

More Answers (1)

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

Categories

Find more on Colormaps in Help Center and File Exchange

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!