MATLAB Answers

sara sar

How to store values in cell array?

Asked by sara sar
on 13 Oct 2018
Latest activity Commented on by Image Analyst
on 16 Oct 2018
I made a GUI that allows the user to determine regions in an image by using ROI object. the ROI object return its position as a matrix. I want to save the positions in a cell array when ever the user determine region in the image.
How can I do that?


Sign in to comment.

2 Answers

Answer by madhan ravi
on 13 Oct 2018
Edited by madhan ravi
on 13 Oct 2018

An example:
A = {rand(4)} %stores the matrix as a cell array if you use curly braces
you can access the elements like a normal one
A{1}(1,1) %first element is obtained


Thank you @stephen
In fact I'm beginner with using matlab.. could you please explain where should I write what you wrote?
I have this function who is already initialized when I created the GUI:
% --- Outputs from this function are returned to the command line.
function varargout = untitled_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)% Get default command line output from handles structure
varargout{1} = handles.output;
Then I created a button that allow the user to specify region using ROI object:
% --- Executes on button press in rect.
function rect_Callback(hObject, eventdata, handles)
% hObject handle to rect (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% determine the positions
h = imrect;
position = getPosition(h);
SafeAreas = {position}
I do not understand where should I make the changes that suggested by you @stephen
Thank you so much!
@sara sar: if you want to pass data between callback function then use guidata (if you are unfortunately using GUIDE) or use nested functions (if you are happily writing your own GUI):
Note that callback functions do not return any output arguments, so there is no point in defining varargout.
If you want to return data from the GUI to the base workspace then either follow what I explained in my last comment, or if you are using GUIDE then use the OutputFcn:
Note that you can use waitfor inside the OutputFcn.

Sign in to comment.

Answer by Image Analyst
on 13 Oct 2018
Edited by Image Analyst
on 13 Oct 2018

You don't need a cell array. See attached demo.
The best way to save the coordinates of a segmented image is as a binary image, or as boundaries (after using bwboundaries()).
You can use imwrite() to save the binary image in a .PNG disk file, or use save() to save the boundaries in a .mat file on disk if you need to.


Show 1 older comment
OK, but not in a cell array. What would you put in the cell(s)? The binary image? The coordinates? Since you'd already have the variables to put into the cell (like you think you'd want), then why use an additional variable of a cell array? Just use the variables themselves and skip the cell array.
I want to put the coordinates in the cells. The user can specify more than one region using ROI objects. for example: there are 3 cars in the image. the user want to specify 2 of them, then the system will calculate the distance between the cars -that specified by the user- . So, I want to allow the user to specify many regions in the image, then I will store the coordinates of the ROI objects in cell array, after that the system will calculate the distance between them.
See my attached spatial calibration demo.

Sign in to comment.