How to solve Error using delete. Argument must contain a string.

function [char_out, letter] = projection_profileBeta()
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
%format compact;
%format loose;
global caption;
img_biner =imread('text_document.jpg');
%find the vertical profile
verticalProfile = sum(binaryImage, 2);
% Find dividing lines between the characters.
props = regionprops(verticalProfile == 0, 'Centroid');
%props = regionprops(horizontalProfile <= 1, 'Centroid'); % new modified
xyCentroids = [props.Centroid];
%dividingLines = xyCentroids(1:2:end)
dividingLines = xyCentroids(2:2:end)
% Extract each line.
global hSubplots;
figure(1);
indexSubplot = (1:10);
% indexSubplot2 = (6:10);
for k = 1 : length(dividingLines)-1
thisX = round(dividingLines(k));
nextX = round(dividingLines(k+1));
subplot(20, 10, indexSubplot );
thisLetter = binaryImage(thisX:nextX, :); % line cropping
imshow(thisLetter);
caption = sprintf('Letter #%d', k);
title(caption, 'FontSize', 8);
indexSubplot = indexSubplot + 10;
%% Cropping Lines into Character
char_out = horizontal_profile(thisLetter);
%% End
pause(0.5) % se we can se every cropping
% indexSubplot = (indexSubplot) + 3;
% indexSubplot2 = (indexSubplot2) + 10;
if k ~= length(dividingLines)-1 % delete hSubplots if k is not euqal to limit loop
delete(hSubplots); % to refresh subplots of characters in the figure 2.
end
end
%SECOND FUNCTION to crop each characters from the each lines
function char_crop = horizontal_profile(img_letters)
global caption;
global hSubplots;
figure(2)
h1 = subplot(4, 12, 1:12);
imshow(img_letters);
title(caption,'FontSize',20);
impixelinfo
% Find horizontal profile
h2 = subplot(4, 12, 13:24);
horizontalProfile = sum(img_letters, 1);
%end
plot(horizontalProfile, 'b-');
title('Horizontal Profile', 'FontSize', 20);
grid on;
% Find dividing lines between the characters.
props2 = regionprops(horizontalProfile == 0, 'Centroid');
%props = regionprops(horizontalProfile <= 1, 'Centroid'); % new modified
xyCentroids2 = [props2.Centroid];
%dividingLines = xyCentroids(1:2:end)
dividingChar = xyCentroids2(1:2:end);
hSubplots = {};
for kk = 1 : length(dividingChar)-1
thisX2 = round(dividingChar(kk));
nextX2 = round(dividingChar(kk+1));
%h3 = subplot(4, 12, indexSub+kk);
h3 = subplot(4, 12, 24+kk);
thisCharacter = img_letters(:, thisX2:nextX2);
%
char_crop = padarray(img_out, [1 1], 'both'); % padding the image by 1 on every sides.
char_crop = imresize(char_crop,[42, 24]); % resizing to be 42m x 24n
char_crop = bwmorph(char_crop, 'thin', Inf);%Image Thinning
%
imshow(char_crop);
caption2 = sprintf('Character #%d', kk);
title(caption2,'FontSize', 12);
%collecting all subplots' positions.
hSubplots{kk} = {hSubplots, h3};
%end
end
% % Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'Outerposition', [0, 0.05, 1, 0.95]);
drawnow;
when i tried this code without separated function , it worked beautifully. but when i separated the code into 2 different function in a m-file, the code gave me error like this:
Error using delete
Argument must contain a string.
Error in projection_profileBETA (line 94)
delete(hSubplots); % to refresh all subplots in the figure 2.
why did it demand a string suddenly on delete parameter ?
thank you

3 Comments

This line on top of a function is completely meaningless:
clear; % Erase all existing variables. Or clearvars if you want.
There are no variables to clear. This is called "cargo cult programming" and a waste of time.
Do not redefine "char" as a variable, because shadowing important built-in functions causes troubles frequently. But the 2 outputs "[char, letter]" are not defined at all anywhere.
@ Jan : i just updated my post code. actually i meant "char_out" instead of "char". so will clear give an error if there are no variable to clear sir ?
First respond to Jan’s Answer below.

Sign in to comment.

 Accepted Answer

I guess this is the old problem of using globals: You cannot see, where such variables are changed. A callback of a timer or a GUI can modify it without any chance to see, who is responsible.
Use at least the debugger to find out, what the contents of hSubplots is. Type this in the command window:
dbstop if error
Run your code again and when it stops type:
class(hSubplots)
size(hSubplots)
hSubplots
What do you see? Does this reveal why the value is not accepted by delete?

13 Comments

i see the class of my variable which is cell.
actually i had known that hSubplots wasn't string. but i had written code roughly like my code above but it ran beautifully. but after i separated the code into 3 different functions in a file it gave me error.
@Jan : does delete function only process string parameter sir ? because on my previous code, hSubplots was axes and it worked sir.
delete() can be applied to a handle graphic handle, or to a character vector, or to a string scalar .
You indicate that before hSubplots was an axes: it is valid to delete() an axes object.
You indicate that now hSubplots is a cell array. It is not valid to delete() a cell array.
@Walter Roberson : I actually indicated hSubplots as array, so that i could store axes object to hSubplots and then delete that array to refresh my subplot. But there was a warning that suggested me to use cell instead to reduce time execute or someting like that. thats why i indicated hSbplots as cell array and stored my axeses into hSubplots.
for K = 1 : numels(hSubplots)
delete(hSubplots{K})
end
I have never seen a warning about putting graphics objects into cell arrays. I just looked over all of the code analyzer messages and nothing like that is present. The only thing I see that might be remotely connected is about performance issues for expanding an array inside a loop.
@Walter Roberson : yes, i meant the warning about performance issues.
@Walter Roberson : i've tried your code
for i = 1 : numel(hSubplots)
delete(hSubplots{i});
end
but still give the same error sir.
I'd tried to use array "[]" first to store all axeses, and then code ran beautifully. But after i use cell the code gave me error. i actually want to use cell to avoid "..Consider perallocating for speed." warning.
@Bachtiar Muhammad Lubis: Please do not write "same error", but post the complete error message. I told you already hiow to check the class of the argument passed to delete by the debugger. Of course this is required now again.
"I'd tried to use array "[]"" - this is not clear enough. Better post the code. When the code ran fine with it, why did you change it to a cell? The "consider pre-allocating" can be avoided by pre-allocating. This is the same problem for cell arrays as for handle graphic errors. To create an array to store n axes objects, use:
HList = gobjects(1, n)
@Jan : this was the error sir:
Error using delete
Argument must contain a string.
Error in projection_profileBETA (line 94)
delete(hSubplots); % to refresh all subplots in the figure 2.
finally i used
hSubplots = [hSubpltos; h3];
and deleted the array to refresh the subplots :
for K = 1 : numels(hSubplots)
delete(hSubplots);
end
the error wasn't pop up anymore.
Thank you for your response by the way.
Do you mean:
delete(hSubPlots(K))
with the index K ?
Argument must contain a string
was showing up earlier because you were trying to apply delete() to a cell array. delete() can be applied to an array of graphics handles, or it can be applied to a character vector or (in recent versions) a scalar string object.
@Jan : I am sorry sir. I meant just
delete(hSubplots);
without using loop to delete hSubplots.1 by 1.
hSubplots variable was created in horizontal_profile() function. and delete it in projection_prrofileBeta() function.
@ Walter Roberson : i think so sir. I just tought that it was same if i store those vector of grafic handles to cell instead of array, even faster.
Thank you for the info sir. you've helped me a lot

Sign in to comment.

More Answers (0)

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Products

Release

R2015b

Community Treasure Hunt

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

Start Hunting!