Assignment has more non-singleton rhs dimensions than non-singleton subscripts, please help

Asked by jeremy wright on 19 Aug 2012
Latest activity Commented on by jeremy wright on 21 Aug 2012

I am writing a program that finds dark disks in an image then crops that image for a grouping of images.

After everyone's help i got the code to work up to actually saving an image.. heres the code so far

clear all; close all; clc;
%script for running several images through finddisk
path = 'C:\Users\darkloki3\Desktop\Research in Squires Lab\images for multidiskfind\cool frames';
destination= 'C:\Users\darkloki3\Desktop\Research in Squires Lab\images for multidiskfind\cool frames\cropped cool frames';
frames = dir(path);
%creating an array with the names of the image files in the folder
for n= 1:length(frames)
    filenumber = int2str(n);%integer to string
    while length(filenumber)<5
        filenumber=cat(2,'0',filenumber);
    end
    newfilename(n,:)=[path,filesep,filenumber,'.png'];
end   
threshold = 45;
if exist(destination) ~= 7
    mkdir(destination)
end
frames;
nu=1;
for i=1:895
    finddisk(newfilename(i,:),'png',threshold);
%     disk = ans;
%     %crop image around disk and save as a new image
    imwrite(ans,['C:\Users\darkloki3\Desktop\hopefully cropped',filesep,num2str(nu)],'jpeg')
    nu=nu+1;
end
for n= 1:length(frames)
    filenumber2 = int2str(n);%integer to string
    while length(filenumber2)<5
        filenumber2=cat(2,'0',filenumber2);
    end
    newfilename2(n,:)=[path,filesep,filenumber2,'.png'];
end   
for j=1:895
    finddiskcenter(newfilename2(j,:),'png',threshold);
    matrix=imread(newfilename2(j,:),'png');
    x=floor(ans(1,2));
    y=floor(ans(1,1));
    w=1;
    newmatrix=zeros(200);
    for i = (y-200):570
        z=1;
        for j = (x-200):760
            newmatrix(w,z) = matrix(i,j);
            z=z+1;
        end
        w=w+1;
    end
    imwrite(newmatrix,['C:\Users\darkloki3\Desktop\hopefully cropped',filesep,num2str(j)],'png')
end

3 Comments

jeremy wright on 19 Aug 2012

After using the suggestions posted my new code is as follows. The only problem now is that it says ->

    "Can't open file "C:\Users\darkloki3\Desktop\hopefully cropped\" for writing.
    You may not have write permission."
clear all; close all; clc;
%script for running several images through finddisk
path = 'C:\Users\darkloki3\Desktop\Research in Squires Lab\images for multidiskfind\cool frames';
destination= 'C:\Users\darkloki3\Desktop\Research in Squires Lab\images for multidiskfind\cool frames\cropped cool frames';
frames = dir(path);
%creating an array with the names of the image files in the folder
for n= 1:length(frames)
    filenumber = int2str(n);%integer to string
    while length(filenumber)<5
        filenumber=cat(2,'0',filenumber);
    end
    newfilename(n,:)=[path,filesep,filenumber,'.png'];
end   
threshold = 45;
if exist(destination) ~= 7
    mkdir(destination)
end
frames;
nu=1;
for i=1:895
    finddisk(newfilename(i,:),'png',threshold);
%     disk = ans;
%     %crop image around disk and save as a new image
    imwrite(ans,['C:\Users\darkloki3\Desktop\hopefully cropped',filesep,nu],'png')
    nu=nu+1;
end
jeremy wright on 19 Aug 2012

I know everything else pretty much works now because i got no errors when i forgot to put a valid savepath for imwrite

Walter Roberson on 20 Aug 2012

num2str(nu) in the imwrite()

jeremy wright

Tags

Products

3 Answers

Answer by Star Strider on 19 Aug 2012
Edited by Star Strider on 19 Aug 2012
Accepted answer

I suggest changing:

newfilename(n,1)=[path,filesep,filenumber,'.png'];

to:

newfilename(n,:)=[path,filesep,filenumber,'.png'];

( i.e. inserting a colon ‘:’ in place of ‘1’ in the newfilename subscripts ) and see if that works. (It worked for me in a simulation.) If it doesn't, please list the error. This is not an insoluble problem!

23 Comments

jeremy wright on 20 Aug 2012

hmm, i just took out the .png and it just created a "file"....not a png

jeremy wright on 20 Aug 2012

i just got it to work, i commented out the "newfilename=zeros"

now i just have to see if i can get the original program to write

jeremy wright on 21 Aug 2012

I am happy to say my program works!!

Star Strider
Answer by José-Luis on 19 Aug 2012
Edited by José-Luis on 19 Aug 2012

Hello

In your code

newfilename=char(filename);

is an array of characters, that you then try to assign strings to:

newfilename(n,1)=[path,filesep,filenumber,'.png'];

That's a no-no. I figure that is what's producing the error. If you want to preallocate an array of 10 filenames:

nameArray = cell(10,1);

that you can then populate, e.g.:

nameArray{1} = 'yourString';

and access:

iWantMyStringBack = nameArray{1};

Cheers!

4 Comments

José-Luis on 19 Aug 2012

Hi Jeremy.

I modified the code above. I just used repmat to create a cell array. I give another alternative in the modified code. Be careful to store and extract using the curly braces in order to get and store the string.

Cheers!

jeremy wright on 19 Aug 2012

ok i changed it and it still says conversion from cell to char is not possible... hmm i reran and now it says " Attempt to reference field of non-structure array."

jeremy wright on 19 Aug 2012

whoops, nevermind about the non structure thing, i accidentally deleted the apostrophes around .png

the very original error message was "Assignment has more non-singleton rhs dimensions than non-singleton subscripts." somehow i forgot to include that in the original question. hope that helps.

José-Luis
Answer by Jan Simon on 19 Aug 2012
  • Do not shadow the important function path by a local variable.
  • SPRINTF is smarter for adding leading zeros.
  • Use a cell string instead of a CHAR matrix:
folder = 'C:\Users\darkloki3\Desktop\Research in Squires Lab\images for multidiskfind\cool frames';
newfilename = cell(1, length(frames));
for n = 1:length(frames)
  filenumber = sprintf('%05d', n);
  newfilename{n} = [folder, filesep, filenumber, '.png'];
end 

3 Comments

jeremy wright on 20 Aug 2012

Could you please tell me what exactly '%05d' does?

Star Strider on 20 Aug 2012

It's one of a number of format specifiers used in fprintf, sprintf, and other I/O functions. Specifically, '%05d' inserts leading zeros in a 5-digit field to return to filenumber a string to use in newfilename. For instance, if:

n = 1
filenumber = '00001'

or if:

n = 32767
filenumber = '32767'

The reason for zero-padding is to be sure all the filenames have the same length (number of characters). That makes them much easier to sort, find, and read later.

jeremy wright on 20 Aug 2012

Thank you very much.

Jan Simon

Contact us