Scripts - File commands
Data file commands
ReadData
ReadData(filename) loads a datafile into the spreadsheet.
If no filename is specified, the program will show the "Select file" dialog box.
If the filename is specified, it should contain the full path, e.g. "e:\documents\MedCalc\MyData.mc1".
The file can be a MedCalc file (*.mc1), Excel file (*.xls, *.xlsx), SPSS file (*.sav) or any other datafile that MedCalc can import.
To access a file on the desktop, you can use the constant @desktoppath, e.g.
file=@desktoppath+"MyData.mc1"; ReadData(file);
SaveData
SaveData(filename) saves the spreadsheet data.
If no filename is specified, the program will show the "Select file" dialog box.
If the filename is specified, it should contain the full path, e.g. "e:\documents\MedCalc\MyData.mc1".
Text file commands
AppendFile
AppendFile(filename) opens the file with name filename.
- If the file does not exist, it is created.
- If the file already exists, it is opened for writing. The filepointer is placed at the end of the file and new text is appended to the existing file.
CreateFile
CreateFile(filename) creates a text file with name filename.
- If the file does not exist, it is created.
- If the file already exists, it is opened for writing and will be overwritten with new content.
CreateHTML
CreateHTML(filename) creates a html file with name filename.
- If the file does not exist, it is created.
- If the file already exists, it is opened for writing and will be overwritten with new content.
- The file name extension does not have to be htm or html. You can use for example 'report.doc' as a filename. HTML files can easily be viewed and further edited as a document in Word.
- You only need to provide the body part of the HTML file. MedCalc will add the header up to and including the <body> part, and MedCalc will add the </body> </html> part when you close the HTML file. The HTML header contains CSS formatting for MedCalc reports, graphs, matrices and tables.
Write
Write(filename, data) writes the data as text to the specified file.
The file must already have been opened with the OpenFile or CreateHTML command.
In the text to write, you can include a new line code "\n" or tab code "\t".
Data can be text, a real number, a complex number, a matrix or a table.
You can write several values to the file using one single Write command. The different items need to be separated with the list separator.
You can specify the number of decimals by adding :n to the item. For example, write(filename,25.6123:2) will write 25.61 to the file. This works for real numbers, complex numbers and matrices.
WriteLn
WriteLn(filename) writes a new line to the specified file.
The file must already have been opened with the OpenFile or CreateHTML command.
When you use WriteLn on a HTML file, MedCalc will insert a <br> tag.
CloseFile
CloseFile(filename) closes the file for writing.
The file must have been opened with the OpenFile or CreateHTML command.
ViewFile
ViewFile(filename) opens the file. The program that is used to view the file, is dependent on the file extension. For example, if the filename is report.doc, the file will be opened with Word (if you are using Word as text editor). If the filename is report.html, the file will be opened in a browser. Using ViewFile, you can also open a web page url like "https://www.medcalc.org/".
Graph file commands
SaveGraph
SaveGraph(filename, graphid) saves a graph on disk using the specified filename.
The image format is PNG or SVG and the filename must have ".png" or ".svg" as extension.
Graphid is any of the system variables $graph, $graph1, or $graph2, which represent graphs obtained after using the PROC command.
proc(scatter); SaveGraph("d:\scatterdiagram.svg",$graph);
File functions
FileList
The FileList([path,][mask]) lists the files in the given path that match the mask, into a table. The table also contains the file sizes. Both arguments are optional and default to the current directory and *.* respectively.
For example, t=FileList("*.mc1"); creates a table t containing the mc1 files in the current directory.
A file mask is a pattern of fixed and wildcard characters used to match folder and file names. These masks provide a means for identifying specific files or groups of files based on their name and extension.
Most commonly the mask consists only of an asterisk and a file extension, for example: *.mc1 to select mc1 files, *.jpg to select jpg files, etc.
SelectFolder
The SelectFolder() function takes no arguments. The function displays a dialog in which the user can select a disk folder, and retuns the selected folder.
For example, folder=SelectFolder();
GetOpenFilename
The GetOpenFilename([path,][mask]) displays a dialog in which the user can select a file for reading. The filename must match the given mask. Both arguments are optional and default to the current directory and *.* respectively.
For example, file=GetOpenFilename($DataPath,"*.mc1");
GetSaveFilename
The GetSaveFilename([path,][mask]) displays a dialog in which the user can select a file for writing. The filename must match the given mask. Both arguments are optional and default to the current directory and *.* respectively.
For example, file=GetSaveFilename($DataPath,"*.mc1");