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

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

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
I know everything else pretty much works now because i got no errors when i forgot to put a valid savepath for imwrite

Sign in to comment.

 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

just tried this and still get the error of "Assignment has more non-singleton rhs dimensions than non-singleton subscripts."
I may have found the problem. Comment out newfilename here, i.e.:
length(frames);
% newfilename=char(filename); % <= COMMENT OUT
%creating an array with the names of the image files in the folder
and see if that fixes your code. I used one of my own directories and found that the first declaration of newfilename created a [N x 1] vector. In your loop, you're defining it as a [1 x M] vector at each call, likely the source of your error message. I got a slightly modified version of your code (modified to avoid crashing MATLAB as it did the first time I ran it because I forgot to comment-out your path directory, and running it inadvertently reset mine) to run fine after I commented-out the first decaration of newfilename.
Check the dimensions of newfilename in both places first to be sure. I believe you'll discover the same essential discrepancy that I did.
One other thing — be sure to use:
newfilename(n,:)=[path,filesep,filenumber,'.png'];
rather than your original line or you'll get a ‘Subscripted assignment dimension mismatch.’ error.
ok, thanks for the help, i got past that error....any chance you could advise me on how to make a function return a value because in my function "finddisk" i have C defined and i want to use that C each loop but i am not quite sure how to make that work
Please post your code for your finddisk function (I can't find any MATLAB documentation for it so it it's not in any MATLAB toolboxes I have) and I'll do my best to help.
What value do you want it to return?
oh, i figured out how to return what i needed, now i am just stuck because i can't write to the file i chose.
What do you want to do and how are you trying to do it?
the code i am using now is posted under the original code. I am trying to save new image files to a folder.
it says
"Can't open file "C:\Users\darkloki3\Desktop\hopefully cropped\" for writing.You may not have write permission."
Error in ==> multidiskfind at 29 imwrite(ans,['C:\Users\darkloki3\Desktop\hopefully cropped',filesep,nu],'png')
Please see if Walter Roberson's comment just now to your original post:
num2str(nu) in the imwrite()
will solve the problem.
I admit I didn't catch that when I read your code.
hmm ok it worked...kind of. It seems that it did not create png images....under file type all it says is file....now im totally confused as to where the problem lies.
Silly question mayhap, but did you ever define the variable ans anywhere? (For what it's worth, I do not suggest using ans as a variable name. I know ans is supposed to be a sort of ‘catch-all’ assignment MATLAB uses, but I do not advise you to depend on it, especially when those of us who are following this discussion have absolutely no idea what finddisk does or what it calculates or is supposed to return.) Anyway, it seems ans is the image you want to write to the file.
Come to think of it, when I read your code, you've done absolutely everything except actually create an image!
I don't even see imread anywhere, unless it magically appears in your finddisk function and something finddisk does is returned somehow to your calling program, however your finddisk call doesn't seem to return anything. I can't even find a global statement (as much as I and others discourage global statements, they are at least a round-about way of passing values to and from functions).
Unless I missed something, creating an image to save — or cajoling finddisk to do so and return it to your main routine — seems to be your next task.
i renamed finddisk as finddiskcenter but it has the same basic code. what it does is find the disk, make a matrix containing only the disk and tells me the location of the center.
I changed the original code to reflect all the changes i have made so that you can better understand my dilemma.
heres the code for find the disk center:
function [center]=finddiskcenter (filename,format,threshold)
% this script is so i can try and see if i can get the computer to find, and
% crop around, the disk
A = imread(filename,format);
dimensions = size(A);
for i=1:dimensions(1)
for j=1:dimensions(2)
if A(i,j) > threshold
A(i,j)= 0;
end
end
end
x=1;
w=1;
% w is the top of the disk
while max(A(w,:))==0
w=w+1;
end
E=transpose(A);
z=1;
%z is the left side of the disk
while max(E(z,:))==0
z=z+1;
end
% disp(w+1)
% disp(z+1)
%C=zeros((dimensions(1)-w-1),(dimensions(2)-z-1));
for m =w+1:dimensions(1)
y=1;
if max(A(m,:))~=0%stops at right side of disk
for n =z+1:dimensions(2)
if A(m,n)~=0
C(x,y) = A(m,n);
end
y=y+1;
end
end
x = x+1;
end
subplot(1,2,1),image(A)
subplot(1,2,2),image(C)
findcent=size(C);
aleph = z+1+.5*findcent(1);
beth = w+1+.5*findcent(2);
% disp('center is at');
% disp([aleph beth]);
center = [aleph beth];
%center located at coordinate point (z+1),?-(w+1))
I wanted to thank you for helping me. sorry for such an extensive amount of trouble.
My pleasure to help!
You're still not getting anything back from finddiskcenter because you're not asking it to return anything. I suggest you change your code here:
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
to something like:
diskcntr = finddiskcenter(newfilename(i,:),'png',threshold);
However, it seems to me that you also need to have finddiskcenter return images A and C if those are the images you want to save to disk in your imwrite statement. If that is what you want to do, that solution is simply to add one or both of them (depending on what you want to do) to your initial function statement:
function [center, A, C]=finddiskcenter (filename,format,threshold)
and then the call to it becomes something similar to:
[diskcntr, Aimg, Cimg] = finddiskcenter(newfilename(i,:),'png',threshold);
and changing the first argument to your imwrite statement to whatever image you want to save (in place of ans). I also suggest commenting-out the subplot statements in finddiskcenter. Plot them in your calling program instead, especially if you put them there originally to troubleshoot the finddiskcenter code.
Also, Jan Simon made some excellent suggestions. Please be sure to read them.
Here is the program i was using to deal with single images, I think it might be easier to solve the problem of why it is not actually saving an image here than the larger program. Then i can make a function out of this program and have the multiple image one call it. Its doing everything i want except saving the image. I also changed the original code using Jan Simon's suggestions.
clear all; close all; clc;
cntr=finddiskcenter('C:\Users\darkloki3\Desktop\Research in Squires Lab\images for multidiskfind\cool frames\00100.png','png',40);
matrix = imread('C:\Users\darkloki3\Desktop\Research in Squires Lab\images for multidiskfind\cool frames\00100.png','png');
%define center
cntr
x=floor(cntr(1,1));
y=floor(cntr(1,2));
w=1;
newmatrix=zeros(200);
for i = (y-200):(y+200)
z=1;
for j = (x-200):(x+200)
newmatrix(w,z) = matrix(i,j);
z=z+1;
end
w=w+1;
end
imwrite(newmatrix,'C:\Users\darkloki3\Desktop\Research in Squires Lab\images for multidiskfind\cool frames\new00100.png','png')
% display(newmatrix)
%size(newmatrix)
% matrix(295,117)
subplot(1,2,1),imshow(matrix)
subplot(1,2,2),image(newmatrix)
% imshow(newmatrix)
I'm not certain what you want to do. I admit to being a bit lost.
What is ans, where does it come from, and what are x and y returning?
Other than that, and except for the fact that finddiskcenter is not returning any values to your calling program (the one you posted here), I don't see anything wrong. I assume the subplots display the correct images for matrix and newmatrix.
Are you getting any errors?
i am not getting any arrors and the subplot shows the correct original and cropped images. ans is what was returned by finddiskcenter....need to change that so it makes more sense... x and y are the location of the center of the disk.
**update: changed ans to cntr in both code i am using and the code i posted
oh and somehow it started actually saving .png files...just nothing is in the file
I'm close to being out of ideas. It may not be necessary to specify both the (.png) file suffix and the 'png' format specifier. Keep the filename suffix, drop the specifier and see it that works.
hmm, i just took out the .png and it just created a "file"....not a png
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

Sign in to comment.

More Answers (2)

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

I just tried using the repmat function and now it gives the error at the same place the last was at that "Conversion to cell from char is not possible. " any advice to solve this new problem?
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!
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."
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.

Sign in to comment.

  • 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

Could you please tell me what exactly '%05d' does?
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.

Sign in to comment.

Categories

Find more on Images in Help Center and File Exchange

Products

Tags

Community Treasure Hunt

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

Start Hunting!