关于本站
人大经济论坛-经管之家:分享大学、考研、论文、会计、留学、数据、经济学、金融学、管理学、统计学、博弈论、统计年鉴、行业分析包括等相关资源。
经管之家是国内活跃的在线教育咨询平台!
获取电子版《CDA一级教材》
完整电子版已上线CDA网校,累计已有10万+在读~ 教材严格按考试大纲编写,适合CDA考生备考,也适合业务及数据分析岗位的从业者提升自我。
TOP热门关键词
免费学术公开课,扫码加入![]() |
可以用load语句读入一个用空格或逗号分隔数据项的文本文件,格式为:
load 向量名(变量名)=文件名;
load 矩阵名[行数,列数] = 文件名;
第一种格式读入所有数据到一个向量,适用于未知数据个数的情况,第二种格式读入给定的行、列数的数据项到一个矩阵。例如读入当前目录下的expendure.txt只要用:
load x[100,3]=expendure.txt;
要写一个文本文件也很简单,只要用output file = 文件名 reset;
语句就可以打开一个文件并从头开始写,这时用print语句输出的结果在显示到屏幕的同时被输出到指定的文件。如果上面的reset改为on,则输出是附加在指定文件末尾。为了关闭屏幕输出,用screen off;语句。再打开用“screen on”语句。也可以暂时关闭文件输出,用:output off;语句。恢复用“output on”语句。对于初学者,输出最好用print语句,简单明了,结果只在gauss系统下的Command Input-Output窗口显示。如使用过多的文本输出语句,不利于自己理解。
注意:在调入数据文件时,其路径一定要符合文件夹所处的地址。
如:output file=gmm.out reset;语句中等号后面没有具体的路径,这是因为我们在菜单栏已经把路径设置到D:\gauss7.0\ex下了。下面的语句load d[172,9]=habit.dat;也是如此。如果菜单栏只是把路径设置到D:\gauss7.0,这时,调用数据以及产出路径就要为load,
d[172,9]=D:\gauss7.0\ex\ habit.dat了,不然gauss系统提示找不到文件。
Gauss data 数据文件的后缀为.dat或.dht文件,打开或输入Gauss数据文件时的语句为,
Open x = data;
一些软件包,如fanpac,读者最好把fanpac中的,src文件看懂,然后使用它的估计程序,编写自己的
这样你不会局限于它的调用格式了。
一些软件包,做的非常不好,特别是调用数据格式上,不很直观
如.fmt矩阵数据格式的调用。
给大家举个例子:
new;cls;
load cpi[216,1]=tzl.txt; 调用自己的数据格式
save cpi; 保存为.fmt格式
open fx=cpi.fmt; 打开.fmt文件
y1=readr(fx,220); 读写.fmt。
但是这个读写,你在输入输出窗口看不见。
你必须在Tools工具栏下,Matrix Editor 编辑窗口中输入y1才能看到这个数据,但不能复制,可能少了其他一些功能。
所以,有些代码的调用格式是采取.fmt格式,太灵活了,也不好的。
在gauss userGuide中的File I/O输入输出标题下,读者把这些方面一定要读懂。
1、load
load statement can be used to load the data directly into a GAUSS matrix. The resulting GAUSS matrix must be no larger than the limit for a single matrix.
For example,
load x[] = dat1.asc;
will load the data in the file dat1.asc into an Nx1 matrix x. This method is preferred because rows(x) can be used to determine how many elements were actually loaded, and the matrix can be reshape’d to the desired form:
load x[] = dat1.asc;
if rows(x) eq 500;
x = reshape(x,100,5);
else;
errorlog “Read Error”;
end;
endif;
For quick interactive loading without error checking, use
load x[100,5] = dat1.asc;
This will load the data into a 100x5 matrix. If there are more or fewer than 500 numbers in the data set, the matrix will automatically be reshaped to 100x5.
2、Writing
To write data to an ASCII file, the print or printfm command is used to print to the auxiliary output. The resulting files are standard ASCII files and can be edited with GAUSS’s editor or another text editor.
The output and outwidth commands are used to control the auxiliary output. The print or printfm command is used to control what is sent to the output file.
The window can be turned on and off using screen. When printing a large amount of data to the auxiliary output, the window can be turned off using the command
screen off;
This will make the process much faster, especially if the auxiliary output is a disk file.
It is easy to forget to turn the window on again. Use the end statement to terminate your programs; end will automatically perform screen on and output off.
The following commands can be used to control printing to the auxiliary output:
format Specify format for printing a matrix.
output Open, close, rename auxiliary output file or device.
outwidth Auxiliary output width.
printfm Formatted matrix print.
print Print matrix or string.
screen Turn printing to the window on and off.
This example illustrates printing a matrix to a file:
format /rd 8,2;
outwidth 132;
output file = myfile.asc reset;
screen off;
print x;
output off;
screen on;
The numbers in the matrix x will be printed with a field width of 8 spaces per number, and with 2 places beyond the decimal point. The resulting file will be an ASCII data file. It will have 132 column lines maximum.
A more extended example follows. This program will write the contents of the GAUSS file mydata.dat into an ASCII file called mydata.asc. If there is an existing file by the name of mydata.asc, it will be overwritten:
output file = mydata.asc reset;
screen off;
format /rd 1,8;
open fp = mydata;
do until eof(fp);
print readr(fp,200);;
endo;
fp = close(fp);
end;
The output ... reset command will create an auxiliary output file called mydata.asc to receive the output. The window is turned off to speed up the process. The GAUSS data file mydata.dat is opened for reading, and 200 rows will be read per iteration until the end of the file is reached. The data read will be printed to the auxiliary output mydata.asc only, because the window is off.
3、字符串输入输出
getf will read a file and return it in a string variable. Any kind of file can be read in this way as long as it will fit into a single string variable.
To read files sequentially, use fopen to open the file and use fgets, fputs, and associated functions to read and write the file. The current position in a file can be determined with ftell. The following example uses these functions to copy an ASCII text file:
proc copy(src, dest);
local fin, fout, str;
fin = fopen(src, “rb”);
if not fin;
retp(1);
endif;
fout = fopen(dest, “wb”);
if not fin;
call close(fin);
retp(2);
endif;
do until eof(fin);
str = fgets(fin, 1024);
if fputs(fout, str) /= 1;
call close(fin);
call close(fout);
retp(3);
endif;
endo;
call close(fin);
call close(fout);
retp(0);
endp;
免流量费下载资料----在经管之家app可以下载论坛上的所有资源,并且不额外收取下载高峰期的论坛币。
涵盖所有经管领域的优秀内容----覆盖经济、管理、金融投资、计量统计、数据分析、国贸、财会等专业的学习宝库,各类资料应有尽有。
来自五湖四海的经管达人----已经有上千万的经管人来到这里,你可以找到任何学科方向、有共同话题的朋友。
经管之家(原人大经济论坛),跨越高校的围墙,带你走进经管知识的新世界。
扫描下方二维码下载并注册APP
您可能感兴趣的文章
人气文章
本文标题:问题解决方法:初学Gauss 对于数据输入的疑问
本文链接网址:https://bbs.pinggu.org/jg/ruanjianpeixun_gaussruanjianpeixun_1000438_1.html
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。



