Tutorial04 MATRIX
Tutorial04 MATRIX
FAQ
Back to top
Contents [show]
A cell is a flexible type of variable that can hold any type of variable. A cell array is simply an array of those cells. It's somewhat confusing so
let's make an analogy. A cell is like a bucket. You can throw anything you want into the bucket: a string, an integer, a double, an array,
a structure, even another cell array. Now let's say you have an array of buckets - an array of cells or a "Cell Array". Each bucket can contain
something different, or they might all contain the same type of variable. Bucket 1 could contain a string, while bucket 2 could contain an image
(array of uint8's), while bucket 3 could be a int32. Or all buckets could contain strings of various lengths. It's totally flexible.
ca{1} = myString;
ca{2} = myInteger;
ca{3} = myDoubleArray;
ca{4} = rgbImage;
ca{5} = myStructure;
The braces should be read as "contents of", so if you say ca{4} = rgbImage, you are saying that "the content of" cell #4 is the variable
rgbImage.
Another way to use the cell is to refer to the cell itself, rather than the contents of it, and for that you use parentheses. The item it refers to
must be a cell. For example ca(1) is a cell, ca(2) is a cell, and ca(3) is a cell, even though those cells contain variables of arbitrary, and
possibly different, types. To make something a cell, you enclose it in braces, like this:
ca(1) = {myString};
ca(2) = {myInteger};
ca(3) = {myDoubleArray};
ca(4) = {rgbImage};
ca(5) = {myStructure};
This set of code is entirely equivalent to the first set of code. For the first line, it's basically like saying "Let's get a bucket (a cell) and put the
string into it - that's what {myString} by itself is. Then let's take that bucket and make it bucket #1, replacing any bucket that was already there."
In other words, take the cell {myString} and make it be element #1 (bucket #1) of the cell array called "ca." It uses parentheses which means it
refers to the whole single bucket (the bucket plus the contents) while the first set of code used braces which refers to only the contents of the
bucket. So ca(1) equals the cell "{myString}", while ca{1} equals the string "myString" because the braces said to get the contents of the cell.
In other words, ca{1} says don't give me the bucket with the string inside, just give me the string alone, without the bucket. It's just a slight
difference - a slightly different way of considering it. Saying
ca{1} = myString;
are equivalent for the most part. You can use either way and I don't really think one way or the other is really preferred. You can use
whatever way is easier for you to think about it. Maybe one way will be more intuitive for you than the other way, but again, they are
equivalent.
Cell arrays are similar to structures, which you probably are more familiar with, in that both are containers that can hold variables of a variety of
different types (arrays, strings, scalars, even other structures or cells). The difference is that with structures you refer to the different
"members" or "fields" by their name (e.g. UserSettings.myString), while with cells you refer to them by their index number (e.g. ca{1}).
https://matlab.fandom.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F 1/40
25.04.2019 FAQ | MATLAB Wiki | FANDOM powered by Wikia
Here is some demo code that may help explain cell arrays, and the type of classes you get when you use braces or parentheses:
One use of cell arrays is to hold lists of strings of different lengths. Since arrays are rectangular, you can't have an character array of strings
unless each string was the same length (or padded with blanks to be as long as the longest string). To get around that, you can use a cell
array instead of a character array. Each cell in the cell array would hold a string of a different length - they don't have to all be the same length
like with a character array. For example:
If you get strange error messages while working with cells or cell arrays, one easy thing to try is to change your braces into parentheses, or
your parentheses into braces, and see if that eliminates the errors.
It's also possible to mix indexing of the row and column of the cell array with the indexing of the contents of the single cell at that row and
column of the cell array. For example, let's create a cell array of 2 rows and 3 columns, and in every cell of that let's put a 4 element integer
array. Then we'll access the second element of the integer array at the cell in row 1, column 2 of the cell array.
https://matlab.fandom.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F 2/40
25.04.2019 FAQ | MATLAB Wiki | FANDOM powered by Wikia
fprintf('The second element of the integer array in cell row %d, column %d is %d\n',...
row, column, ourValue);
To visualize, imagine you had an array of buckets arranged in 2 rows and 3 columns (this is our cell array), and in each bucket are 4 billiard
balls arranged in a line. The above example goes to the bucket in the first row and second column, and reads off the number of the second
billiard ball in that bucket.
For more information, this link gives good examples about accessing cell data: http://www.mathworks.com/help/matlab/matlab_prog/access-
data-in-a-cell-array.html
It doesn't. Don't worry - your number is not truncated. It uses full double-precision floating point numbers to calculate everything. However, by
default it only prints a few decimal places to the screen in the command window. You can change this, to print out more decimal places, using
the command:
Type:
Why does the transpose operator take the complex conjugate? Edit
When performing linear algebra operations on complex matrices, it is almost always the complex conjugate transpose (also called the
Hermitian transpose) that is needed (see Gilbert Strang's linear algebra book for discussion- page 293 in edition 3). The bare apostrophe is an
operator that takes the complex conjugate transpose. The non-conjugating transpose operator is a period followed by an apostrophe. Type
help punct for more info.
By definition, NaN is not equal to any number, not even NaN itself. Therefore there are two ways to detect NaN values:
For speed purposes the use of isnan() tends to be 20%-30% faster. Here's a test snippet if you want to see the comparison:
t1 = 0; %time of isnan
t2 = 0; %time of ~=
for ii = 1:100
tic
idx1 = isnan(A);
t1 = t1+toc;
https://matlab.fandom.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F 3/40
25.04.2019 FAQ | MATLAB Wiki | FANDOM powered by Wikia
tic
idx2 = A~=A;
t2 = t2 + toc;
end
%{
ratio = 1.2179
ans = 1
%}
How can I make MATLAB use the full window width for displaying matrices? Edit
In R12 (MATLAB 6.0), this can be controlled via a preference. Select the File | Preferences... menu item, and select Command Window in the
Preferences dialog that appears. In the Display section, there's a checkbox labeled Limit matrix display width to eighty columns. Unchecking
that box allows matrix displays to make full use of the Command Window's width. [Unchecked is the default.]
Starting with MATLAB R12.1, users can access the current command window size using the root property CommandWindowSize. That is,
sz=get(0, 'CommandWindowSize');. In R12.0, there is no way to do this unless you call undocumented C functions from a MEX file.
%{
Stuff to be commented out
%}
You can also highlight a section of code and type control-r to comment out the code -- this will place a percent symbol (%) at the beginning
of the line. Typing control-t will uncomment the lines by removing any percent symbol that is the first non-blank character on the line.
If you have an older version, the built-in editor in MATLAB 6.0 has a block-comment feature, which will simply put a comment character on
each line. Or you can use matlab-mode for Emacs, which supports this as well.
if 0
commented out code
end
This is not the best solution, since parse errors inside that block will cause an error.
The key is to create a startup.m file. Look at the online help for more detailed instructions specific to your operating system.
You can use the following one-line function to determine the indices of the local maxima.
If you have the Image Processing Toolbox, you can use the imregionalmax() function. If you have the Signal Processing Toolbox you can use
the findpeaks() function.
You may have used a variable called "i" earlier in your program or session, thus overwriting the imaginary constant i with your own number. In
this case, MATLAB will use your new value for i instead of treating i as sqrt(-1). Five ways to ensure that you receive a complex result are:
y = 6i;
i=sqrt(-1)
y = 6*i;
clear i
y = 6*i;
Use j instead of i (assuming you haven't used a variable called "j" earlier in you program or session)
Note: these are very good reasons for not using i & j as indexes (in FOR loops, for example)
y = 6*j;
y = complex(0, 6);
Easiest solution: use the PATHTOOL GUI. Or if you want command line access:
Suggested by Joshua Stiff: You can use addpath to add directories from the command line, and path2rc to write the current path back to
`pathdef.m'. If you do not have write permissions for `pathdef.m', path2rc can write to a different file, which you can execute from your
'startup.m'.
What are the versions of MATLAB and associated runtime files? Edit
These are documented in some technical solutions Solution 1-4GSNCF Solution 1-1IW46N
How do I fix the error "Function definitions are not permitted in this context."? Edit
You can have multiple functions defined in one m-file, but before Matlab R2016b you can't have a script followed by one or more functions
in the same m-file. For example, this m-file:
https://matlab.fandom.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F 5/40
25.04.2019 FAQ | MATLAB Wiki | FANDOM powered by Wikia
clc; % Clear the command window.
m = magic(7);
MyCustomFunction(m)
because lines 1-5 are a script, and 7-9 are a function. To fix, add a "function" line as the first line in your code (after any comments) and it will
work. See below:
If you don't have that function line, it's a script, and you can't then define functions later (further down) in your code. By adding that line, you'll
be making a function out of the first few script lines in your m-file, and when all code is contained in functions, there is no error.
Programming Edit
Back to top
Please don't do this! You will find that MATLAB arrays (either numeric or cell) will let you do the same thing in a much faster, much more
readable way. For example, if A1 through A10 contain scalars, use:
Now refer to A(i) whenever you mean Ai. In case each Ai contains a vector or matrix, each with a different size, you want to use cell arrays,
which are intended exactly for this:
for k = 1 : 10
A{k} = 1 : k;
end
Note that each A{i} contains a different size matrix. And be sure to use the curly braces for the subscript, not parentheses! Remember (from
the "What is a cell array" FAQ entry above) that A(k) is the cell itself, while A{k} refers to the contents of the cell. So in our above example
A(k) is a cell while A{k} is an integer. See the FAQ entry on cells if this is still unclear to you.
Another approach is to use structures with dynamic field names instead of cell arrays. The fields of the structure can be the variable names
you want. And you can index into them with dynamic field references. For example:
https://matlab.fandom.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F 6/40
25.04.2019 FAQ | MATLAB Wiki | FANDOM powered by Wikia
In this case, you end up with the variable s, a structure, containing fields specified by the names in the strings that are stored in the cells of the
cell array. You can assign anything to the field such as a scalar, an array, a string, another structure, a cell array, or whatever you want. In this
example we just assigned the integer in the index variable.
Now, if you still really want to go against our advice and create variables with dynamically generated names, you need to use the eval()
function. With eval(), you use MATLAB commands to generate the string that will perform the operation you intend. For example, eval('A=10')
has the same effect as A=10, and eval(['A' 'B' '=10']) has the same effect as AB=10, only the eval() function executes much more slowly. So in
a loop, you could use:
for i=1:10
eval(sprintf('A%d = [1:i]', i));
end
Notice how much more obfuscated this is. It could be made possibly clearer to split it up into multiple lines:
for i=1:10
lineOfCode = sprintf('A%d = [1:i]', i);
% Now, execute lineOfCode just as if you'd typed
% >> Ai=[i:i]' into the command window
eval(lineOfCode);
end
In addition, this can cause difficult-to-troubleshoot problems in your code, particularly if you try to dynamically create a variable with the same
name as a function:
function y = mysin(x)
eval('sin = 5;');
y = sin(x);
Calling this function with "y = mysin(1)" will not return y = 5 (the first element of the sin variable created by EVAL) -- it will return the sine of 1,
because when the function was parsed there was no variable named sin and so the usage of sin on the last line was parsed as a call to the
built-in SIN function. The fact that a variable named sin existed at runtime is irrelevant; the parsetime "decision" takes precedence.
Repeat: don't create variables at runtime using eval() unless you have a very good reason, such as someone gives you a MAT file with
2000 variables named A1428, for example. Even in that case, you can avoid eval() by using dynamic field names of a structure:
If the files that you want to process are sequentially numbered, like "file1.txt", "file2.txt", "file3.txt", etc. then you can use SPRINTF or
NUM2STR to create the filename and LOAD, IMREAD, FOPEN, etc. to retrieve the data from the file. (Also note the three different ways of
building the file name - you can use your favorite way.)
https://matlab.fandom.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F 7/40
25.04.2019 FAQ | MATLAB Wiki | FANDOM powered by Wikia
for k = 1:20
% Create a mat filename, and load it into a structure called matData.
matFileName = sprintf('mat%d.mat', k);
if exist(matFileName, 'file')
matData = load(matFileName);
else
fprintf('File %s does not exist.\n', matFileName);
end
In the above code, matData, imageData, and textData will get overwritten each time. You should save them to an array or cell array if you need
to use them outside the loop, otherwise use them immediately inside the loop.
The second method is if you want to process all the files whose name matches a pattern in a directory. You can use the DIR function to
return a list of all file names matching the pattern, for example all .txt files or all .csv files, or all files named myFilennnn.dat (where nnnn is
some number). Note that while this example uses *.jpg as the pattern and IMREAD to read in the data, as with the previous example you could
use whatever pattern and file reading function suits your application's needs:
Or you can use the simpler, though not as robust, code inspired by this StackOverflow question :
csvfiles = dir('*.csv')
end
The simplistic code above assumes that all files will be in the current folder. Or you can try a "File Exchange Pick of the Week": FileFun .
https://matlab.fandom.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F 8/40
25.04.2019 FAQ | MATLAB Wiki | FANDOM powered by Wikia
If you call the dir() function to get file information (including file names), like this:
fileList = dir('*.*');
and get files sorted like this file1.dat, file10.dat, file11.dat, file2.dat, file20.dat, file21.dat, file3.dat, etc. and what you really wanted was them to
be sorted like this: file1.dat, file2.dat, file3.dat, file10.dat, file11.dat, file20.dat, file21.dat, then see Natural Order Sorting , a File Exchange
Pick of the Week.
How do I fix the error "Subscript indices must either be real positive integers or logicals."? Edit
In MATLAB all array indices must be logical or positive numeric integers. This means that the following is permitted:
>> A = [123,456,789];
>> logicalIndexes = A > 400 % Produces a logical (boolean) array of true (1) and false (0)
logicalIndexes =
0 1 1
>> A(logicalIndexes)
ans =
456 789
>> A(0)
>> A(-1)
Note that fractional numbers, negative integers, zero, and complex/imaginary numbers are not permitted indices. Note that zero is only
permitted as an index if it's not really an integer or double zero, but really "false" - a logical data type. When MATLAB displays logical values it
uses 0 and 1 rather than "false" and "true".
The reason is that the indexes refer to rows and columns in the array. So while you can have row #1 or column #3, you can't have row
#3.14159, nor can you have the 0th row or column, or the (-1)th row or column.
To fix the error you must make sure that your indexes are real, positive integer numbers, or logicals. They can be scalars (single numbers) or
vectors or arrays of many numbers. You might take the expression for your index and make it into a single variable, like myIndexes, and then
examine that in the variable editor or use code like this to figure out it's real data type and value:
The official Mathworks answer to this question can be found here: [2]
How do I fix the error "Function definitions are not permitted in this context"? Edit
https://matlab.fandom.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F 9/40
25.04.2019 FAQ | MATLAB Wiki | FANDOM powered by Wikia
You cannot mix a script and function(s) in the same m-file. You can have a script, and that script can call functions in other m-files, or you can
have all functions with no script at all. Most likely you have forgotten to include the "function" keyword and the name of your m-file as the first
executable line of your m-file. If you do that, it will probably work. See the following examples:
% Here is the first function, that has the name of the m-file:
function test % <----- Key function definition keyword!
clc;
% Clear the command window.
workspace; % Make sure the workspace panel is showing.
format compact;
m = randi(9, 2, 3) % Define sample data.
output = SomeFunction(m) % Call SomeFunction()
Note: if the name of the first function declared in the m-file does not match the file name, it will execute when called by the file name, not the
name after the function keyword. For example, in the second example above, if we had improperly called the function TestFunction
function TestFunction
then the code would execute by issuing the command "test" because that's the name of the m-file. Issuing the command "TestFunction" would
give the error "Undefined function or variable 'testFunction'."
Yes. If the file has nothing but numbers separated by whitespace, and has a constant number of columns through the entire file, you can just
type load myfile.txt.
The function DLMREAD is more flexible and allows you to read files with fields delimited by any character.
If you have mixed data, such as columns of text and numbers, you can use READTABLE. The first line is the column headers line and says
what the fields of the table will be called.
https://matlab.fandom.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F 10/40
25.04.2019 FAQ | MATLAB Wiki | FANDOM powered by Wikia
t = readtable('file.txt');
The function TEXTREAD is more flexible still and allows you to skip lines at the beginning, ignore certain comment lines, read text as well as
numbers, and more. Type help textread for more info.
If you are dealing with a more complicated file try XLSREAD, for example when opening a csv file with only partially numerical content.
If none of these suit your needs, you can always use the low-level file I/O functions FOPEN, FREAD, FSCANF, FGETL, FSEEK and FCLOSE
to read the data however you would like.
1) Using globals is perfectly okay (that's why they are there in first place), just like for's and while's and other intelligent 2nd generation
computer language constructs (that MATLAB is built on), as long as you are careful about where, why, and how you use them.
2) However, using globals is not recommended by many programmers because it shows that she/he didn't think ahead but was rather sloppy...
and just kept adding on those never-ending "important set up" parameters that she/he needed to use by all the axes - or whatever - of a one
single project.
3) Using globals can be a problem in terms of bookkeeping if you end up having dozens of them hovering around in your workspace. If you are
not careful and use the same names for global variables that represent different things, then you may start having problems if the global
<myParameter> from function1 is mixed up with the global <myParameter> from function2. You can do <whos global> to show you all the
global variables.
4) A nice way to reduce the number of global variables is to collect all your globals into a single structure (thereby uncluttering your
workspace). For example
% Declare UserSettings (or whatever name you want) as global in this function.
global UserSettings;
% Get the user folder of the user who logged in to the computer.
userFolder = getenv('USERPROFILE') % 'C:\Users\YourName
% Set up global variable, UserSettings, for use in any other function that
% declares UserSettings as global, but in ONLY those functions.
UserSettings.dataFolder = fullfile(userFolder, '\Documents')
UserSettings.resultsFolder = fullfile(UserSettings.dataFolder, '\Results')
UserSettings.someVariable = 42*pi;
Just attach any variables that you want to make global to the UserSettings structure. Now, any other function that declares UserSettings as
global will have access to all of the member fields of UserSettings. Other functions that do not have the "global UserSettings" line in them will
not be able to see the UserSettings global variable. It is not global unless the global line is included in the function.
5) Alternatively you can not have UserSettings as global and instead pass it as an input argument to only those functions that need it. This is
one preferred way of sharing variables in MATLAB ... and it shows your intelligence, to boot.
If you're using global variables because you want to share variables between functions, look at the section on How can I share data between
callback functions in my GUI. That section of this FAQ lists some alternatives to using global variables.
The logical vectors created from logical and relational operations can be used to reference subarrays. Suppose X is an ordinary matrix and L is
a matrix of the same size that is the result of some logical operation. Then X(L) specifies the elements of X where the elements of L are
nonzero.
https://matlab.fandom.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F 11/40
25.04.2019 FAQ | MATLAB Wiki | FANDOM powered by Wikia
m = magic(5)
% Get the logical matrix which is zero where
% m <= 20 and 1 where m >= 21
bigNumbersLocations = m > 20
% Extract those big numbers into an array
% Method #1:
bigNumbers = zeros(size(m));
bigNumbers(bigNumbersLocations) = m(bigNumbersLocations)
% Method #2:
bigNumbers2 = m;
bigNumbers2(~bigNumbersLocations) = 0
% Display the big numbers. It will be a 1D vector.
m(bigNumbersLocations)
m =
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
bigNumbersLocations =
0 1 0 0 0
1 0 0 0 0
0 0 0 0 1
0 0 0 1 0
0 0 1 0 0
bigNumbers =
0 24 0 0 0
23 0 0 0 0
0 0 0 0 22
0 0 0 21 0
0 0 25 0 0
bigNumbers2 =
0 24 0 0 0
23 0 0 0 0
0 0 0 0 22
0 0 0 21 0
0 0 25 0 0
ans =
23
24
25
21
22
I've discovered to my horror that structs take up an obscene amount of overhead (I'm running version 5.3.1.29215a (R11.1) on a Dec ALPHA).
I have a set of 10,242 observations, each consisting of 3+13=16 fields, which have 3*27 + 1*13 = 94 values. So the total size in bytes should
be 10,242 * 94 * 8 bytes/double = 7,701,984.
I have this stored in a 1 x 10242 data structure, and when I issue the whos command, it tells me that the data now takes up 27,367,136 bytes!
My guess would be that a structure contains MATLAB arrays. Each array has some overhead, like data type, array sizes, etc. In your second
implementation (index using data.latitude(observation)), there are 10,242 times less arrays allocated. Note that in your data, for each
observation, you have 13 arrays with one value. I don't know how large the matrix header exactly is, but it is a waste putting only a single value
in it!
https://matlab.fandom.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F 12/40
25.04.2019 FAQ | MATLAB Wiki | FANDOM powered by Wikia
I think Cris has hit it exactly. Every MATLAB matrix has an overhead of ~100 bytes, even matrices with a single element. In this example, there
are 16 fields * 10242 elements = 163872 matrices. Each one of these matrices adds an additional 100 bytes, for 16.4Mbytes in pure overhead.
This still comes up a little short of the amount reported, but it is fairly close.
It is much more efficient, both for storage and computation, to use a struct of arrays rather than an array of structs.
Memory errors are one likely reason. Greg Wolodkin suggests the debug memory manager:
The platform-independent way to use the debug memory manager is to set the environment variable MATLAB_MEM_MGR to contain the
string "debug".
On Windows:
$ MATLAB_MEM_MGR=debug matlab
The debug memory manager cannot catch your code the instant it writes out of bounds (tools like Purify can do this but the performance hit
they induce is quite painful). What it will catch is that in general, when you write outside of one memory block you end up writing into another,
corrupting it or (in the case of the debug memory manager) hopefully corrupting only a guard band. When you later free the memory, we can
tell you that you walked off the end of the block and corrupted the guard band.
In many programming languages, boolean operators like AND and OR will stop evaluating as soon as the result is known. For instance,
1 | error('Short-circuit')
would never get to the error part, since the 1 is always true.
MATLAB versions >= 6.5 include the new short-circuiting logical operators || and &&. Use these for all condition tests in loops and similar, and
use the old | and & for element-by-element logical operations. You can find details here
(http://www.mathworks.com/help/techdoc/ref/logicaloperatorsshortcircuit.html ).
In older versions of MATLAB, the boolean operators | and & are only short-circuit evaluated inside the conditions of IF and WHILE statements.
In all other contexts, all parts of the conditional are evaluated.
A frequent variant of this question is: "I have 3 GB of RAM, and 2 GB of swap space. Why can't I create this 600 MB matrix?"
https://matlab.fandom.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F 13/40
25.04.2019 FAQ | MATLAB Wiki | FANDOM powered by Wikia
First of all, issue the command "memory" in the command window to see how much memory is available on your computer. It should return
something like this:
>> memory
Maximum possible array: 520 MB (5.457e+008 bytes) *
Memory available for all arrays: 1343 MB (1.409e+009 bytes) **
Memory used by MATLAB: 447 MB (4.690e+008 bytes)
Physical Memory (RAM): 3036 MB (3.184e+009 bytes)
Remember that double precision floats take up 8 bytes. So a million element vector takes up 8Mbytes. Be sure you're estimating properly.
Many operations need to create duplicate matrices. For example, B = inv(A.') must create a tempory variable the same size as A to hold the
transpose, and B is the same size as A.
If you're sure your matrices are reasonably sized, then read these Mathworks Technical Notes: Tech Note 1106: Memory Management Guide
and Tech Note 1107: Avoiding Out of Memory Errors
Another variant of this question is: "How do I pre-allocate memory when using MATLAB?"
If the matrix size is not defined prior to populating it with data through a FOR loop, memory fragmentation problems may happen since
MATLAB is not aware of the final matrix size upon the conclusion of the FOR loop. In order to work around this issue, one solution is to pre-
allocate memory by creating an initial matrix of zeros with the final size of the matrix being populated in the FOR loop. You should read the
following Mathworks article: Technical Solution 1-18150 for a more complete discussion of this problem.
fname = 'foobag';
save fname variable;
fname = 'foobar';
save(fname, 'variable');
In fact, it is true in general that the following two lines are equivalent:
This allows one replace any or all of the parameters with dynamically generated strings. This is also useful in commands like PRINT, LOAD,
CLEAR, etc.
What's the difference between M-files, Pcode, and MEX files? Edit
M-files are plain ASCII text that is interpreted at run time. Actually it is parsed once and "just-in-time" compiled, but this is transparent to
the user. Use M-files for most of your MATLAB development, and for platform independence and maintainability.
Pcode is a preparsed and encoded version of the M-file. Since it is preparsed, it saves on the load time of the function. This is most likely
not an issue except for very large M-files, since most are parsed only once anyway. Pcode also lets you hide the source code from
others. Careful, there is no way to convert Pcode back to the M-file source. Pcode is platform independent.
https://matlab.fandom.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F 14/40
25.04.2019 FAQ | MATLAB Wiki | FANDOM powered by Wikia
MEX files are native C or C++ files that are dynamically linked directly into the MATLAB application at runtime. They must be compiled
for each hardware architecture on which they are to be run. MEX files have the potential to crash the MATLAB application, but rather
large speed gains are possible, depending on the algorithm.
If you are attempting to use pass-by-reference to modify the input argument passed into a function, the answer to the question depends on
whether the input is a handle object or a value object. The two types of objects are described in the Object-Oriented Programming
(http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_oop/brfylq3.html ) and Programming Fundamentals
(http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/brtm8si.html ) sections of the documentation. By default, objects
(including matrices, arrays, etc. of the built-in data types) are value objects.
Handle objects do exhibit reference behavior when passed as function arguments; value objects do not. When you pass a handle object to a
function, MATLAB still copies the value of the argument to the parameter variable in the function (with one bit of subtlety; see below.)
However, all copies of a handle object refer to the same underlying object.
If a function modifies a handle object passed as an input argument, the modification affects the object referenced by both the original and
copied handles. In this case, the function does not need to return the result to be reassigned.
If instead you are attempting to use pass-by-reference to avoid unnecessary copying of data into the workspace of the function you're calling,
you should be aware that MATLAB uses a system commonly called "copy-on-write" to avoid making a copy of the input argument inside the
function workspace until or unless you modify the input argument. If you do not modify the input argument, MATLAB will avoid making a copy.
For instance, in this code:
function y = functionOfLargeMatrix(x)
y = x(1);
MATLAB will not make a copy of the input in the workspace of functionOfLargeMatrix, as x is not being changed in that function. If on the other
hand, you called this function:
function y = functionOfLargeMatrix2(x)
x(2) = 2;
y = x(1);
then x is being modified inside the workspace of functionOfLargeMatrix2, and so a copy must be made.
How can I pass additional parameters into a "function function" like ODE45, QUAD, FSOLVE, FMINCON, GA,
etc? Edit
The term "function functions" refers to functions in MATLAB and the toolboxes that accept a function (usually a function handle) and evaluate
that function repeatedly during the course of their work. Some examples of "function functions" are:
There are several documents on The MathWorks support website that shows examples of how to pass additional parameters to the functions
used by the "function functions".
https://matlab.fandom.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F 15/40
25.04.2019 FAQ | MATLAB Wiki | FANDOM powered by Wikia
ODE solvers like ODE45 and integration functions like QUAD or QUADL (http://www.mathworks.com/support/solutions/data/1-
1AIJF.html?solution=1-1AIJF )
Optimization functions in toolbox/matlab/funfun and toolbox/optim (FMINCON, FSOLVE, FZERO, LSQCURVEFIT, etc.)
(http://www.mathworks.com/support/solutions/data/1-19HM6.html?solution=1-19HM6 )
Genetic Algorithm and Direct Search Toolbox functions (GA, PATTERNSEARCH) (http://www.mathworks.com/support/solutions/data/1-
1BZ6V.html?solution=1-1BZ6V )
This information has also been incorporated into the documentation in recent versions of MATLAB and the toolboxes:
MATLAB (http://www.mathworks.com/access/helpdesk/help/techdoc/math/f2-941087.html )
Optimization Toolbox (http://www.mathworks.com/access/helpdesk/help/toolbox/optim/ug/f67947.html )
GA function, Genetic Algorithm and Direct Search Toolbox (http://www.mathworks.com/access/helpdesk/help/toolbox/gads/bqlex3b-
1.html )
PATTERNSEARCH function, Genetic Algorithm and Direct Search Toolbox
(http://www.mathworks.com/access/helpdesk/help//gads/f12810.html )
Why does MATLAB clear the breakpoints I've set in my M-file? Edit
When a function is cleared from memory using the CLEAR function, breakpoints in that file are also cleared. That means that if you execute
code including the statement "clear functions", "clear <the name of your function>", or "clear all", your breakpoints will be cleared.
If you want to have your program stop execution and enter debug mode regardless of whether or not you have cleared it, insert a call to the
KEYBOARD function into the code at the location where you want to enter debug mode. This will not be cleared by the CLEAR function and
will cause MATLAB to enter debug mode when the KEYBOARD function is called.
Alternately, you can use the "Stop if Errors/Warnings" item under the Debug menu in the MATLAB Editor to cause MATLAB to enter debug
mode whenever the code you're running throws an error that is not caught by a TRY/CATCH block, whenever it throws an error that is caught
by a TRY/CATCH block, whenever it throws a warning, or whenever the value of a variable becomes Inf or NaN. You can find more information
on this item in the documentation (http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_env/bq4hw4g-1.html ).
Why do I receive an error about an undefined variable or the recursion limit when calling ODE45? Edit
One common cause of an "Undefined function or variable" error or an error about the RecursionLimit being exceeded when using an ODE
solver like ODE45 is that the function being called by the ODE solver itself contains the call to the ODE solver. For instance, if your code is:
function dy = myodefunction(t, y)
dy = [y(2); 2*t];
y0 = [0; 1];
tspan = [0 10];
[t, y] = ode45(@myodefunction, y0, tspan);
If you call myodefunction with no inputs, you will receive an error on the second line, where MATLAB tries to use t and y to define dy. If you
call myodefunction with two inputs, it will proceed without error to the ODE45 call. ODE45 will call myodefunction with two inputs, and that call
will proceed without error to the ODE45 call. This process will repeat until ODE45 has recursively call myodefunction a number of times equal
to the root RecursionLimit property, at which point MATLAB will throw an error.
To avoid these errors, do not include the call to the ODE solver inside your ODE function (the function you pass to the ODE solver as the first
input argument.) Instead, define the ODE function in a subfunction or as a separate function file and use that in your ODE45 call.
% begin myodefunction.m
function [t, y] = myodefunction
y0 = [0; 1];
https://matlab.fandom.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F 16/40
25.04.2019 FAQ | MATLAB Wiki | FANDOM powered by Wikia
tspan = [0 10];
[t, y] = ode45(@myodesubfun, y0, tspan);
function dy = myodesubfun(t, y)
dy = [y(2); 2*t];
% end myodefunction.m
The separate file approach requires one function or script file that calls ODE45:
% begin myodefunction.m
function [t, y] = myodefunction
y0 = [0; 1];
tspan = [0 10];
[t, y] = ode45(@myodeseparatefun, y0, tspan);
% end myodefunction.m
% begin myodeseparatefun.m
function dy = myodeseparatefun(t, y)
dy = [y(2); 2*t];
% end myodeseparatefun.m
The EVAL function is one of the most powerful, flexible, and potentially dangerous functions in MATLAB. Since EVAL is so powerful, it is
easy to misuse the function. In a way, the EVAL function is a lot like global variables; both are tools that are so easy to use that it might be
easier to use them rather than to search for a more elegant, safer, and appropriate solution. There is a major drawback to the EVAL function,
although it can be avoided if you use EVAL carefully. EVAL can be used to alter arbitrary variables. In addition, two related functions, evalin
and assignin, can be used to alter variables in different function workspaces. These functions can create bugs which are difficult to reproduce
and nearly impossible to eliminate. Further explanation, and The Mathworks official warning against using eval, can be found in Mathworks
Tech Note 1103 .
Where did my file go? The risks of using the cd function. Edit
Sometimes if you're writing code that does file input/output without specifying the full path (folder + base file name) you may not find your file. It
may not be there to read in or may not be written out to the folder that you expected. For example
storedStruct = load('mySavedParameters.mat');
imwrite('myOutputImage.png')
If another function call used the cd() function to change the current folder, then you would be looking to that folder when you tried to read in a
file or write out a file. If you thought that you were looking at a different folder, then you'll get a "file not found" error upon trying to read in a file,
or else not find the file in that folder that you thought you wrote out to. It would be best if any code that used cd() saved and restored the
original folder:
originalFolder = pwd;
cd(originalFolder);
but you cannot always rely upon that. It's much, much better to not use cd() and instead create the full-blown explicit filename with functions
such as sprintf(), fileparts(), and fullfile(). Because if you have the full path name of the file, you'll know for certain where it will get saved to or
read from. See the following code for guidance:
Graphics Edit
Back to top
Unlike custom functions that you write, the callback functions take the predetermined input variables (hObject, eventdata, handles) so it's not
clear how you can pass in other variables that you need to. There are several techniques you can use to share data between callback
functions in your GUI. Which approach is easiest depends on whether you are creating your GUI using GUIDE to design the layout (GUIDE-
based GUIs) or whether you are calling FIGURE, AXES, UICONTROL, etc. directly (programmatic GUIs.)
Using the handles structure. You can dynamically add on new members to the handles structure that contain your variables that you want
to pass in. Unlike global variables, which expose the variables only where you put the global statement, attaching to the handles
structure and passing handles will share all the variables you have attached to the handles structure. This could expose variables to your
function that you did not need or want exposed. Since variables passed in to MATLAB functions are "pass by value" and not "pass by
reference" if you change any of the variables, you are only changing the local copy. If you change any of the variables attached as
members of the handle structure, and you want to retain these changed values in the calling function, you will have to return the handles
structure as an output of your function, and accept it in your calling function, or else use the guidata() function. Otherwise changes you
make to the handles structure variables are only local and will not survive once the function has returned.
Creating your callback functions as nested functions inside the main GUI function. The function must be truly nested with the main
function's "end" statement and not merely listed as another separate, independent function just defined in the m-file. (There is a
difference.)
Storing data in a property [often the UserData property] of a component in the GUI and retrieving it when needed. For example
Storing data in the application workspace using the SETAPPDATA and GETAPPDATA functions.
https://matlab.fandom.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F 18/40
25.04.2019 FAQ | MATLAB Wiki | FANDOM powered by Wikia
Declaring the variable(s) global. For example, if you have this inside a function:
global myVariable; % Declare global. Any function with this line it it can see this variable.
then any other function that also has the "global myVariable" declaration in it will be able to see the variable called "myVariable". Functions
that do not have the "global myVariable" line in them will not be able to see the variable. So it's actually more like a "friend" variable (if you're
familiar with C++ programming) than a truly global variable because not every function or workspace sees it. The "myVariable" variable will not
be seen in the "base" workspace - it will be seen only inside functions with the "global myVariable" declaration in them.
Writing the data out to a file, such as a mat file with the save function, and then having the called function read in that mat file.
Sharing between multiple GUIs. If the "main" GUI calls other GUIs, then the best way to do it is by passing variables in via the input
argument list, and accepting output variables via the output argument list. The output argument of GUI2 can then be sent into GUI3. So
someplace in GUI1 (like the callback function of the "Go!" button of GUI1), you'd have this
or something along those lines. The arguments can be extracted out of the varargin cell array of your opening code for the GUI, for example in
the GUI's "OpeningFcn" function if you used GUIDE. Once they are in your opening function, then they can be shared amongst the other
functions in the GUI with the methods mentioned earlier in this section. This method will not let GUI1 control GUI2 and GUI3's parameters
"live" - they can be changed only when calling the GUIs. To have GUI1 control GUI2 when GUI2 is already running, you can use the assignin()
function.
This answer from Geoff Hayes in the Answers forum may also help you in the multiple GUI situation: [3]
Official Documentation. The documentation contains instructions for using these techniques to share data between callbacks in these
Mathworks web pages:
Doug Hull's video tutorial Passing data between GUIDE callbacks without globals in MATLAB
Doug Hull's Video Tutorial How to pass data from one GUI to another
The ticklabel gets its properties from the axis to which it is attached. So set(gca, 'fontsize', 14) should do the trick. Type get(gca) to see what
else you can set.
Not directly... MATLAB does not interpret TeX strings in ticklabels. You can play games with placing text by hand. See the MathWorks solution
for some ideas of how to work around this.
There are also some free third-party software packages you can use to accomplish this. One community-generated solution in the MATLAB
File Exchange is Format Tick Labels (http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=15986) as an example. It
will replace axes tick labels with formatted text objects that can include both Tex and LaTex interpreted strings.
Doug Schwarz has written a Styled Text Toolbox that does this. It is freely available at:
http://www.frontiernet.net/~dmschwarz/stextfun/index.html
At least from 2014 this is supported via MATLAB. See here: Change Axis Tick Values and Labels
As of MATLAB 7.0 (R14), you can use the 'MultiSelect' parameter with UIGETFILE to allow the selection of multiple files. If you are using a
version of MATLAB prior to version 7.0, you can use the `uigetfiles.dll' submission on the MATLAB Central File Exchange to do this on a
Windows platform. This file is located here (http://www.mathworks.com/matlabcentral/fileexchange/Files.jsp?type=category&id=&fileId=331
).
Sorry, there's no easy solution. MATLAB does not support hierarchical figures, so you can't have a container control holding your controls. If
you really need this you'll have to create your own, using the callbacks from the scrollbar to modify the position properties of your controls.
What I would do is to add 2 pushbuttons to the figure: one at the top right and another at the bottom right and use these buttons to control the
vertical scroll of the content of the figure. (surely you can add another 2 horizontal pushbuttons, lower left and lower right).
Whenever any of these buttons is pressed, you loop over all the controls except the two pushbuttons, and increment/decrement the
vertical/horizontal postition value of each control. Something like this:
https://matlab.fandom.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F 20/40
25.04.2019 FAQ | MATLAB Wiki | FANDOM powered by Wikia
As of release R2014b, set the axes XTickLabelRotation, YTickLabelRotation, or ZTickLabelRotation properties. See the "Tick Values and
Labels" section of the documentation for axes properties for more information.
For earlier releases, you cannot rotate tick labels directly but there are some community-generated solutions to simulate this in the MATLAB
Central File Exchange. See Format Tick Labels (http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=15986 ) as
an example.
If you simply want to edit the matrix as if it were an Excel spreadsheet, you can use the builtin Array Editor. Type openvar(my_var) at the
command prompt, or double click on the variable in the Workspace Browser. Or you can use the uitable control. See the help for uitable.
filename = 'cameraman.tif';
variableValue = 1234;
One way to do this is to insert a CAXIS command in your plotting function/script. For this to work well, you first need to have a first look at all of
your data to determine what are the minimum and maximum values over the entire set of images. For example, if the overall minimum value
amongst all images is 40 and the overall maximum is 256, you may issue the command "caxis([40 260])" for each image.
Tech note 1215, at http://www.mathworks.com/support/tech-notes/1200/1215.shtml , addresses a related question, namely "How can I use
multiple colormaps in a single figure". As a bonus, it includes a thorough discussion of colormaps in general.
There are probably several hundred of these default handle graphics options. Rather than trying to remember any particular one, the best thing
is to learn the general principle behind all of these default handle graphics properties. The basic call to insert into your startup.m file is :
set(0,'DefaultObjectnamePropertyName',Value)
set(0,'DefaultLineMarkerSize',12);
set(0,'DefaultLineMarker','d');
set(0,'DefaultLineLineWidth', 2);
set(0,'DefaultAxesLineWidth', 2);
set(0,'DefaultAxesXGrid','on');
set(0,'DefaultAxesTickDir','out');
set(0,'DefaultAxesTickLength',[0.015 0.015]);
set(0,'DefaultAxesFontName','Arial')
https://matlab.fandom.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F 21/40
25.04.2019 FAQ | MATLAB Wiki | FANDOM powered by Wikia
For more details, do a full text search for 'Defining Default Values' in the R12 online help, and click on the very first hit. Also see the following
entries in the R12 online help:
How can I modify the default bar color used for histograms? Edit
A histogram is made up of patch objects. The trick is to modify the FaceColor property of these patches. A short example follows:
x=rand(400,1);
hist(x); % Default facecolor is blue
h=get(gca,'Children');
set(h,'FaceColor', 'm'); % magenta facecolor
How can I draw more than two lines with plotyy? Edit
You can use the axes' handles to plot additional lines, as follows:
This behavior, where MOVIE displayed the movie one time more than was requested, was eliminated in MATLAB 7.4 (R2007a), as mentioned
in the release notes (http://www.mathworks.com/access/helpdesk/help/techdoc/rn/bq08z9s-1.html#bq08z9s-3 ).
In older releases, the movie function displayed each frame as it loaded the data into memory, and then played the movie the requested
number of times all the data was loaded. This eliminated long delays with a blank screen when you loaded a memory-intensive movie. The
movie's load cycle was not considered one of the movie repetitions.
You can't. One hopes that The MathWorks will include this often-requested feature in a future, but there is no guarantee.
Related to this, changing the stacking order of your GUI elements might allow you to set the tab order, but this seems to not always work.
GUIDE in MATLAB version >= 6.5 includes a Tab Order Editor, which does a better job at this.
The text command can be used in a vectorized form to automatically add text labels wherever needed. Say you have a matrix D, where the
first column contains X coordinates and the second column contains Y coordinates. Then us illustrates:
https://matlab.fandom.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F 22/40
25.04.2019 FAQ | MATLAB Wiki | FANDOM powered by Wikia
plot(D(:,1),D(:,2),'+-');
n=num2str(D,'%5.3f/');
n=n(:,1:end-1); % Just to remove the trailing slash
text(D(:,1),D(:,2),n);
http://www.mathworks.com/help/techdoc/creating_plots/f10-1460.html
How do I save my figure, axes, or image? I'm having trouble with the built in MATLAB functions. Edit
Many people encounter trouble when they try to use the various built in MATLAB functions (such as print, saveas, hgsave, etc.) for saving
figures, axes, or images. For example, the colors may be different than expected, there may be undesired spacing around it, it may be all black
or all white, the overlay graphics don't get saved along with the underlying image, the resolution is not correct, the wrong part of the window is
being saved, etc. Try using export_fig in the File Exchange . This may solve your problems. Does this work?
How can I get Greek letters and other symbols on my GUI? Edit
Math/Algorithms Edit
Back to top
Why is 0.3 - 0.2 - 0.1 (or similar) not equal to zero? Edit
As is mentioned frequently in the newsgroup, some floating point numbers can not be represented exactly in binary form. So that's why you
see the very small but not zero result. See EPS.
The difference is that 0:0.1:0.4 increments by a number very close to but not exactly 0.1 for the reasons mentioned below. So after a few steps
it will be off whereas [0 0.1 0.2 0.3 0.4] is forcing the the numbers to their proper value, as accurately as they can be represented anyway.
If you're trying to compare two floating-point numbers, be very careful about using == to do so.
An alternate comparison method is to check if the two numbers you're comparing are "close enough" (within a tolerance value of each other).
For example:
% Instead of a == b
a = 0.3;
b = 0.1 + 0.2;
% Instead of a == b
% with releases R2014b and earlier, use:
a = 0.3;
b = 0.1 + 0.2;
tol = 5 * eps(a)
https://matlab.fandom.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F 24/40
25.04.2019 FAQ | MATLAB Wiki | FANDOM powered by Wikia
areEssentiallyEqual = abs(a-b) < tol
% for some small value of tol relative to a and b
You can see this same sort of behavior outside MATLAB. Using pencil and paper (or a chalkboard, or a whiteboard, etc.) compute x = 1/3 to
as many decimal places as you want. The number of decimal places must be finite, however. Now compute y = 3*x. In exact arithmetic, y
would be exactly 1; however, since x is not exactly one third but is a rounded approximation to one third, y will not be exactly 1.
For a readable introduction to floating point arithmetic, look at Cleve's Corner article from 1996: Floating Points (PDF)
(http://www.mathworks.com/company/newsletters/news_notes/pdf/Fall96Cleve.pdf )
For an official "Answer" by the Mathworks in the Answers forum, read Why-does-mat2str-0-005-90-return-0-0050000000000000001-in-matlab
For more rigorous and detailed information on floating point arithmetic, read the following paper: What Every Computer Scientist Should Know
About Floating Point Arithmetic (http://docs.sun.com/source/806-3568/ncg_goldberg.html )
Another resource is Technical Note 1108 (http://www.mathworks.com/support/tech-notes/1100/1108.html ) on the Support section of The
MathWorks website.
Let's say you have a column vector or row vector and want to compute the mean of vector values in a window of length N that moves in jumps
of N elements. In other words,you want to compute the mean in each block, with the blocks not overlapping. The window of length N will
"jump" by N elements each time. Below is sample code that does this for both a column vector and a row vector.
% Compute the means of elements 1-3, then of 4-6, then of 7-9, etc.
columnVector = (1 : 15)'
means3 = mean(m2d, 1)
rowVector = (1 : 15)
means3 = mean(m2d, 1)
m2d =
1 4 7 10 13
2 5 8 11 14
3 6 9 12 15
https://matlab.fandom.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F 25/40
25.04.2019 FAQ | MATLAB Wiki | FANDOM powered by Wikia
means3 =
2 5 8 11 14
To create a 2D logical image of a solid circle (a disc), you can use code like this:
It can be extended to handle ellipses by putting in factors inside the sqrt() in the obvious places. If you want, this circle mask can be used to
assign image values either inside or outside the circle to a new gray level:
If you would like the circle to be only a single pixel wide circumference in the digital image, rather than a solid disc, then you can use this
code:
% Write those (x,y) into the image with gray level 255.
for k = 1 : length(x)
row = round(y(k));
col = round(x(k));
myImage(row, col) = 255;
end
% Display the image. It may appear as though there are gaps in the circle
% due to subsampling for display but examine the image in the variable inspector
https://matlab.fandom.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F 26/40
25.04.2019 FAQ | MATLAB Wiki | FANDOM powered by Wikia
% and you'll see there are no gaps/breaks in the circle.
imshow(myImage);
axis('on', 'image');
% Plot crosshairs in the overlay at the center
hold on;
plot(xCenter, yCenter, 'r+', 'MarkerSize', 100);
If you want to plot a circle, you can use the rectangle() function:
radius = 5;
centerX = 20;
centerY = 30;
rectangle('Position',[centerX - radius, centerY - radius, radius*2, radius*2],...
'Curvature',[1,1],...
'FaceColor','r');
axis square;
Newer versions of the Image Processing Toolbox have the viscircles() function for plotting multiple circles simultaneously in the overlay above
the digital image.
radius = 5;
centerX = 20;
centerY = 30;
viscircles([centerX, centerY], radius);
axis square;
Or, if you want a list of x and y coordinates of the perimeter of the circle, or of an arc, you can do this:
xCenter = 12;
yCenter = 10;
theta = 0 : 0.01 : 2*pi;
radius = 5;
x = radius * cos(theta) + xCenter;
y = radius * sin(theta) + yCenter;
plot(x, y);
axis square;
xlim([0 20]);
ylim([0 20]);
grid on;
Note that it will be a circle according to the tick marks on the axes, but whether it appears perfectly circular on your computer monitor depends
on your video adapter settings, and whether you use, or don't use, the 'square' and 'equal' options for the axis command. The above code can
also be used to create an arc - a portion of a circle. Just change the line that assigns theta to start and stop at the desired angles of the arc.
If you want this to be in the overlay of an existing plot or image, issue the "hold on" command prior to issuing the rectangle command, or else
the circle will destroy the existing plot or image.
The use of meshgrid (or ndgrid) can be easily extended to 3D to create a logical mask for a sphere.
Creating an arc is very similar to creating a circle. Simply specify a starting and ending angle for theta.
The code is very similar to the code to create a circle from above.
xCenter = 12.5;
yCenter = 10;
xRadius = 2.5;
yRadius = 8;
theta = 0 : 0.01 : 2*pi;
x = xRadius * cos(theta) + xCenter;
y = yRadius * sin(theta) + yCenter;
plot(x, y, 'LineWidth', 3);
axis square;
xlim([0 20]);
ylim([0 20]);
grid on;
The code is very similar to the code to create a circle from above - just specify an inner and outer radius.
https://matlab.fandom.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F 28/40
25.04.2019 FAQ | MATLAB Wiki | FANDOM powered by Wikia
% Next create the circle in the image.
centerX = 320;
centerY = 240;
innerRadius = 100;
outerRadius = 140;
array2D = (rowsInImage - centerY).^2 ...
+ (columnsInImage - centerX).^2;
ringPixels = array2D >= innerRadius.^2 & array2D <= outerRadius.^2;
% ringPixels is a 2D "logical" array.
% Now, display it.
image(ringPixels) ;
colormap([0 0 0; 1 1 1]);
title('Binary Image of a Ring', 'FontSize', 25);
An elegant chunk of code to perform least-squares circle fitting was written by Bucher Izhak and has been floating around the newgroup for
some time. The first reference to it that I can find is in msgid:<3A13371D.A732886D%40skm.com.au>:
Tom Davis provided a more sophisticated approach that works for more cases in msgid:<[email protected]> and msgid:
<[email protected]>. Code included.
See this paper Least-squares orthogonal distances fitting of circle, sphere, ellipse, hyperbola, and parabola
To create a set of (x,y) coordinates within the perimeter of a solid circle (a disc), you can use code like this from Roger Stafford in his Answers
posting :
https://matlab.fandom.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F 29/40
25.04.2019 FAQ | MATLAB Wiki | FANDOM powered by Wikia
grid on;
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
fontSize = 30;
xlabel('X', 'FontSize', fontSize);
ylabel('Y', 'FontSize', fontSize);
title('Random Locations Within a Circle', 'FontSize', fontSize);
To create a set of (x, y, z) coordinates uniformly and randomly distributed over the surface of a hollow sphere (a round shell), you can use code
like this from Roger Stafford in his Answers Forum posting [4]
% Define the radius of the shell of points, the surface of the sphere.
radius = 10;
% Define the number of points to place on the surface of the sphere.
numPoints = 1000; % Use a large value.
% Get a 3-by-numPoints list of (x, y, z) coordinates.
r = randn(3, numPoints);
% At this point the points can be anywhere in 3D space,
% not just on the surface of a sphere, a shell of constant radius.
% Divide by the distance of each point from the origin.
% This will place each point out at a definite radius of 1, not scattered throughout the volume.
r = bsxfun(@rdivide, r, sqrt(sum(r.^2,1)));
% Now multiply by radius to make the shell out
% at a specified radius instead of a radius of exactly 1
r = radius * r;
% Extract the x, y, and z coordinates from the array.
x = r(1,:); % Extract x from row #1.
y = r(2,:); % Extract y from row #2.
z = r(3,:); % Extract z from row #3.
% Display the shell of points
scatter3(x, y, z);
axis square; % Make sure the aspect ratio is maintained as it's displayed and rotated.
xlabel('X', 'FontSize', 20);
ylabel('Y', 'FontSize', 20);
zlabel('Z', 'FontSize', 20);
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
msgbox('Now use the circular arrow icon on the toolbar to rotate the sphere.');
You can use the built-in function polyarea. If you wish to avoid using 'polyarea', one method is this (offered by Roger Stafford). Let x and y be
vectors of the corresponding coordinates of the polygon's vertices taken in counterclockwise order around the polygon.
area = 1/2*sum(x.*y([2:end,1])-y.*x([2:end,1]));
You can find the areas of closed contours you generated with the contour function with the following code:
How does the backslash operator work? What does it do? Edit
For full matrices, pseudocode describing the algorithm can be found in the MathWorks Technical Solution How does the backslash operator
work when A is full? (http://www.mathworks.com/support/solutions/data/1-172BD.html?solution=1-172BD ) or Operating on Sparse
https://matlab.fandom.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F 30/40
25.04.2019 FAQ | MATLAB Wiki | FANDOM powered by Wikia
Matrices (http://www.mathworks.com/access/helpdesk/help/techdoc/index.html?/access/helpdesk/help/techdoc/math/f6-8856.html ).
Also for sparse matrices, you can turn on monitoring routines that show you some of the steps. Use spparms('spumoni', 1), or
spparms('spumoni', 2)
After MATLAB5.3, there is a factorial function, but it is not vectorized. Why this is so we will never know. Instead, you can use the gamma
function, which is vectorized. Careful, factorial(n) = gamma(n+1). If you are trying to compute a ratio of factorials, see the next question.
If you are trying to compute "n choose k", just use the function nchoosek. Otherwise, Paul Skoczylas suggests in msgid:
<[email protected]>
If I wanted to evaluate n!/(n-j)! for large values of n and/or j (but still assuming n>j), I would use the gammaln function.
gamma(m+1)=m!
gammaln(m+1)=log(m!)
A=n!/(n-j)!
log(A)=log(n!/(n-j)!)
log(A)=log(n!)-log((n-j)!)
A=exp(log(n!)-log((n-j)!))
so,
A=exp(gammaln(n+1)-gammaln(n-j+1))
In the same way there are two solutions (plus and minus) for the square root of a positive number, there are multiple solutions for roots of
negative (and complex) numbers. If you express the number in magnitude*exp(i*theta) form, the cube root (for instance) takes the form
(magnitude^(1/3))*exp(i*(theta+2k*pi)/3), for k=0:2 or 1:3.
-8 is 8*exp(i*pi), so theta=pi and the cube roots are 2*exp(i*pi/3), 2*exp(-i*pi/3), and 2*exp(i*pi). The last one simplifies to -2.
MATLAB always returns the first solution counter-clockwise from the positive real axis, i.e. 2*exp(i*pi/3) or: 1 + 1.732i. Armed with this
knowledge, you can compute all or some particular root. For instance, if you want the negative real cube root, simply take the cube root of the
absolute value of the number, and negate it.
For a different wording and more information, see the related [MathWorks Tech Solution http://www.mathworks.com/support/solutions/data/1-
15M1N.html?solution=1-15M1N ].
Finally, Joe Sababa suggests in msgid:<[email protected]> a method to find all the roots at once:
P=[1 0 0 27];
roots(P)
x = nthroot(-8, 3)
x=
https://matlab.fandom.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F 31/40
25.04.2019 FAQ | MATLAB Wiki | FANDOM powered by Wikia
-2
The easiest way to find the spectrum of irregularly sampled data is to resample it to uniform samples. You can do this with MATLAB's interp1
function. The accuracy of your spectrum will depend on the accuracy of the interpolation. You will want to experiment with several of the
interpolation methods that are available in interp1. I believe that for interpolation with a limited window (i.e. interpolating a sample value from
N nearest neighbors), the Lagrange interpolation is optimal, but Lagrange is not one of the choices in interp1.
If interpolation doesn't work there are other schemes available. The Lomb-Scargle periodogram is often mentioned in relation to this question
and may be more appropriate if your data has very uneven spacing (e.g. very large or very small spacings). I know that this algorithm is listed
in Numerical Recipes, but I don't have a good on-line reference (with MATLAB code) to point you to.
In general, the problem is that the spacing between points determines the "importance" of a particular point. For example, if several points are
very close together, then small amounts of noise on those measurements will tend to have a greater effect on inferring the slope of the function
(and with it, the high frequency energy) than the same amounts of noise on measurements that are further apart.
In recent MATLAB versions (>= 6.5), this is actually not such a bad idea, as the accelerator will do a good job on a loop like this.
This is a fine solution, except that having to replicate vector uses memory unnecessarily, and is less cache-efficient. The accelerated single
loop may run faster in some cases, dujjkke to the better cache-usage.
In newer versions of MATLAB you can use bsxfun. BSX stands for Binary Singleton eXpansion. It accomplishes the same thing without the
memory footprint of repmat and is more compact than a for-loop.
output = bsxfun(@times,matrix,vector);
If A is MxN and B is Nx1 or 1xN, then A*sparse(1:N, 1:N, B) multiplies the columns of A by the elements of B. Similarly, sparse(1:M, 1:M, B)*A
multiplies the rows of A by the corresponding elements of B. For division, use 1./B in place of B as the argument to sparse.
This solution requires the conversion of the full vector to a sparse format, but the actual computation will be very fast. The fastest solution
depends on the sizes involved, so try them all!
If you have a set of (x,y) points that define a curve, and you want to find discontinuities in the slope, or "kinks" in the curve, you can check the
radius of curvature between 3 points along the curve. Please see this reference: Posting by Roger Stafford in the Answers forum
https://matlab.fandom.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F 32/40
25.04.2019 FAQ | MATLAB Wiki | FANDOM powered by Wikia
Back to top
Follow the instructions in technical support solution document. Follow this link: Deactivate MATLAB . It has instructions for both when you
have MATLAB still installed and still have the computer, and for when MATLAB has been uninstalled or you do not have the computer.
When MATLAB automatically activates, it registers the volume serial number and the MAC addresses of the machine to your license.
If you cannot access the computer you want to deactivate MATLAB on, you can deactivate the "old computer" that MATLAB registered to by
deactivating manually from your MathWorks License Center, since technically it is no longer accessible and can't be deactivated from that
specific machine with the specific MAC address.
During installation, the installer crashes, hangs, gives an error message, does not authenticate your
license, or otherwise does finish installing correctly. Edit
There are so many ways that you could have problems during installation and volunteers in the Answers forum can't possibly know the
solutions to all of them or even some of them. Asking in the Answers forum or the Newsgroup will only delay you from getting the issue
solved. The Mathworks gives free support for all installation problems, even by telephone and even for the Student Edition. This is the
fastest way to solve your installation problem: call The Mathworks at (508)647-7000. Although the Student Edition normally does not get
technical support, it does get support for installation issues.
After installation, MATLAB crashes or gives an error message when I try to run MATLAB. Edit
The causes for crashes are many and complex, and complicated to figure out, especially if you don't have access to the source code. This is
why it's best for The Mathworks to work on your crash issue. Call The Mathworks at (508)647-7000 and explain to them the circumstances for
your crash. The Mathworks gives free support for all installation problems, even by telephone and even for Student, Home, or Trial versions.
Note, this is just if MATLAB itself crashes, not if it's your m-file throwing an error (red text in the command window) or a DLL or mex file causing
the crash.
How do I release or return a toolbox license key back to the pool of available keys without restarting the
license manager or exiting MATLAB? Edit
Start MATLAB using the command matlab -nodesktop. A related switch is -nojvm, which starts MATLAB without the Java Virtual Machine,
making it take much less memory. However many of the editor and browser features will not work.
This is likely to be a problem only under Windows, where MATLAB must poll for Ctrl-C events. If it is deep within matrix computation, it will
simply not respond. If this occurs inside a loop construct, you can force MATLAB to poll more often by inserting drawnow or pause (0) into the
loop. This will also update your figures and make GUIs more responsive.
https://matlab.fandom.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F 33/40
25.04.2019 FAQ | MATLAB Wiki | FANDOM powered by Wikia
Back to top
To set an image to some contant value either inside or outside some defined regions, you first need to make a binary image. See this page for
demo code: Mask an image.
To find objects or regions in a certain color range, please refer to: Image Analyst's Color Segmentation Demos
To see how to extract individual frames from a video file and save them to disk, see this demo: Extract Frames From Movie. The demo also
shows how to do image processing and analysis on the individual frames and displays the results as a function of frame number.
To see how to take individual frames from image files on disk and create and play a movie from them, see the bottom part of this demo: Extract
Frames From Movie
How do I measure a distance or area in real world units instead of in pixels? Edit
You need to measure an object of known length so that you can get a spatial calibration factor. Put a ruler, a tape measure, a standard sheet
of paper, or anything else of known length into the scene. Then snap a picture of it and use imdistline() to find the length in pixels. For
example, let's say you laid down a sheet of A4 paper and measured it's distance as 2100 pixels. Knowing that it is 21 cm long, you get a
spatial calibration factor of 21/2100, which you multiply your distances in pixels by to get the distance in cm. Multiply by that calibration factor
squared to convert the area in pixels to the area in square centimeters.
calibrationFactor = 21/2200;
distanceInCm = distanceInPixels * calibrationFactor;
areaInSquareCm = areaInPixels * calibrationFactor ^ 2;
Back to top
To generate a standalone executable that you can execute outside MATLAB or on a computer without MATLAB, you will need to use the
MATLAB Compiler.
http://www.mathworks.com/products/compiler/
The section titled "Deployment Process" in the documentation for this product gives instructions on what steps you need to perform to run the
executable on a machine that does not have MATLAB installed.
My standalone executable won't run on the target computer. What can I try? Edit
https://matlab.fandom.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F 34/40
25.04.2019 FAQ | MATLAB Wiki | FANDOM powered by Wikia
If you've deployed (installed) your standalone executable application on the end-user's computer and it won't run, there are various things you
can try to fix the situation:
1) Make sure that you have installed the MCR (MATLAB Compiler Runtime) on the target computer that is the same version that you have
on the system that compiled the program. A different version will not work. For example, a program compiled with version 8.1 will not work
on a computer that does not have that exact 8.1 version installed on it. Earlier or later versions will not work. If this is the problem, then you
should see an error message notifying you of this in the console window that appears as the application tries to launch. You can have multiple,
even older, versions of MCR on the target computer without any conflict, but you must at least have the same version it was compiled with.
2) Run the Dependency Report to see what functions and files are called. On MATLAB since R2013b, click the down arrow on the title bar of
the "Current Folder" panel and select "Reports/Dependency Report". On older versions, use the "Tools/Show dependency report" pull down
menu item. See what files are called. Pay particular attention to those that are labeled "other."
3) Try fdep in the File Exchange. It's like a souped up version of the dependency report tool that's built into MATLAB. Look at all the modules
that are listed. It sometimes finds modules that the Dependency Report tool doesn't.
4) Run DependencyWalker on the executable and look for any missing components. This must be run on the target computer, not the
computer that compiled it, unless it won't run on that computer either.
5) Make sure any ancillary files that are needed at run time are shipped with the installation package. This includes DLL's, OCX's,
initialization/configuration/mat files, sample data files, documentation, Excel templates, etc.
6) Call the Mathworks and let them figure it out. Sometimes the mistake is on their side. For example, one time they figured out that there was
something missing in the image processing toolbox or the compiler so that compiled versions didn't run. They made a patch for it.
7) Also be aware that the MCR installer may claim that it will install the Microsoft Visual C redistributable. However that never seems to work
for me and Dependence Walker will show that it can't find msvcr80.dll and msvcr90.dll. In this case, go to the Microsoft web site and
download and install the redistributable yourself. You can also try here: Latest Supported Visual C++ Downloads . After doing that it seems to
work most of the time. Sometimes the installer just quits after installing the Visual C++ redistributable and never installs the actual MATLAB run
time library software. Some have found that rebooting after that first installation where it quits will allow you to rerun the MCRInstaller.exe and it
will then finish.
8) Check your startup.m code for commands that should not be run in a deployed executable. When your compiled app runs, it also executes
the startup.m file that you had when you compiled the code. So whatever code you had in your startup.m file will also get run on the target
computer. It is not obvious to people that their startup.m code gets run before the actual code that they wanted to run gets run, but it does. So
make sure that everything that's in there makes sense when run on the target computer. For example if your startup.m code changes directory
to a particular directory, or reads a specific file, and that file does not exist on the target computer, it will throw an error or warning. You can
have code in your startup.m file that runs only on your computer while in development mode, and other code that is run only in
deployed code on the target computer if you check the isdeployed variable. It is a built-in variable that is true if the code is compiled and
false if it's source code running in the normal MATLAB development environment. For example:
if isdeployed
% Code that gets run only by compiled applications
uiwait(msgbox('This is a compiled application'));
else
% Code that gets run only in development environment
uiwait(helpdlg('You are in development mode.'));
% For example, set current folder to your main working code folder.
cd('C:/SomeFolderOfYours');
end
9) Have the startup code for your application print out ctfroot to the console window to see where the executable actually unpacks the
executable to.
https://matlab.fandom.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F 35/40
25.04.2019 FAQ | MATLAB Wiki | FANDOM powered by Wikia
If you refer to subfolders in your application, they will actually be under this folder, which is usually in a temporary, hidden folder (in Windows,
it's under C:\Documents and Settings\username\Local Settings\Temp) and not where you installed your executable.
10) If your application tries to call a DLL with a different number of bits, you will have trouble - it may not launch. For example, you cannot
compile a 64 bit m-file that tries to call a 32 bit DLL. This is a common problem with users trying to call old DLLs from third party
manufacturers (e.g. manufacturers of cameras or other peripherals). If you're calling a 32 bit DLL, you MUST use the 32 bit version of
MATLAB to compile it, and install the 32 bit version of the MCR from the matching MATLAB Release on the target computer. The target
computer can be either a 32 bit computer or a 64 bit computer. You can install both the 32 bit and 64 bit versions of MATLAB on your
computer and they will live happily together with no conflicts.
11) If you're running the executable by double-clicking the icon in Windows Explorer, and it encounters an unhandled error (an error that you
don't catch in a try/catch block), then the application may just exit before you ever see any error messages. To make sure you see the error
messages before they vanish, run the program from a console window (an MS-DOS command prompt). From the start menu, type cmd in the
run box to get a console window. Then use cd to navigate to the folder where you have your executable. Then type in the name of the
executable. When it runs, any errors will go to the console window but the console window will not close and you'll have the ability to see the
errors. You might also be able to do this from the MATLAB command window prompt if you type an exclamation point (bang) followed by the
name of the executable.
12) Some Simulink blocks implement portions of their functionality using shared libraries (DLL files on Windows). Additionally, in some cases
EXEs generated from models using these blocks are dependent on these shared libraries as well. In this case it seems like the Simulink UDP
Send block has some of these dependencies.
The PACKNGO function can be used to locate the needed shared libraries. Go into your modelname_target_rtw folder and load the 'buildInfo'
object into the workspace from the "buildInfo.mat" file. Then you can execute the command
>> packNGo(buildInfo);
This will create compressed folder in the same directory containing the generated EXE. This compressed folder should contain any DLLs that
the generated EXE is dependent on. When running the EXE outside of the MATLAB/Simulink environment these DLLs need to be on the
system path or within the same directory as the EXE. This information came from the Answers forum
The folder where you installed your compiled executable is not the actual folder that your application runs out of. For example, it's not really in
C:\Program Files\MyApp (or wherever), it's really in some secret hidden folder - for example, in
C:\Users\MyUserName\AppData\Local\Temp\MyUserName\mcrCache7.16\MyApp\. So if you put any files in the folder where you think the exe
is (C:\Program Files\MyApp), it won't find them because the exe is not really there. The exe in that folder is actually a self-extracting archive
that unzips the real executable to the secret folder. So your file would have to be located there in that secret folder for your executable to see it
(that is, if you neglected to include the full path and just assume it would find your file in the current working directory, which is bad practice).
The ways around this are to ship the files with the -a option of mcc, or include it in deploytool if you're using deploytool to create your
executable. Or better yet, specify the full path of any files you use and make sure they're there. Use fullfile() to create full file names, and
exist(filename, 'file') to check for them ebfore you use them. Alert the user with uiwait(warndlg(yourWarningMessage)) if the file does not
exist. Create yourWarningMessage with sprintf() explaining the missing file to your user.
If you don't want your exe to unpack the archive to a secret hidden folder, then (in Windows) you can set the MCR_CACHE_ROOT
environment variable to "." (dot). Then it will unpack a bunch of stuff (subfolders, etc.) to the current folder (where at least you can see it and
know it's there).
How can I suppress the black console window (DOS command window) when I run my standalone
executable? Edit
https://matlab.fandom.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F 36/40
25.04.2019 FAQ | MATLAB Wiki | FANDOM powered by Wikia
"Suppress appearance of the MS-DOS command window when generating a standalone application. Use -e in place of the -m option. This
option is available for Windows only. Use with -R option to generate error logging as such:
or:
You can also suppress the MS-DOS command window when using deploytool by creating a Windows Standalone Application."
Back to top
First of all, you can find out what toolboxes are installed on your computer by issuing the "ver" command in the MATLAB command window. To
find out what toolboxes are required by any specific m-file, you can:
Select "Tools->Show Dependency Report" from the main MATLAB pulldown menu. To get the Tools menu to show up, you must have
focus in the Editor window (not the Command window). In other words, you must have clicked last in the Editor window to make it the
active window.
You can use "fdep " from the File Exchange. This sometimes finds dependent files that the dependency report doesn't find.
Where can I find the release notes for all the versions? Edit
The Release Notes tell you what new features were introduced, or what bugs were fixed, in versions since the year 2004. Other than the
direct link just given, here's a way to navigate to the release notes for current and older versions of MATLAB.
Note that release notes are available there for releases back to R14 in 2004. The release notes summarize new features, compatibility
considerations, and fixed and known bugs.
Similarly, you can find release notes for the Image Processing Toolbox going back to version 2.2.2 in 2000.
http://blogs.mathworks.com/
http://www.cb.uu.se/~cris/blog/
http://imageprocessingblog.com/
https://matlab.fandom.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F 37/40
25.04.2019 FAQ | MATLAB Wiki | FANDOM powered by Wikia
http://www.mathworks.com/matlabcentral/fileexchange/ .
First, check the File Exchange for free, user-contributed code submissions:
http://www.mathworks.com/matlabcentral/fileexchange/
http://www.mathworks.com/products/product_listing/index.html
Finally, you can look to other specific user-written toolboxes, such as:
Styled Text Toolbox: written by Doug Schwarz, this toolbox allows extremely flexible formatting of text strings, including symbols, math
formulae, etc. Its TeX interpreter is much more complete than the builtin MATLAB interpreter.
http://www.frontiernet.net/~dmschwarz/stextfun/index.html
MATLAB and LaTeX: Arno Linnemann has written an M-file to simplify the inclusion of MATLAB graphics into LaTeX documents, along with a
nice document of tips and tricks. http://www.uni-kassel.de/~linne/matlab/WelcomeEng.html
Genetic Algorithm Optimization Toolbox: GAOT implements simulated evolution in the MATLAB environment. Written by the Meta-Heuristic
Research and Applications Group at the North Carolina State University Department of Industrial Engineering:
http://www.ise.ncsu.edu/mirage/GAToolBox/gaot/ (WARNING: as of Nov. 10, 2010 that link is defunct).
One choice is the image processing book written by Steve Eddins. Steve Eddins is a software development manager in the MATLAB and
image processing areas at MathWorks . Steve coauthored Digital Image Processing Using MATLAB . He writes about image processing
concepts, algorithm implementations, and MATLAB, both in the book and on his Steve on Image Processing blog .
Where are the official Mathworks Tech Notes for MATLAB and Simulink? Edit
There aren't many of them but they can be found off the Mathworks Support web page here: Tech Notes and How-to Guides
Where are the official Mathworks Bug Reports for MATLAB and Simulink? Edit
Bug Reports
First of all, you can find out what toolboxes are installed on your computer by issuing the "ver" command in the MATLAB command window. To
find out what toolboxes are required by any specific m-file, you can:
Select "Tools->Show Dependency Report" from the main MATLAB pulldown menu. To get the Tools menu to show up, you must have
focus in the Editor window (not the Command window). In other words, you must have clicked last in the Editor window to make it the
https://matlab.fandom.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F 38/40
25.04.2019 FAQ | MATLAB Wiki | FANDOM powered by Wikia
active window.
You can use "fdep " from the File Exchange. This sometimes finds dependent files that the dependency report doesn't find.
You can use "depfun " from the File Exchange.
Simulink Edit
Back to top
What are best practices for modeling communications systems with Simulink? Edit
Miscellaneous Edit
Back to top
Can you program up the algorithm in this article for me and explain it to me? Edit
You may find that the algorithm in the scientific article is interesting and may be useful to you, but you may not understand it, or you may not
know how to program it up in MATLAB, so you ask if anyone in the Answers forum or newsgroup will program it up for you. Unfortunately, the
newsgroup and Answers volunteers generally don't have the time to read some scientific article, understand it themselves, then explain it to
you, code it up for you, and finally hand over the code to you.There are just too many papers - we can't even find the time to read the papers
that we find interesting, Much less other ones. If you really need it done, here are some options for you:
On the PC, you can use the /r flag to start your m-file immediately upon starting MATLAB. Using this in combination with the Windows Task
Manager, you can set a MATLAB job to run when you want it to run, even if you are not at the computer. Please note that you will need to exit
MATLAB at the end of the m-file. Alternately, if you only want a single file to run, you can name it startup.m and place it in your MATLAB
directory.
For UNIX, you can use the at or cron commands to execute a script which runs MATLAB. In the script, use this general syntax:
If you want to record output, either use the SAVE command inside your m-file or redirect the output of the MATLAB session to a file using this
syntax:
This syntax only works for scripts. If you need to run a function, create a wrapper script that calls the function.
Back to top
https://matlab.fandom.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F 39/40
25.04.2019 FAQ | MATLAB Wiki | FANDOM powered by Wikia
https://matlab.fandom.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F 40/40