What is the default variable in MATLAB? - matlab

What is the default variable in MATLAB if you do not assign one to an equation? I was thinking it was maybe just x?

The answer is ans, this is also shown in the workspace which contains the variables. Consider this example input in the command line:
3*4
ans =
12
Just be sure never to use ans as an actual variable, as that'll mess up your code good.

Related

Making symbolic functions without hard-coding in MATLAB

I want to make symbolic functions theta1(t), theta2(t), theta3(t),...,thetaN(t) where N is some parameter I can define in MATLAB. I know that I can use something like sym('theta',[1 N]) to get [theta1, theta2, theta3,..., thetaN]. However, how can I do the same thing with theta being a function of t? The way to hard-code it would be like syms theta1(t) theta2(t) theta3(t) ... thetaN(t), but I want to make this general.
I do not want to directly use the sym command here because "support of character vectors that are not valid variable names and do not define a number will be removed in a future release", meaning something like sym('theta1(t)') would not be valid in future releases.
Any suggestions?
Figured part of it out. I could do something like the following
for i = 1:N
syms(strcat('theta',num2str(i),'(t)'))
end
However, if I want to assign a variable that contains all the symbolic expressions I'm still stuck. If I try
for i = 1:N
my_array(i) = syms(strcat('theta',num2str(i),'(t)'))
end
I get Error using syms (line 133). Using input and output arguments simultaneously is not supported. It works if I use sym instead of syms, but this leads to the warning I mentioned in my original post.

How to get back clear command if we assign a value to it (MATLAB)?

In MATLAB, built-in functions can be assigned a variable value like plot = 5. From then on the function plot() will not be available. plot(x), for a variable x, will give a compilation error. To get back the function we just have to delete the variable plot by using clear plot.
clear is the command usually used to get back the built-in function.
My question is if we assign a value (scalar or matrix) to the function clear, how do we get back the function clear?
clear clear won't obviously work. I couldn't think of a way to get the function back, other than closing and restarting MATLAB.
As already mentioned its very bad practice to shadow matlab functions like clear - however in this case you can use builtin to clear your clear variable:
>> clear = 1
>> whos
Name Size Bytes Class Attributes
clear 1x1 8 double
>> builtin clear
>> whos
>>
To only clear clear use:
builtin clear clear
No, clear is the function to clear, delete, a variable. if you clear the variable that was "shadowing" your function, then the function can be found again. if you "shadow" the clear function, then you have no way of clearing anything anymore!
It is very bad practice to name things plot clear surf conv or any other MATLAB function, because of shadowing problems, and you clearly discovered why.
Never shadow a variable in MATLAB!
#Jucobs gives a very nice hint in the comments. Use exist. It will return a different value if the thing is a variable (1) or a function (2,5)

Matlab: how to manupulate expression

I am currently having an equation that contains 4~5 variables, and I am unfamiliar with matlab. I want to see how a variable change with the other variable fixed (might assign them some value). Maying graphing them out will help. I found it cumbersome to work in the main GUI in matlab, this is mainly after I type in my equation, I didn't know if the expression is what I want because of missing brackets, etc.
Recently I found mudpad, it is better because it will show you the expression in pretty form. I am wondering is there any other tool-box that is more suitable for my intention?
Update:
Ps=1;
Pr=1;
Pd=1;
g=0.25;
d1=1;
d2=1;
n=0.18;
Yr = #(x)(Ps)/(d1*((Pd*g^2)/d2 + (n*(x- 2))/(x- 1)));
Yr_=ezplot(Yr,0,1);
There is actually 4 more equations to plot and I am only posting one of them.

How to dump variables as MATLAB source code?

Is there a way to dump a MATLAB variable as the source code for the corresponding literal initializer? IOW, I'm looking for some function x such that, for example:
>> A = zeros(2);
>> x(A)
ans =
[0 0; 0 0]
>> class(x(A))
ans =
char
Is there such a function, or an easy way to achieve the same effect? (I realize that literal initializers may not exist for some MATLAB items; for such items the problem is intrinsically unsolvable.)
I am aware of the fact that MATLAB offers many ways to save data to files, but none of the ways I've found produce MATLAB source code, which is what I'm after.
For simple numeric values (and also char arrays), the mat2str function does what you're looking for.
For example, (from the MATLAB documentation):
Consider the matrix
x = [3.85 2.91; 7.74 8.99]
x =
3.8500 2.9100
7.7400 8.9900
The statement
A = mat2str(x)
produces
A =
[3.85 2.91;7.74 8.99]
where A is a string of 21 characters, including the square brackets, spaces, and a semicolon.
Further, passing the string 'class' as the second argument ensure that the answer will be case to the correct numeric type.
See the MATLAB documentation for mat2str, or run
doc mat2str
in MATLAB, for more information.
I know you are looking for a function that can do this, rather than an interactive procedure, but for anyone else who wants to do this manually...
The MATLAB variable editor/viewer has built-in code generation functionality. Open the variable in the editor, click the save icon, and choose MATLAB Script (*.m) file type (default is .mat):
The resulting MatrixCode.m:
% -------------------------------------------------------------------
% Generated by MATLAB on 3-Mar-2014 17:35:49
% MATLAB version: 8.3.0.73043 (R2014a)
% -------------------------------------------------------------------
M = ...
[16 2 3 13;
5 11 10 8;
9 7 6 12;
4 14 15 1];
Maybe someone with Java and reverse engineering skills can figure out how to call this GUI operation from the command line.
As Sam Roberts commented, matlab.io.saveVariablesToScript is now the ultimate method to convert any datatype to a script. This method was introduced in 2014a and works for struct, cell, and all primitive datatypes.
chappjc's method is also correct, but it uses a MATLAB GUI frontend to the saveVariablesToScript method.

How to suppress "ans" line from MATLAB output?

EDIT: The question above concerns strictly to the output that MATLAB
produces by default in an interactive session, as illustrated by
the given example. I have no interest in ways to modify the
appearance of the output produced by scripts, functions, methods, etc.
Also, the motivation for this is to keep more of my laptop's extremely scarce "screen real estate" for actually informative output.
Even with format compact, MATLAB's output includes an ans = line in addition to the line(s) that show the output proper. E.g.
>> format compact
>> date
ans =
04-Sep-2012
>>
Is there any way to suppress the ans = line, so that, e.g., the last interaction above looks like this?:
>> date
04-Sep-2012
>>
...or at least like this?:
>> date
ans = 04-Sep-2012
>>
This is a bit tricky and might have other consequences, but if you are mainly displaying data of a certain type (double, char, etc.) you can overwrite the corresponding built-in display method.
For example,
>> % Before overwriting the #char/display
>> date
ans =
04-Sep-2012
Now create an #char directory in a location that is on MATLAB's path and add a method called display.m:
function display(x)
disp(x)
end
Then you would have
>> % After overwriting the #char/display
>> date
04-Sep-2012
ans is merely the name of the variable in which MATLAB stores its "last" answer. The easiest way to "avoid it" would be simply to assign the result to some other variable and print it out, in a clear fashion, with fprintf or sprintf.
disp(sprintf('<Your format>',<variables>) does that job.
If you were creating a new class for your work, then you would have some options. The "pretty" display on the command window is created by the display method which all classes inherit/ Generally, display prints the name of the variable, the " = \n" and then calls the disp method, which you are probably familiar with. (help display for details).
However, for standard Matlab arrays, these methods are all built-ins, and I don't believe that they can be modified.
So, while you have endless options if you are building tools (sprintf, fprintf, disp, various tools hacking the underlying java display), I do not know of any way to setup a "more compact" display style as the default during quick, interactive work.