3

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"

Community
  • 1
  • 1
Pranav Totala
  • 148
  • 2
  • 14
  • 1
    It seems to me that MATLAB made things more complicated by introducing the string object and the double-quote. Note that the single quote and the double quote are different things, and create different type objects. If `da` is a string, you are concatenating it with two char arrays into `name`. I don't know what this will do, but it's probably something unexpected. Did you print `name` to see what it looks like? – Cris Luengo Feb 10 '18 at 16:20
  • 1
    Maybe `name` becomes a string, but `imwrite` expects a char array? – Cris Luengo Feb 10 '18 at 16:21
  • Thanks, i just read input of `imread` is allows only char array and no strings. – Pranav Totala Feb 11 '18 at 14:23

1 Answers1

3

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'));
Cris Luengo
  • 55,762
  • 10
  • 62
  • 120