Pranav Totala
Pranav Totala

Reputation: 158

imwrite giving inconsistent error in Matlab

I wrote a code to clean and print multiple images,

data_1=csvread(data)
for h=1:30
    im_old=imread(strcat('catches\image_generator (',int2str(h),').png'));
    im_bw=func_bw(im_old);
    im_2=func_clean_tr(im_bw);
    [im_3a,im_3b]=edge_trial(im_2);
    da=data_1{h,2};
    name=strcat('trrr\',da,'trial.png');
    imwrite(im_3b,strcat('trrr\',int2str(h),'trial.png'));
end

There is a particular problem. The imwrite works when the parameters are:

imwrite(im_3b,strcat('trrr\',int2str(h),'trial.png'));

But it wont work when I give the parameters as:

imwrite(im_3b,strcat('trrr\',da,'trial.png'));

I cross checked that da is a 1x1 string and strcat('trrr\',da,'trial.png') is also a 1x1 string. The error shown is:

Error using imwrite>parse_inputs (line 510)

A filename must be supplied.

No idea why imwrite is treating two strings differently...

Edit1: my data_1 reads like: 1,X55N3 2,PQZXS 3,HDDS3... Also, value of da=data_1{h,2}; is "X55N3"

Upvotes: 3

Views: 463

Answers (1)

Cris Luengo
Cris Luengo

Reputation: 60494

MATLAB is still sort of transitioning to the new string class. Traditionally, MATLAB has always used char arrays where you need a string. They have introduced the string class in R2016b, and haven't updated all functions in all toolboxes yet to also take a string where they used to take a char array.

I'm using R2017a, and see this when using imread with a string:

>> imread("cameraman.tif");
Error using imread>parse_inputs (line 450)
The file name or URL argument must be a character vector.

Error in imread (line 322)
[filename, fmt_s, extraArgs, was_cached_fmt_used] = parse_inputs(cached_fmt, varargin{:});

However, this works:

>> imread(char("cameraman.tif"));

So your solution is to convert the string into a char array:

imwrite(im_3b,char(strcat('trrr\',da,'trial.png')));

or:

imwrite(im_3b,strcat('trrr\',char(da),'trial.png'));

Upvotes: 3

Related Questions