Using Greek alphabet (or any non-ANSI alphabet) as variable names in MATLAB - matlab

Is it possible using Greek alphabet to represent variables in MATLAB?
For example, I'd like to use the Greek character epsilon as a variable in MATLAB. I tried to insert \epsilon but I received an error.

It is not possible.
I refer to the following part of Matlab documentation:
Valid Names
A valid variable name starts with a letter, followed by letters,
digits, or underscores. MATLAB is case sensitive, so A and a are not
the same variable. The maximum length of a variable name is the value
that the namelengthmax command returns.
Letter is defined as ANSI character between a-z and A-Z.
For example, the following hebrew letter Aleph returns false (in Matlab R2018a returns true):
isletter('א')
By the way, you can always check whether your variable name is fine by using genvarname.
genvarname('א')
ans =
x0x1A

While Andrey's answer is true for variable names it's a different story for figures.
title('\epsilon\omega') will actually work and generate an epsilon and an omega as title (although the matlab font replaces them with different symbols). If you export the figure as an eps or pdf file you will see that the title really is epsilon omega. Actually any LaTeX control sequence will work!
Same is true for all the figure text objects such as legends and axis labels.

Related

How to create a Matlab symbol composition ∆t?

I'd like to create a single symbol which is contains a delta "∆" and a t.
For visualisation purposes i use Matlab Live Script.
Combining several latin letters is easy done by:
sym('dt')
But these either result in an exception or don't do a conversion:
sym('Delta t')
sym('Deltat')
The workaround to multiply two symbols does not work in all cases:
sym('Delta')*sym('t')
(sym('Delta')*sym('t'))^2
However, if i square the symbol, i'd like to have this behavior:
sym('dt')^2
But it should contain the delta symbol:
Is this what you want?
title('\Deltat')
If you want, you can also do
title('\Deltat^{2}')
The answer is no, you cant use non-ASCII for variables in MATLAB. This means no \Delta, no \epsilon, or any other non-standard chars.
You can generate the LaTeX representation of the results, and then print them either in text in MATLAB or in your favourite LaTeX editor with the function latex

How to rename table variables in Matlab?

I would like to create a table from a matrix of spectra data, using a specific list of variables (column, which here correspond to 1000 spectral wavelenght values) instead of putting the name manually.
To do so, i use the array2table function, and matlab documentation shows that names for rows and label of variables must be put as cell arrays, (and not matrix). So I need to first convert my x-axis (spectral wavelenght) to a cell array. I use the following:
C = num2cell(xaxis); % to convert into a cell array (each cell contain 1 value)
isvarname C % to check that the variable is valid as a cell array
T = array2table(R,'RowNames', concentration,'VariableNames',C);
Here: R is the matrix, concentration is a Cell array 1x500, xaxis is the wavelenght of spectral data 1x1000 (which is ranged from 600 to 1800, approx. there is no null value).
Unfortunately, I got the following error:
"Error using array2table (line 62)
The VariableNames property must be a cell array, with each element containing one nonempty string."
which means I can input properly the columns (variable) names (while row names, however, works fine).
Note: i tried the
T.Properties.VariableNames = c but it is not working either.
I checked the other post on table name value but its not helping,
Any thought about this?
Thank you very much.
Maybe you mistyped your question's code, but it seems that you are using the array xaxis instead of C. Anyway, I suggest you try casting the cells content to strings this way
T = array2table(R,'RowNames', concentration,'VariableNames',cellfun(#(x)num2str(x),num2cell(xaxis),'uniformoutput',false));
Edit
Looking through the table docs it says that
Variable names, specified as the comma-separated pair consisting of 'VariableNames' and a cell array of character vectors that are nonempty and distinct. The number of character vectors must equal the number of variables. The variable names that you assign must be valid MATLAB® variable names. You can determine valid variable names using the function isvarname.
Furthermore, the isvarname function says that
A valid variable name begins with a letter and contains not more than namelengthmax characters. Valid variable names can include letters, digits, and underscores. MATLAB keywords are not valid variable names. To determine if the input is a MATLAB keyword, use the iskeyword function.
This means that you cannot use the xaxis values as variable names by themselves, you need to, at least, prepend a character and remove the decimal dot. You can do so with the following
T = array2table(R,'RowNames', concentration,...
'VariableNames',cellfun(#(x)['wavel_',regexprep(num2str(x),'.','_')],num2cell(xaxis),'uniformoutput',false));
This code will prepend 'wavel_' to the numerical string value. It will also replace the dots with underscores using the regexprep function. However, it seems like this is really unnecessary because the column names are not really informative. The array2table function's doc says that if you don't supply the 'variableNames' option it will do the following:
If valid MATLAB identifiers are not available for use as variable names, MATLAB uses a cell array of N character vectors of the form {'Var1' ... 'VarN'} where N is the number of variables.
Maybe using the default variable names is good enough.

MATLAB – Logarithm operator error

I'm writing a script in MATLAB that displays Before and After images of a given original image and an image gone through the logarithm operator point transformation. I've tried debugging the program to see what's wrong with it, but for some reason, it isn't running in MATLAB. I keep getting the error on the command line (logarithm-operator is the name of the script):
Here is the script:
a = imread('cells.png');
ad = im2double(a);
x = ad;
[r, c] = size(ad);
factor = 1;
for i = 1:r
for j = 1:c
x(i, j) = factor *log(1+ ad(i,j));
end
end
subplot(1,2,1);imshow(ad);title('Before');
subplot(1,2,2);imshow(x);title('After');
Matlab script or function names cannot contain a hyphen; only letters, numbers, or underscores are allowed, and must begin with a letter. The hyphen in your script's name confuses Matlab and leads it into thinking that logarithm is the name of the function/script it's supposed to be calling.
These are the same requirements as those for variable names. You can have a look at the documentation for isvarname:
A valid variable name is a character string of letters, digits, and
underscores, totaling not more than namelengthmax characters and
beginning with a letter.
You have to change the name of your script from logarithm-operator to logarithm_operator. Because the names of variables, scripts, functions, etc in matlab does not contain the symbol : hyphen -.

MATLAB num2str format

I'm storing variable values in MATLAB and putting one of the variable values as part of the file name . e.g. "Error=1e-003.mat", however different version of MATLAB gives me different format when I'm converting numbers to string using num2str command. The number 1e-3, MATLAB2011 gives me 1e-003 and MATLAB2012 gives me 1e-03.
This runs into trouble when I try to load a batch of files with a mix of those two format. Does anyone know a way to add a zero or delete a zero for the exponent so the names are consistent? Or any other ways to solve this problem?
Here's a fairly robust way to do it:
A = num2str(YOUR_NUMBER, '%0.0e');
if A(end-2)=='-' || A(end-2)=='+'
A = [A(1:end-2) '0' A(end-1:end)]; end
In words: convert the number, and check if the second-to-last character is either a '+' or a '-'. If this is so, add a '0'.
Specify a "Format string as the second argument like this:
>> disp(num2str(2920230,'%0.10e'))
2.9202300000e+006
here %0.10e means display at least 0 digits before decimal and exactly 10 digits after it in exponent form.

Function to generate Random letters

If there is any built-in function for getting random "letters"
like RANDOM(low,high) we used to generate the random number?
For my typing tutor I want to generate some sentences(which need not to be meaningful).
So I need to get some function to generate letters so that i can generate words from that.
(Change in logic also will be accepted. But I prefer this logic.)
Thank you
If you use the 'chr()' function, you'll be able to generate ASCII letters.
chr(RANDOM(65,90))
gives you a random upper-case letter between A and Z,
chr(RANDOM(97,122))
should give you a lowercase one.