Use a variable as a file name for pdflatex - matlab

I've used MATLAB and pdfLatex but now I'm trying to combine them. I've found that you can call pdflatex with a ! in MATLAB and that seems to work but I want the filename that get's latexed to be dynamic (i.e. a variable) Here is my code (MATLAB) functionally.
for loop starts here
filename = concatinate(these variables/text strings.tex)
open file
write (fprintf) this latex code to filename (this part works)
close file
!pdflatex filename
for loop ends here
If I type the exact file name (file.tex) after !pdflatex it works, but the filename changes and I need it to produce about 80 files. Is there a way to make pdflatex evaluate the variable so that I can create these files in the code instead of one by one?
Thanks. Gizm0
Update: Thanks to #Luis Mendo my program is now calling pdflatex and working on some of the files, but I am getting this error on many lines.
! Text line contains an invalid character.
Here is an example of one of the lines:
\noindent
When I copied and pasted the line above that is what showed up, but the line on the screen looks like this:
\noindent Good Instructor. \\
If I retype the line it works fine, but there is something weird about the way MATLAB is writing the line to the tex file that is making it invalid. The \noindent and \ are printed directly. "Good Instructor." comes from a variable.
I tried converting the tex file to a txt file and now that line looks like this:
\noindent G o o d i n s t r u c t o r . \\
Any ideas?

Use system instead of !. It does the same thing but lets you build the string dynamically:
system(['pdflatex ' filename])

Related

i would like to extract the pspictures from a tex file and put the in another file so they can processed into ps or pdf files really easily

I have a list of files .tex file that contain fragments in the tex that build ps pictures which can be slow to process.
There are multiple fragments across multiple files and the end delimiter is \end{pspicture}
% this is the beginning of the fragment
\begin{pspicture}(0,0)(23,5)
\rput{0}(0,3){\crdKs}
\rput(1,3){\crdtres}
\rput(5,3){\crdAh}
\rput(6,3){\crdKh}
\rput(7,3){\crdsixh}
\rput(8,3){\crdtreh}
\rput(12,3){\crdQd}
\rput(13,3){\crdeigd}
\rput(14,3){\crdsixd}
\rput(15,3){\crdfived}
\rput(16,3){\crdtwod}
\rput(20,3){\crdKc}
\rput(21,3){\crdfourc}
\end{pspicture}
I would like to extract the fragments.
I am not sure how to go about this? can awk do this or sed?
They seem to work line by line, rather than work on the whole fragment.
I am not really looking for a solution just a good candidate tool.
sed -En '/^\\begin\{pspicture\}.*$/,/^\\end\{pspicture\}.*$/p' file
Utilising sed with -E for regular expressions.
Use //,// to determine start and ending regular expressions and print all lines from the start to the end.

\n is not working while \t works as expected

I have a very long script with a lot of plot functions and related legend functions but these legend functions use 1, 2, 3 or 4 to locate legends on corners (as it was in older versions). However now I need to change them with name/value pairs. To do so, I wrote down a script to convert them. I want to copy code into text file and converted code will be on another text file so I can recopy it to my .m files. Here my code is;
fid=fopen('drawFigure.txt','r');
fid2=fopen('drawFigure2.txt','w');
codeLine=fgetl(fid);
while(codeLine ~= -1)
legAv=strfind(codeLine,'legend');
if (~isempty(legAv))
loc3=strfind(codeLine,'3');
loc2=strfind(codeLine,'2');
if(~isempty(loc3))
loc=loc3;
elseif(~isempty(loc2))
loc=loc2;
end
%here I check the next indice of codeLine to make sure I will change location factor
if(codeLine(loc+1)==')')
codeLine(loc)=[];
str1=codeLine(1:loc-1);
str2=codeLine(loc:length(codeLine));
mystr="'location','northeast'";
codeLine=strcat(str1,mystr,str2);
endif
endif
fprintf(fid2,'%s\n',codeLine);
codeLine=fgetl(fid);
endwhile
fclose(fid2);
fclose(fid);`
In this code, If I wrote \t, it would be working but \n is not working. Also, tried to get help from a software engineer friend but he couldn’t figure out the reason.
From your information, I am guessing that you are opening your file using Windows notepad. If this is the case, newlines (\n) are not shown unless preceeded by a cariage return (\r). This means that you should change:
fprintf(fid2,'%s\n',codeLine);
to
fprintf(fid2,'%s\r\n',codeLine);
or alternatively, use a basic text editor with more support such as Notepad++ for Windows.

How to use 'fprintf' to show the output in a txt file and save it instead of command window in Matlab?

I've been trying to make the output shown in the text file instead of the command window. Im blur right now as i already look at a lot of example but it always show the error on fprintf. I try to edit the code of fprintf(fid,'%s\n',word);%Write 'word' in text file (upper) in one of the Matlab example which is Automatically Detect and Recognize Text in Natural Images.
This is the link of the code.
https://www.mathworks.com/help/vision/examples/automatically-detect-and-recognize-text-in-natural-images.html?s_tid=srchtitle
Basically the above link display the output on the command window. But, i need it to be on the txt file.
Im really new to this, i want to know what code do i need to put, how and where should i put the fprintf to make the output shown on the text file and not on the command window.
Also, can i save the text file after that? do i to put any additional code?
I really need your help. Thank u in advance!
It seems you're looking for the fopen() method. It takes two parameters, the first being the name of the file you'd like to write to, and the second being the mode. If the file specified does not exist in the root directory, it will be created on execution.
fileID = fopen('exp.txt','w');
fprintf(fileID, fid,'%s\n', word);
fclose(fileID); % Make sure to always close the stream after finishing
More on fopen() here

Problems with Wget64

Attempting to write a Wget to get and save Vanguard pricing data. So far I have 2 statements that both work correctly from the Command Line when I paste the string. When I save the string as a bat file one works and the other gives an unexpected result.
The string that works correctly in both places is:
Wget64 --output-document=C:\Users\Default\downloads\VVA_Daily_Portfolio-%DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%.html "https://personal.vanguard.com/us/funds/annuities/variable"
The string that only works in the Command Line and not as a bat file is:
Wget64 --output-document=C:\Users\Default\downloads\VVA_Fund64_History-%DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%.html "https://personal.vanguard.com/us/funds/tools/pricehistorysearch?radio=1&results=get&FundType=VVAP&FundIntExt=INT&FundId=0064&fundName=0064&fundValue=0064&radiobutton2=1&beginDate=03%2F01%2F2017&endDate=12%2F31%2F2017&year=#res"
Can someone help me write the script so that the expected result is achieved. I suspect that the Vanguard website can tell the difference between a Command Line vs bat file query, or that there is something inherently different between the two methods of execution.
ANy assistance is appreciated. Dan
The cmd command parser behaves differently in command line and batch files. In this case, the main problem is the variable expansion. In command line when a variable does not contain a value (it is undefined), the variable read operation is not removed, but inside batch files the read operation is removed.
That means that something like echo(%thisDoesNotExist% will output (under the assumption the variable does not exist) %thisDoesNotExist% in command line and nothing in batch file.
What relation has this with your problem?
If we split your wget in parts you have
Wget64
--output-document="C:\Us ... y-%DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%.html"
^........^ ^.........^ ^.........^
"https://pe ... h?radio=1& ... &beginDate=03%2F01%2F2017&endDate=12%2F31%2F2017&year=#res"
^....^ ^....^
You can see where the parser tries to resolve variables, correctly in the output case and incorrectly (from the purpouse of the command point of view) in the URL.
You need to escape (by doubling them) the percent signs that are not part of a variable read operation, ex. ... beginDate=03%%2F01%%2F2017&...

Getting output on the same line of a file in DOS?

If I have output from two sources that I want to put together on the same line, how would I do that?
In my case I have a file and a program. The file is something like this:
listOfThings=
My program outputs a list of strings on a single line. I want have a small script that runs nightly to put these two things together on a single line. I can't figure out how to do this right though
example batch file
type header.txt > outputfile.txt
myProgram >> outputfile.txt
which results in this:
listOfThings=
foo bar baz etc
I really need the output file to have the list immediately follow the =, but I can't figure out how to do it with the >> operator. (and before anyone suggests it, I can't do something like put a \ on the end of the listOfThings= line, that won't work for what I'm trying to do)
You need to make sure that the contents of header.txt does not have a carriage return linefeed pair in it. Look at it with a hex editor and make sure there is no 0x0d0a in it.
Have you made sure that header.txt doesn't have any line separators in it at all? (Ie, the = is the very last byte of the file).
Also, try copying header.txt to outputfile.txt in case type is appending a line feed on it's own.