Recently, someone asked us some questions by email. I thought that the questions would be interesting to a wider Matlab programming audience, so I decided to post them here. The questions relate to GUIDE, arrays, and Matlab speed.
Here is the original email:
1st, is the coding for the function of all GUIDE tool linking? For
instance, the coding wrote in pushbutton_callback is connected with coding
wrote in pushbutton1_callback. Is that possible? Or I have to write in
separate .m file, each .m file consists of each function?
2nd, I want to display multiple images in uipanel as you can see in my
coding. So, is it possible to use subplot in uipanel?
3rd, I am totally new in Matlab, so basically immitate source code from
FileExchange or Matlab blogs. There are few errors quite often occurred
and I still can’t clear my doubt thru wiki/google.
For example 1: Attempted to access cell(2,1); index out of bounds because
size(cell)=[1,22]. for preallocate the database.
For example 2: ??? Undefined function or variable ‘Nfile. What does it
means? As I had “defined” it as Nfiles = numel(fnam); Is it something
wrong with my concept?
4th, I found that when run more often .m file, more slower the
performance. Is this the software problem or my computer problem?
Here are my answers to her questions:
1. You should be able to declare the callback function when you create
the pushbutton. Therefore, you should be able to use the same callback
function for different pushbuttons. If you want the pushbuttons to do
different tasks but use the same function, you could put a parameter in
your function call that specifies which task you want the function to
perform. You would then put a “switch” statement into your function to
use the parameter.
2. You should be able to use subplot in the UI panel.
3. I am not sure about your errors, because I can’t see your code.
However, I have some ideas about what might be wrong. Matlab uses arrays
for everything. The first index in an array specifies the row, and the
second index specifies the column. If you wanted row 4 and column 7 from
array A, you would type A(4,7). For the first error, it looks like you
are trying to access a location in the cell array that doesn’t exist. If
an array A is 1×22, then you can’t access location A(2,1), because that
array has only one row, and the location that you want is in the second
row. Additionally, be careful not to give any of your variables the name
of a data type or function. If you name a variable “cell”, which is data
type, then you may get strange effects in your code. For the second
error, it seems like you just have a typo. You are trying to use Nfile,
but you had already created a variable named Nfiles.
4. If I understand you correctly, Matlab on your computer runs more
slowly after you have opened it and run several m-files. You should make
sure that you clear your Matlab workspace with the “clear” command after
you run a script. Try to get rid of all the variables that you don’t need
to use, as they can slow down Matlab. If Matlab still runs slow, then try
to restart Matlab. If that doesn’t fix the problem, then try to restart