matlab 'colorbar' error
Ältere Kommentare anzeigen
I am getting this error message thread when I try to add a colorbar to a figure, thanks for any help!:
>> colorbar
Check for incorrect argument data type or missing argument in call to function
'imag'.
Error in flipud (line 14)
x = flip(x,1);
Error in matlab.graphics.shape.internal.AxesLayoutManager/insertAboveAxes
Error in matlab.graphics.shape.internal.AxesLayoutManager.getManager
Error in legendcolorbarlayout (line 34)
hManager = matlab.graphics.shape.internal.AxesLayoutManager.getManager(hAx);
Error in matlab.graphics.illustration.ColorBar/setAxesImpl
Error in matlab.graphics.illustration.ColorBar/set.Axes_I
Error in matlab.graphics.illustration.ColorBar/set.Axes
Error in colorbar (line 238)
cbar.Axes = peeraxes;
12 Kommentare
Adam Danz
am 17 Jan. 2022
What does this return?
which colorbar -all
Walter Roberson
am 17 Jan. 2022
Better yet, what does this return:
which flip -all
Adam Danz
am 18 Jan. 2022
Walter Roberson's right - you're likely shadowing the flip function. I just created a flip.m and ran colorbar and it produced the same error.
Bill Johns
am 18 Jan. 2022
Bearbeitet: Adam Danz
am 18 Jan. 2022
Steven Lord
am 18 Jan. 2022
Since the error message calls out the imag function let's check that it is not shadowed either.
which -all imag
Bill Johns
am 18 Jan. 2022
Bearbeitet: Adam Danz
am 18 Jan. 2022
Bill Johns
am 18 Jan. 2022
I wonder why your paths do not indicate "Shadowed" as shown below.
which flip -all
Brita Irving
am 7 Mai 2025
Verschoben: Walter Roberson
am 7 Mai 2025
Was this ever resolved? I am having the same error, on windows and mac platforms. I just updated to R2024b from R2022b to resolve this but the error is still happening!
Walter Roberson
am 7 Mai 2025
Generally speaking, these kinds of problems are most often due to having a third-party function with the same name as a Mathworks function.
To debug the problem, create a new directory and cd to the new directory. Then inside the new directory
restoredefaultpath; rehash toolboxcache
and try executing colorbar again from inside the new directory. If it works, then the problem was due to a third-party function with the same name as a mathworks function.
Brita Irving
am 7 Mai 2025
Bearbeitet: Walter Roberson
am 8 Mai 2025
Thank you! I did that, and it indeed did work but when I tried which imag -all it still didn't show a third-party function with the same name, and there was no imag variable in my workspace.
In the end, I edited my startup.m, which contained a lot of addpath(genpath(...)) calls to different toolboxes installed over time. I tested which was causing the error, and removed it. Now the error is gone!
% TESTING ERROR
% okay, issue with some folder in matlab-utility because when commented out
% addpath(genpath('D:\MATLAB\matlab-utility')); in startup.m, the colorbar works fine.
% first, identify all folders in matlab-utility
d = dir('D:\MATLAB\matlab-utility');
d = d([d.isdir]);
d = d(4:end); % remove {'.'} {'..'} {'.git'}
% First, check that error does not happen
figure;
imagesc(peaks);
colorbar;
close
% Now loop through and add them one at a time and see if error comes up
for nfolder = 1:numel(d)
addpath(genpath(fullfile(d(nfolder).folder,d(nfolder).name)))
try
figure;
imagesc(peaks);
colorbar;
title(fullfile(d(nfolder).folder,d(nfolder).name))
catch
fprintf('error here: %s\n',fullfile(d(nfolder).folder,d(nfolder).name))
keyboard
end
pause(1)
close
end
% do a final check
addpath(genpath('D:\MATLAB\matlab-utility'));
figure;
imagesc(peaks);
colorbar;
title('D:\MATLAB\matlab-utility')
fprintf('function end\n')
This was the original error I was getting, that is now resolved.
Error in flip (line 33) throw(ME);
^^^^^^^^^^ Error in flipud (line 14) x = flip(x,1);
^^^^^^^^^^^^^^ Error in matlab.graphics.shape.internal.AxesLayoutManager/insertAboveAxes Error in matlab.graphics.shape.internal.AxesLayoutManager.getManager Error in legendcolorbarlayout (line 38) hManager = matlab.graphics.shape.internal.AxesLayoutManager.getManager(hAx);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error in matlab.graphics.illustration.ColorBar/setAxesImpl Error in matlab.graphics.illustration.ColorBar/set.Axes_I Error in matlab.graphics.illustration.ColorBar/set.Axes Error in colorbar (line 241)
cbar.Axes = peeraxes;
^^^^^^^^^^^^^^^^^^^^^
Walter Roberson
am 8 Mai 2025
d = dir('D:\MATLAB\matlab-utility');
d = d([d.isdir]);
d = d(4:end); % remove {'.'} {'..'} {'.git'}
That code assumes that . and .. and .git are the first three directories returned by dir(). That is a very common mistake. dir() returns files in the order reported by the operating system, rather than sorting files itself. The operating system in turn reports files in the order returned by the file system, rather than sorting files itself.
The order of files returned by NTFS is not documented. In practice it appears to be controlled by the NTFS file name encoding parameter. The NTFS file name encoding parameter of any given NTFS file system is in turn controlled by the languages settings of the person who created the NTFS file system (there is not explicit file name encoding command line setting when creating NTFS file systesm; it is whatever the user happened to be using.)
Given the usual file name encoding parameter... there are various file names that sort before '.' sorts.
What you should have instead of d = d(4:end) is a line such as
d(ismember({d.name}, {'.', '..', '.git'})) = [];
Antworten (0)
Kategorien
Mehr zu Images finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!