Append matrix with subsequent images

3 views (last 30 days)
Hello,
I am having trouble solving a problem where I am trying to create a matrix that is later written to excel. The way the program works is that I upload an image into a GUI, and then alter the image so it is possible to count individual objects. I then have a button that saves the information to a matrix (the file name and the number of objects in the image). However, I am having trouble figuring out how to append the matrix. The way the code is set up, I am only able to create one array that only contains the latest file name and object number. I would like to be able to append the matrix with the information from subsequent images. Please let me know if you have any ideas! I imagine I would have to change the way new images are being stored (instead of just making the name and number equal to "x"). Would I have to write a loop in order to make this happen?
Additionally, I have tried using "xlsappend", but I keep receving this errror:
Error in
matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)GUI('pushbutton1_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback.
Array indices must be positive integers or logical values.
Error in GUI>pushbutton4_Callback (line 120)
Image_New_Name = name(Last_Characters:(Last_Characters+2));
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in GUI (line 17)
gui_mainfcn(gui_State, varargin{:});
Error in
matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)GUI('pushbutton4_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback.
This is how I search for and upload the image:
function pushbutton1_Callback(hObject, eventdata, handles)
%Define image as a useable variable by reading it from a folder
[Filename, Pathname]=uigetfile('*.jpg', 'File Selector');
global name
name=strcat(Pathname, Filename);
global Current_Image
Current_Image=imread(name);
set(handles.Browse, 'string', name);
axes(handles.axes1);
imshow(Current_Image);
This is how the program counts the images (bw2 is the file name, I figured I would spare everyone from the 20 lines of code that alters the image)
cc = bwconncomp(bw2,4);
global number
number = cc.NumObjects;
set(handles.text4,'string',number);
This is the button that saves the image information
function pushbutton4_Callback(hObject, eventdata, handles)
global number
global name
Image_Name_Length = length(name);
Last_Characters = (Image_Name_Length - 6);
Image_New_Name = name(Last_Characters:(Last_Characters+2));
New_Name_String = {Image_New_Name};
x = [New_Name_String, number]
header={'Image', 'Cells'};
xlswrite('Image Counter', header, 'Sheet1');
xlswrite('Image Counter', x, 'Sheet1');

Accepted Answer

Geoff Hayes
Geoff Hayes on 26 Apr 2019
Edited: Geoff Hayes on 26 Apr 2019
Kevin - you may want to consider not using global variables. Instead use the handles structure to save the data that you want other callbacks to have access to. For example, in one callback you might do
function pushbutton1_Callback(hObject, eventdata, handles)
[Filename, Pathname]=uigetfile('*.jpg', 'File Selector');
handles.name=strcat(Pathname, Filename);
handles.Current_Image=imread(handles.name);
set(handles.Browse, 'string', handles.name);
axes(handles.axes1);
imshow(handles.Current_Image);
guidata(hObject, handles); % saves the updated structure
Once you've added the fields to your handles structure, you then must call guidata(hObject, handles) which will save the updated structure and allow the other callbacks to have access to this updated structure. In the other callbacks, you would then reference this field to get the data you want. To guard against an error you might do something like
if isfield(handles, 'name')
name = handles.name;
% do something
end
As for your error message, it is being generated from the line
Image_New_Name = name(Last_Characters:(Last_Characters+2));
and it telling you that you are using indices that are not positive or not logical. I suspect that the Last_Characters variables is either 0 or a negative integer. This could be because the global name variables has not been set correctly (is it empty?). You could put a breakpoint at the line
Image_Name_Length = length(name);
and see what is name and what the length might be (zero?). Eliminating the global variables may help solve this problem...
As for appending new matrices, do you just want to append to the Excel spreadsheet (on the same page or a different one) or actually append matrices together. If the latter, you'd probably want a cell array to manage the different matrices since they could be of different dimensions. This cell array could be a field in your handles structure.
  1 Comment
Kevin Smith
Kevin Smith on 26 Apr 2019
Hello Geoff,
Thank you for the feedback! Unfortunately I am not experienced enough in matlab to understand what most of this means. But I do appreciate the suggestions as it gives me something new to learn.
For the excel sheet I want to add the new information to the next blank line. Currently, the way I have it set up, the data just overwrites every time.
Thank you!

Sign in to comment.

More Answers (0)

Categories

Find more on Line Plots in Help Center and File Exchange

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!