How can I pass variables into my title with Latex Interpretation? - matlab

I would like some parameter values to appear where the white box is in my picture (subplot(3,2,1)). It would be ideal if the interpretation in title() could be latex, like the others.
I have tried the following:
title( sprintf( ' $\delta$ = %s $\rho$ = %s $\xi$ = %s m = %s', delta, rho, xi, m), 'Interpreter', 'latex')
But I get the following error:
Warning: Control Character '\d' is not valid. See 'doc sprintf' for control characters valid in the format string.
> In SIXY_sol (line 21)
Warning: Error updating Text.
String must have valid interpreter syntax:
$
How can I fix this?

This is not a problem with using the LaTeX interpreter but rather with the way you have used sprintf. In the docs for sprintf, scroll down to the section Text Before or After Formatting Operators. Here you will see that the \ character is used to begin an escape sequence so that you can do things like \n for a new line. You actually want \ to appear in your string so you need to escape the escape character. From that table in the docs you need to replace all of your \ with \\.
Try:
title( sprintf( ' $\\delta$ = %s $\\rho$ = %s $\\xi$ = %s m = %s', delta, rho, xi, m), 'Interpreter', 'latex')

Related

character shows up as 'y' instead of a space '

In matlab i'm coding a Ceaser Cipher, but the space shows up as a 'y' character.
How can I replace that with a space
case 4
disp('Breaking Ceaser Cipher')
cs = menu('Please Enter your Choice','Encryption','Decryption');
if cs==1
c = input('Enter the message: ','s');
sh = str2double(input('Enter shift: ','s'));
c=upper(c);
lc=length(c);
for i=1:lc
p(i)=int16(c(i))-65+sh;
end
p=mod(p,26)+97;
p=char(p);
disp( p)
end
end
output example:
Breaking Ceaser Cipher
Enter the message:
my name is jeff
Enter shift:
5
rdysfrjynxyojkk
Here we see that the encryption is correct, but the space is being replaced by 'y'. It does not replace the character 'y' when used as an input, the space bar somehow comes out as a 'y'.
I'v also tried using p2 = regexprep(c, 'y', ' ') in order to replace the 'y' string with space.Also looked into isspace function. No luck
You are halfway there:
spaces=isspace(c)
% make array of spaces
out=blanks(size(c));
% get array without spaces
c=c(~spaces);
% do stuff to c, without spaces.
p=mod(p,26)+97;
p=char(p);
% Fill p in corresponding locations
out(~spaces)=p;

Printing a warning message over multiple lines

I am trying to print a warning message that is a little long and includes 2 variable calls. Here's my code:
warning( 'MATLAB:questionable_argument', ...
'the arguments dt (%d) and h (%d) are sub-optimal. Consider increasing nt or decreasing nx.', ...
dt, h )
Obviously, the line of text extends to the right when viewing the MATLAB code. How can I break it so it wraps nicely? I've tried multiple things but keep getting syntax errors.
As suggested in comments, just insert a \n where you want to break the line. You can also use a variable for the text, to make it easy to read also within the code:
txt = sprintf(['the arguments dt (%d) and h (%d) are sub-optimal.\n'...
'Consider increasing nt or decreasing nx.'],dt,h);
warning( 'MATLAB:questionable_argument',txt)
If you just embed escape characters such as \n in a warning string, it will not work:
warning('Hi there.\nPlease do not do that.')
will just print out:
Warning: hi there.\nPlease do not do that
However, if you pre-format the text using sprintf , then all the escape characters will work. For instance:
warnText = sprintf('Hi there.\nPlease do not do that.');
warning(warnText)
Produces what you want:
Warning: Hi there.
Please do not do that.
A more simple version than EBH had provided is as shown:
str1 = 'text 1';
str2 = 'text 2';
str3 = 'etc.';
str = sprintf('\n%s \n%s \n%s \n',str1,str2,str3);
warning(str)

matlab text read and write %s character (without escaping)

Dear All (with many thanks in advance),
The following script has trouble reading (and therefore writing) the %s character in the file 'master.py'.
I get that matlab thinks the %s is an escape character, so perhaps an option is to modify the terminator, but I have found this difficult.
(EDIT: Forgot to mention the file master.py is not in my control, so I can't modify the file to %%s for example).
%matlab script
%===============
fileID = fopen('script.py','w');
yMax=5;
fprintf(fileID,'yOverallDim = %d\n', -1*yMax);
%READ IN "master.py" for rest of script
fileID2 = fopen('master.py','r');
currentLine = fgets(fileID2);
while ischar(currentLine)
fprintf(fileID,currentLine);
currentLine = fgets(fileID2);
end
fclose(fileID);
fclose(fileID2);
The file 'master.py' looks like this (and the problem is on line 6 'setName ="Set-%s"%(i+1)':
i=0
for yPos in range (0,yOverallDim,yVoxelSize):
yCoordinate=yPos+(yVoxelSize/2) #
for xPos in range (0,xOverallDim,xVoxelSize):
xCoordinate=xPos+(xVoxelSize/2)
setName ="Set-%s"%(i+1)
p = mdb.models['Model-1'].parts['Part-1']
# p = mdb.models['Model-1'].parts['Part-2']
c = p.cells
cells = c.findAt(((xCoordinate, yCoordinate, 10.0), ))
region = p.Set(cells=cells, name=setName)
p.SectionAssignment(region=region, sectionName='Section-1', offset=0.0, offsetType=MIDDLE_SURFACE, offsetField='', thicknessAssignment=FROM_SECTION)
i+=1
In the documentation of fprintf you'll find this:
fprintf(fileID,formatSpec,A1,...,An) applies the formatSpec to all elements of arrays A1,...An in column order, and writes the data to a text file.
So in your function fprintf uses currentLine as format specification, resulting in an unexpected output for line 6. Correct application of fprintf by providing a formatSpec, fixes this issue and doesn't require any replace operations:
fprintf(fileID, '%s', currentLine);
Your script has no trouble reading the % characters correctly. The "problem" is with fprintf(). This function correctly interpretes the percent signs in the string as formatting characters. Therefore, I think you have to manually escape every single % character in your currentLine string:
currentLine = strrep(currentLine, '%', '%%');
At least, it worked when I checked it on your example data.
Thanks applesoup for identifying my fundamental oversight - the problem is in the fprintf - not in the file read
Thanks serial for enhancing the fprintf

MATLAB: Reading space separated float values from tex file

I am reading a text file using the textscan function of MATLAB. Problem here is that nothing is being read in value as the floating points are separated with three spaces and I am quite new in MATLAB programming to use some efficient syntax. My current code is given below:
Code:
values = textscan(input_file, '%f %f %f %f %f\n %*[^\n]');
The input file follows the following format:
File:
0.781844 952.962130 2251.430836 3412.734125 4456.016362
0.788094 983.834855 2228.432996 3196.415590 4378.885466
0.794344 967.653718 2200.798973 3119.844502 4374.097695
If the floating point values are # separated then the below statement works fine:
values = textscan(input_file, '%f#%f#%f#%f#%f\n %*[^\n]');
Is there any solution except for tokenization ?
You need to specify a delimiter, also you should activate the MultipleDelimsAsOne in order to treat the repeated space as a single delimiter:
value = textscan(input_file, '%f %f %f %f %f \n ','Delimiter',' ','MultipleDelimsAsOne',1);
If needed you can also specify several delimiters at the same time:
del = {';',' '};
If you don't have to use textscan, you could probably use importdata. There you can specify the delimiter as a parameter.
Documentation http://se.mathworks.com/help/matlab/ref/importdata.html
Code example
filename = 'myfile01.txt';
delimiterIn = ' ';
A = importdata(filename,delimiterIn);

Matlab: How to print " ' " character

I am trying to create the following string:
javaaddpath ('C:\MatlabUserLib\ParforProgMonv2')
However, I could only do the following
command = sprintf('%s ', varargin{1}, '(', varargin{2}, ')');
and that gives me:
javaaddpath ( C:\MatlabUserLib\ParforProgMonv2 )
UPDATE:
Based on Dan's suggestion, I used the following:
command = sprintf('%s', varargin{1}, '(', '''', varargin{2}, '''', ')')
Use two single quotation marks. See the docs for formatting strings, btw this concept is known as an escape character (to help you google such things in the future).
command = sprintf('%s ', varargin{1}, '(''', varargin{2}, ''')')
Although I think you might prefer
command = sprintf('%s (''%s'')', varargin{1}, varargin{2})
or if you have no other varargins (which I guess is very unlikely but anyway)
command = sprintf('%s (''%s'')', varargin{:})
There are a couple of ways around this. First you could declare your path as a string variable then pass the string to your command, eg,
path = 'my/path'
javaaddpath (path)
Or you can use special characters to insert things like a single quote or a new line character, so for a single quote,
EDIT: wrong display command as pointed out by Dan below
myString = '" Hi there! "'
disp(myString)