Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"

like image 436
Pranav Totala Avatar asked Feb 23 '26 19:02

Pranav Totala


1 Answers

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'));
like image 161
Cris Luengo Avatar answered Feb 26 '26 10:02

Cris Luengo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!