MATLAB Answers

How do I define a variable without assigning it a value?

3,070 views (last 30 days)
Ethan Sehrt
Ethan Sehrt on 16 Jun 2016
Commented: James Tursa on 12 Nov 2020
I just want to define a variable, f without assigning any values to it so I can use it in an equation.
  1 Comment
James Tursa
James Tursa on 16 Jun 2016
Do you mean symbolically? E.g.,
syms f
Or maybe you mean an anonymous function? E.g.
myfunction = @(f)f+3

Sign in to comment.

Answers (4)

Star Strider
Star Strider on 16 Jun 2016
You can also assign a variable as an empty matrix, cell, string (or any other empty value):
x = []; % Empty Scalar, Vector Or Matrix
y = {}; % Empty Cell
z = ''; % Empty String Or Character
  2 Comments
Walter Roberson
Walter Roberson on 22 Jan 2020
w = zeros(0,0, 'uint8'); %empty variable but uint8 datatype
or
w = uint8([]); %empty variable but uint8 data type

Sign in to comment.


Lucas Hilden
Lucas Hilden on 22 Jan 2020
have no clue
  2 Comments
James Tursa
James Tursa on 12 Nov 2020
MATLAB is a dynamically typed language. You create the variable by assigning it a value. You don't need to create or type it ahead of time like you do in compiled languages such as C/C++.

Sign in to comment.



Walter Roberson
Walter Roberson on 22 Jan 2020
You can use James' File Exchange contribution https://www.mathworks.com/matlabcentral/fileexchange/31362-uninit-create-an-uninitialized-variable-like-zeros-but-faster to create an uninitialized array. The initial contents of the variable will be whatever happened to be in memory,

Community Treasure Hunt

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

Start Hunting!