How to save/export array as PDF in MATLAB - matlab

I have a large cell array which I want to export as report-like format. Is it possible to export arrays (strings and numbers) as a PDF file?
For example, say I have this cell array
data = {'Frank' 'Diana' '06-May-2018'}
and I want to export the this array content to a PDF file. In this case it should simply create a PDF file with the text:
Frank Diana 06-May-2018

The only way I know of for MATLAB to generate a PDF file is through a figure window. You can write text to a figure window, and print it to a PDF file:
fh = figure;
ah = axes('parent',fh,'position',[0,0,1,1],'visible','off',...
'xlim',[0,1],'ylim',[0,40],'ydir','reverse',...
'fontsize',14);
text(0.01,1,'text line 1','parent',ah);
text(0.01,2,'text line 2','parent',ah);
print(fh,'-dpdf','output.pdf')
The MATLAB File Exchange has a bunch of submissions that can help you print text to a figure window. Search for the tag "fprintf".
An alternative solution is to write the data to e.g. a Word document, or a Markdown or LaTeX file, and call appropriate programs from within MATLAB to convert those to PDF. The File Exchange has a submission to control Word. The pandoc or pdflatex external programs can be invoked through the ! or system functions.

Yes, easiest is use Matlab Notebook. Make it as pretty as you like.
Suggest to remove matlab-guide tag from your question, doesn't belong there.

Related

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

Importing data from text file and saving the same in excel

I am trying to read data from text file (which is output given by Tesseract OCR) and save the same in excel file. The problem i am facing here is the text files are in space separated format, and there are multiple files. Now i need to read all the files and save the same in excel sheet.
I am using MATLAB to import and export data. I even thought of using python to convert the files into CSV format so that i can easily import the same in MATLAB and simply excelwrite the same. But no good solution.
Any guidance would be of great help.
thank you
To read a text file in Matlab you can use fscanf or textscan then to export to excel you can use xlswrite that write directly to the excel file.

Writing to complex PDF's in MATLAB

I'm trying to write a MATLAB function that processes a file and writes a report on that file. The report will contain numbers, strings, tables, and images.
After looking at MATLAB's documentation, I can only find functions that save individual items to a file. For example, print saves a plot, write saves a table, etc. How do I create a single file that contains many of these items (e.g. a PDF with images, tables, and text)?
You can use print with the -append option to write multiple pages to a PostScript file in sequence, and then convert the ps to pdf. Using Matlab's handle graphics system, it is possible (if tedious) to design each print page in detail, arrange elements, etc.
However, if your document is going to be really complex, I think it would be better to generate the pdf in another way. One approach would be to write LaTeX code using lots of fprintfs and compile the file using pdflatex.
Btw., I'm not aware of a Matlab function write that generates a pdf.

Convert dataset of .mat format to .csv octave/matlab

there are datasets in .mat format in the this site: http://www.cs.nyu.edu/~roweis/data.html
I want to change the format to .csv.
Can someone tell me how to change the format to create the .csv file.
Thanks!
Suppose that the .mat files from the site are available already. In the command window in Matlab, you may write, for example:
load('C:\Users\YourUserName\Downloads\mnist_all.mat');
to load the .mat file; the result should be a set of matrices test0, test1, ..., train0, train1 ... created in your workspace, which you want saved as CSV files. Because they're different size, you need to save one CSV per variable, e.g. (also in the command window):
csvwrite('C:\Users\YourUserName\Downloads\mnist_test0.csv', test0);
Repeat the command for each variable, and do not forget to change also the name of the output file to avoid overwriting.
Did you tried the csvwrite function in Matlab?
Just load your .mat files with the load function and then write them with csvwrite!
I do not have a Matlab license so I installed GNU Octave 4.2.1 (2017) on Windows 10 (thank you to John W. Eaton and others). I was not fully successful using the csvwrite so I used the following workaround. (BTW, I am totally incompetent in the Octave world. csvwrite worked for simple data structures).
In the Command Window I used the following two commands
load myfile.mat
save("-text","myfile.txt","variablename")
When the "myfile.mat" is loaded, the variable names for the data vectors loaded are displayed in the workspace window. This is the name(s) to use in the save command. Some .mat files will load several data structures.
The "-text" option is the default, so you may not need to include this option in the command.
The output file lists the .mat file contents in text format as single column (of potentially sequential variables). It should be easy to use you text editor to massage this data into the original matrix structure for use in whatever app you are comfortable with.
Had a similar issue. Needed to convert a series of .mat files that had two columns of numerical data into standard data files (ascii text). Note that I don't really ever use csv, but everything here could be adapted by using csvwrite instead of the standard save.
Using Octave 4.2.1 ....
load myfile.mat
LI = [L, I] ## L and I are column vectors representing my data
save myfile.txt LI
Note that L and I appear to be default variable names chosen by Octave for the two columns vectors in my original data file. Ideally a script that iterated over all files with the .mat extension in my directory would be ideal, but this got the job done. It saves the data as two space separated columns of data.
*** Update
The following script works on Octave 4.2.1 for a series of data files with the .mat extension that are in the same directory. It will iterate over them and write the data out to text files with the same name but with the extension .dat . Note that this is not efficient, so if you have a lot of files or if they are large it can take a while to run. I would suggest that you run it from the command line using octave mat2dat.m so you can actually watch it go.
I make no guarantees that this will work for you, but it did for me. I also am NOT proficient in Octave or Matlab, so I'm sure a better solution exists.
# mat2dat.m
dirlist = glob("*.mat")
for i=1:length(dirlist)
filename = dirlist{i,1}
load(filename, "L", "I")
LI = [L,I]
tmpname = filename(1:length(filename)-3)
txtname = strcat(tmpname, 'dat')
save(txtname, "LI")
end

How to most effectively automate repetitive Excel task?

I want to automate Excel using Perl to do the following task(s):
For a list of Excel .xls files, do the following:
Open the file
Set Format to CSV
Save the file under the original filename and directory, but replace the extension "xls" with "csv"
Close the file
End
I found how to open files, even how to save them. I did not find how to change the fileformat/save as a different format. There shall be no user dialogs popping up, it should be fully automated. The Excel file list I can generate myself, a parameterized "find" or maybe "dir" should suffice.
If you are using Excel automation a great help is Excel itself. Use the VBA environment (Alt+F11) to get help for the Excel objects you want to use.
The objectbrowser (F2) is very valuable.
Workbook.SaveAs([Filename], [FileFormat], [Password], [WriteResPassword], [ReadOnlyRecommended], [CreateBackup], [AccessMode As XlSaveAsAccessMode = xlNoChange], [ConflictResolution], [AddToMru], [TextCodepage], [TextVisualLayout], [Local])
Searching for CSV in the object browser will show Excel constants with their values, since you probably cannot use these Excel constants in Perl.
See Spreadsheet::ParseExcel and xls2csv, they will help you.