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". AppendFile(filename) opens the file with name filename. CreateFile(filename) creates a text file with name filename. CreateHTML(filename) creates a html file with name filename.
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(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(filename) closes the file for writing. The file must have been opened with the OpenFile or CreateHTML command.
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/".
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. 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.
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(); 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"); 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");Text file commands
AppendFile
CreateFile
CreateHTML
Write
WriteLn
CloseFile
ViewFile
Graph file commands
SaveGraph
proc(scatter);
SaveGraph("d:\scatterdiagram.svg",$graph);
File functions
FileList
SelectFolder
GetOpenFilename
GetSaveFilename
See also