Wednesday, January 27, 2010

Robert Burns 罗伯特·彭斯

Robert Burns 罗伯特·彭斯

Robert Burns

Born in Alloway, Scotland, on January 25, 1759, Robert Burns was the first of William and Agnes Burnes' seven children. His father, a tenant farmer, educated his children at home. Burns also attended one year of mathematics schooling and, between 1765 and 1768, he attended an "adventure" school established by his father and John Murdock. His father died in bankruptcy in 1784, and Burns and his brother Gilbert took over farm. This hard labor later contributed to the heart trouble that Burns' suffered as an adult.

At the age of fifteen, he fell in love and shortly thereafter he wrote his first poem. As a young man, Burns pursued both love and poetry with uncommon zeal. In 1785, he fathered the first of his fourteen children. His biographer, DeLancey Ferguson, had said, "it was not so much that he was conspicuously sinful as that he sinned conspicuously." Between 1784 and 1785, Burns also wrote many of the poems collected in his first book, Poems, Chiefly in the Scottish Dialect, which was printed in 1786 and paid for by subscriptions. This collection was an immediate success and Burns was celebrated throughout England and Scotland as a great "peasant-poet."

In 1788, he and his wife, Jean Armour, settled in Ellisland, where Burns was given a commission as an excise officer. He also began to assist James Johnson in collecting folk songs for an anthology entitled The Scots Musical Museum. Burns' spent the final twelve years of his life editing and imitating traditional folk songs for this volume and for Select Collection of Original Scottish Airs. These volumes were essential in preserving parts of Scotland's cultural heritage and include such well-known songs as "My Luve is Like a Red Red Rose" and "Auld Land Syne." Robert Burns died from heart disease at the age of thirty-seven. On the day of his death, Jean Armour gave birth to his last son, Maxwell.

Most of Burns' poems were written in Scots. They document and celebrate traditional Scottish culture, expressions of farm life, and class and religious distinctions. Burns wrote in a variety of forms: epistles to friends, ballads, and songs. His best-known poem is the mock-heroic Tam o' Shanter. He is also well known for the over three hundred songs he wrote which celebrate love, friendship, work, and drink with often hilarious and tender sympathy. Even today, he is often referred to as the National Bard of Scotland.

外部链接:彭斯官方网:http://www.robertburns.org/

英国诗人 。1759年1月25日生于苏格兰艾尔郡阿洛韦镇的一个佃农家庭,1796年7月21日卒于邓弗里斯。自幼家境贫寒,未受过正规教育,靠自学获得多方面的知识。最优秀的诗歌作品产生于1785~1790年 ,收集在诗集《主要以苏格兰方言而写的诗》中。诗集体现了诗人一反当时英国诗坛的新古典主义诗风,从地方生活和民间文学中汲取营养,为诗歌创作带来了新鲜的活力,形成了他诗歌创作的基本特色。以虔诚的感情歌颂大自然及乡村生活;以入木三分的犀利言辞讽刺教会及日常生活中人们的虚伪。诗集使彭斯一举成名,被称为天才的农夫。后应邀到爱丁堡,出入于上流社会的显贵中间。但发现自己高傲的天性和激进思想与上流社会格格不入,乃返回故乡务农。一度到苏格兰北部高原地区游历,后来当了税务官,一边任职一边创作。

彭斯的诗歌作品多使用苏格兰方言,并多为抒情短诗,如歌颂爱情的名篇《我的爱人像朵红红的玫瑰》和抒发爱国热情的《苏格兰人》等。他还创作了不少讽刺诗(如《威利长老的祈祷》),诗札(如《致拉布雷克书》)和叙事诗(如《两只狗》和《快活的乞丐》)。作品表达了平民阶级的思想感情,同情下层人民疾苦,同时以健康、自然的方式体现了追求“美酒、女人和歌”的快乐主义人生哲学。彭斯富有敏锐的幽默感。对苏格兰乡村生活的生动描写使他的诗歌作品具有民族特色和艺术魅力。

除诗歌创作外,彭斯还收集整理大量的苏格兰民间歌谣,编辑出版了6卷本的《苏格兰音乐总汇》和8卷本的《原始的苏格兰歌曲选集》。其中《友谊地久天长》不仅享誉苏格兰,而且闻名世界。

在地球的各个角落,在亲朋的离别或是会议的告别仪式,人们以各种不同语言齐唱《友谊地久天长》(Auld Lang Syne,又名《骊歌》),朋友们紧紧挽着手,歌唱永不相忘的友谊。它驱走了人们离别的哀愁,使人们满怀激情各奔前程。这首家喻户晓的苏格兰民歌的词作者,即是著名的民族诗人罗伯特·彭斯(Robert Burns,1759-1796)。

彭斯出生在一个贫苦农民家庭,以租地耕种为生。幼时在苏格兰家乡附近上小学。不久校长离去,父亲请老师来家教学。老师认为彭斯兄弟不比年长的同学差。父亲晚上教他们文法及神学。12岁时彭斯二兄弟又轮流去离家四英里的村落上学,14岁在学习英语之余,开始学习法文。

彭斯15岁时成为父亲身边主要的劳动力,驾驭马匹在土墩及洼地上耕作。劳动极为艰辛。虽数次更换土地租耕,因土地贫瘠,收获仍然不佳。劳动之余,彭斯爱读苏格兰诗人申斯通(Shenstone,1714-1793)、蒲柏(Pope,1688-1744)及弗格森(Fergusson 1750-1774)的作品,也浏览苏格兰小说家麦肯齐(MacKenzie,1745-1831)的书。他希望能成为苏格兰艾尔郡(Ayrshire)的诗人,歌唱故乡的山河。

1784年他的父亲去世,全家迁去莫斯吉尔(Mossgiel),耕作收获并无好转。幸而他的主要用苏格兰方言写的诗集得以出版并迅即获得成功,爱丁堡的出版商又很快为之再版。编辑文学杂志的麦肯齐在评论中称赞这位庄稼汉是位诗歌天才。

于是在爱丁堡,彭斯穿起深色大衣、浅背心、皱边的衬衫,足登鹿皮鞋或长靴,过起出入文学集会及酒馆的双重生活。

彭斯在爱丁堡生活及游历苏格兰一段时期后,仍回乡务农。1788年他考取税务局职员,除了在农田干活外,还要每周在马背上驰骋200英里去上班。执法时,他不放过大鱼,但对贫穷者则手下留情。他为了全力做好税务局的工作,1791年放弃农活迁往邓弗里斯(Dumfries),在那儿度过了他的最后的岁月。

彭斯写了大量的抒情诗,还写讽刺诗及叙事诗。他也喜爱歌曲,有敏锐的音乐耳朵,对节奏有良好的反应。他把最后十年的精力,主要放在为二个丛刊的整理及收集民歌上,使濒临失传的三百多首民歌得以保存下来。

1796年他患了风湿性关节炎及心脏病,于同年7月21日英年早逝。前来送葬的多达二万人。

当年彭斯出生并度过了七年童年的茅屋,位于艾尔郡的阿洛韦镇(Alloway),现由彭斯纪念碑信托基金机构管理。与茅屋相连接的红瓦顶、前面为长廊及花园的博物馆,为信托机构理事会于1920年所扩建。1994年该理事会重铺稻草屋顶,再建18世纪的菜园及石堤。这就是现在世界著名的彭斯茅舍。



彭斯雕像位于艾尔市中心。从市中心附近坐开往杜恩河老桥(Auld Brig O’Doon)的公共汽车,在终点老桥前一站下车,即见到茅草顶的白色平房,木制的门窗是深棕色的。门上方的黑色纪念板上写着“彭斯茅舍”,接着是“罗伯特·彭斯—艾尔郡诗人”及他的生卒年月。进门后先是谷仓,然后是牛棚及马厩。依稀传来牲畜的叫声,蜡制的耕牛旁还有几只母鸡在啄食谷粒。起居室中以蜡像布置一家人当时融洽的情景。父亲在烛光下读《圣经》,母亲抱着妹妹坐在对面,弟弟坐在一边,彭斯则光着脚站在一旁专心听讲。一个小妹妹躺在摇篮里。厨房里熏黑的炉灶还生着火,彭斯出生的床即在厨房内。布置一如当年。幼年时母亲在这里教孩子们唱苏格兰民歌,姨母则介绍给他们大量有关鬼怪神仙的故事和歌曲。“彭斯茅屋”对崇拜彭斯的人来说是圣地,但也是当年贫苦农家家居生活的写照。

彭斯长期生活在农村,从事繁重的农活。地主的剥削,加上土地的贫瘠,欠收、负债、迁居……使他常常过着没有温饱的生活。但他热爱生活,对劳动人民有深厚的感情。在《两只狗》这首诗里,通过两只分属贫富人家的狗之间的对话,描绘了地主家的骄奢淫逸。贫穷的佃户虽然耕作及劳动辛苦,但同欢共乐聚在一起。而这两家的狗能够融洽相处,成为人类不公平生活的鲜明对照。

诗人也理解农民对牲口的深厚感情。他在《新年早晨老农向老马麦琪致辞》一诗中,回顾了老马一生的辛劳后,写道:“我将在留下的麦地上面,把你的缰绳系好,不用费大力气,你就在那边舒舒畅畅吃个饱。”

与彭斯茅屋相通连的彭斯博物馆,收藏了彭斯珍贵的手稿、他的包括早期版本的作品、有关的画像等,有些收藏品来自美国、加拿大甚至南非。

大展览室介绍他一生的劳动、写作和生活。以图片的形式,配合他的诗句、信件或日记,生动地叙述了他当年的经历。这里还展出了他的怀表、记事本、墨水瓶、鼻烟壶,两把柄上刻有“R.B.”的手枪,以及当税务员测酒用的长棒,也展有1786年版的主要用苏格兰方言写的诗集。笔者自然记得寻找《友谊地久天长》的原稿,它原来出自彭斯于1788年写给友人的一封信中。

第二室展出数幅著名的油画。《羊杂宴》描绘彭斯夫妇款待客人的场景。彭斯喜欢这种热闹场面。另有一组四幅的版画,描绘他的作品《汤姆·奥桑特的故事》(Tam o’Shanter),彭斯这部根据民间传说写的长诗,讲的是汤姆深夜回家途中遇鬼的故事。他去除了传说中迷信的成分,以喜剧形式讲魔法,寓有深意。同时,它也把儿时听到的传说,与故乡阿罗韦他幼时熟悉的界标、陈旧的教堂、古老的石桥和石冢等联系起来,具有沧桑感和神秘感。

离开这里步行一里,抵达老杜恩河桥,彭斯纪念碑即位于附近的山丘上。这座希腊式建筑由爱丁堡著名建筑师设计,1823年完成,耗资3247镑。登上这座台式纪念碑,可眺望杜恩河及卡里克山(Carrick Hill)的优美景色。纪念碑的基座建有展览室,展出15种外国文字的彭斯著作。在附近花园里,还建有一个雕像室,内有三座《汤姆·奥桑特的故事》中的人物雕像,真人大小,造型风趣。

归途中于老杜恩河老桥公共汽车站,见到一家大百货公司,里面的多种商品以彭斯命名。如果时间合适(回到艾尔的公共汽车每小时一班),还可看一看介绍彭斯的记录影片。

在他住过的基尔马诺克(Kilmarnock)及邓弗里斯也建有彭斯博物馆、雕像或纪念碑,欧文(lrving)也有他的雕像。甚至远在加拿大和澳大利亚,也有他的纪念碑。位于苏格兰首府爱丁堡的“三作家博物馆”介绍了彭斯、司各特(Walter Scott,1771-1832)及斯蒂文森(Robert L.Stevenson, 1850-1894)的生平,也值得一去。

在彭斯的故乡苏格兰,有数千个彭斯联谊会,苏格兰各地每年都庆祝他的生日。

如此广受故乡人民爱戴的诗人,在世界上也不多见。因为除了长期生活在农村并写出描绘故乡及朴直人民的诗歌外,身受民族压迫的他十分热爱苏格兰,并热情歌颂民主及自由。

在彭斯的青年时代,先后爆发了美国独立战争和法国大革命。他关心世界政治及苏格兰祖国的命运。他写的《华盛顿将军生辰颂》,赞扬美国人民的独立斗争。在法国大革命的影响下,他写了《自由树》和《苏格兰人》两首著名长诗。《自由树》声言有了法兰西这棵自由树,人类将变得平等,世界将获得和平。《苏格兰人》重温历史,以颂扬早年民族英雄华莱士等人的事迹来激励人民:

谁愿为苏格兰国君和法律,
奋力把自由之剑拔出?
生为自由人,死为自由魂,
让他跟我前进!

彭斯是人民的诗人,也是为自由而斗争的战士。这颗明亮的星,永远闪耀在苏格兰的上空,也永远闪耀在爱好和平与友谊的人们心中。



【作品选译】

一朵红红的玫瑰 【英文朗诵:下载地址】

啊,我的爱人象朵红红的玫瑰,
 六月里迎风初开,
啊,我的爱人象支甜甜的曲子,
 奏得合拍又和谐。

我的好姑娘,多么美丽的人儿!
 请看我,多么深挚的爱情!
亲爱的,我永远爱你,
 纵使大海干涸水流尽。

纵使大海千涸水流尽,
 太阳将岩石烧作灰尘,
亲爱的,我永远爱你,
 只要我一息犹存。

珍重吧,我唯一的爱人,
 珍重吧,让我们暂时别离,
但我定要回来,
 哪怕千里万里!
            王佐良译


往昔的时光

老朋友哪能遗忘,
  哪能不放在心上?
老朋友哪能遗忘,
  还有往昔的时光?

为了往昔的时光,老朋友,
  为了往昔的时光,
再干一杯友情的酒,
  为了往昔的时光,

你来痛饮一大杯,
  我也买酒来相陪。
干一杯友情的酒又何妨?
  为了往昔的时光。

我们曾邀游山岗,
  到处将野花拜访。
但以后走上疲惫的旅程,
  逝去了往昔的时光!

我们曾赤脚瞠过河流,
  水声笑语里将时间忘。
如今大海的怒涛把我们隔开,
  逝去了往昔的时光!

忠实的老友,伸出你的手,
  让我们握手聚一堂,
再来痛饮—杯欢乐酒,
  为了往昔的时光!
            王佐良译


给我开门,哦!

  曲调:轻轻地开门


哦,开门,纵使你对戬无情,
  也表一点怜悯,哦。
你虽变了心,我仍忠于糟.
  哦,给我开门,哦。

风吹我苍白的双颊,好冷!
  但冷不过你对我的心,哦.
冰霜使我心血凝冻,
  也没你给我的痛深,哦。

残月沉落白水中,
  时间也随我沉落,哦。
假朋友,变心人,永别不再逢!
  我决不再采烦渎,哦。

她把门儿大敞开,
  见了平地上苍白的尸体,哦,
只喊了一声“爱’就倒在尘埃,
  从此再也不起,哦。
            王佐良译


走过麦田来

(合唱)啊,珍尼是可怜的人儿,
     珍尼哭得悲哀。
    她拖着长裙,
     走过麦田来。

    可怜的人儿,走过麦田来,
     走过麦田来,
    她拖着长裙
     走过麦田来。

    如果一个他碰见一个她,
     走过麦田来,
    如果一个他吻了一个她,
     她何必哭起来?

    如果一个他碰见一个她
     走过山间小道,
    如果一个他吻了一个她,
     别人哪用知道!

(合唱)啊,珍尼是可怜的人儿,
     珍尼哭得悲哀。
    她拖着长裙,
     走过麦田来。
            王佐良译


如果你站在冷风里

呵,如果你站在冷风里,
 一人在草地,在草地,
我的小屋会挡住凶恶的风,
 保护你,保护你。
如果灾难象风暴袭来,
 落在你头上,你头上,
我将用胸脯温暖你,
 一切同享,一切同当。

如果我站在可怕的荒野,
 天黑又把路迷,把路迷,
就是沙漠也变成天堂,
 只要有你,只要有你。
如果我是地球的君王,
 宝座我们共有,我们共有,
我的王冠上有一粒最亮的珍珠——
 它是我的王后,我的王后。
            王佐良译
    选自《彭斯诗选》,人民文学出版社(1959)


苏格兰人①

跟华莱士流过血的苏格兰人,
随布鲁斯作过战的苏格兰人,
起来!倒在血泊里也成——
     要不就夺取胜利!

时刻已到,决战已近,
前线的军情吃紧,
骄横的爱德华在统兵入侵——
     带来锁链,带来奴役!

谁愿卖国求荣?
谁愿爬进懦夫的坟茔?
谁卑鄙到宁做奴隶偷生?——
     让他走,让他逃避!

谁愿将苏格兰国王和法律保护,
拔出自由之剑来痛击、猛舞?
谁愿生作自由人,死作自由魂?——
     让他来,跟我出击!

凭被压迫者的苦难采起誓,
凭你们受奴役的子孙来起誓,
我们决心流血到死——
     但他们必须自由!

打倒骄横的篡位者!
死一个敌人,少一个暴君!
多一次攻击,添一分自由!
     动手——要不就断头!
           袁可嘉译
 ①这是彭斯所作爱国诗中最著名的一首,写的是苏格兰
国王罗伯特·布鲁斯在大破英国侵略军的班诺克本一役
(1314)之前向部队所作的号召。首先发表在1794年6月的
《纪事晨报》。
  诗中所提的华莱士是一位十三世纪的英格兰民族英雄,
也曾大败英军。但后来为奸人出卖,被执处死。爱德华指
英王爱德华二世。
  彭斯一直念念不忘为苏格兰民族独立而斗争的志士,
写此诗时爱国热情尤其澎湃。不仅如此,他还借古讽今,
曾经明白写信告诉朋友说:启发他写这首诗的不止是古代
那场“光荣的争取自由的斗争”,而还有“在时间上却不
是那么遥远的同类性质的斗争”,即法国大革命,当时正
方兴未艾,在苏格兰的彼岸如火如荼地展开。


我的心儿在高原①

我的心儿在高原,我的心不在这儿,
我的心儿在高原,迫遂着鹿儿。
追逐着野鹿,跟踪着獐儿;
我的心儿在高原,不管我上哪儿,
别了啊高原,别了啊北国,
英雄的家乡,可敬的故国,
不管我上哪儿漂荡,我上哪儿遨游,
我永远爱着高原的山丘。

别了啊,高耸的积雪的山岳,
别了啊,山下的溪壑和翠谷,
别了啊,森林和枝檀纵横的树林,
别了啊,急川和洪流的轰鸣,
我的心儿在高原,我的心不在这儿,
我的心儿在高原,追逐着鹿儿,
追逐着野鹿,跟踪着獐儿,
我的心儿在高原,不管我上哪儿。
             袁可嘉译
 ①苏格兰北部地区。

Tuesday, January 19, 2010

Bonferroni correction in SPSS

ANOVA with SPSS


Never, ever, run any statistical test without performing EDA first!

What's wrong with t-tests?

Nothing, except ...

If you want to compare three or more groups using t-tests with the usual 0.05 level of significance, you would have to compare the three groups pairwise (A to B, A to C, B to C), so the chance of getting the wrong result would be:

1 - (0.95 x 0.95 x 0.95) = 14.3%
If you wanted to compare four or more groups, the chance of getting the wrong result would be (0.95)6 = 26%, and for five groups, 40%. Not good, is it? So we use ANOVA. Never perform multiple t-tests: Anyone on this module discovered performing multiple t-tests when they should use ANOVA will be shot!

ANalysis Of VAriance (ANOVA) is such an important statistical method that it would be easy to spend a whole module on this test alone. Like the t-test, ANOVA is a parametric test which assumes:

•data is numerical data representing samples from normally distributed populations

•the variances of the groups are "similar"

•the sizes of the groups are "similar"

•the groups should be independent

so it's important to carry out EDA before starting AVOVA! In fact, ANOVA is quite a robust procedure, so as long as the groups are similar, the test is normally reliable.

ANOVA tests the null hypothesis that the means of all the groups being compared are equal, and produces a statistic called F which is equivalent to the t-statistic from a t-test. But there's a catch. If the means of all the groups tested by ANOVA are equal, fine. But if the result tells us to reject the null hypothesis, we still don't know which of the means differ. We solve this problem by performing what is known as a "post hoc" (after the event) test.

Reminder:

•Independent variable: Variables which are experimentally manipulated by an investigator are called independent variables.

•Dependent variable: Variables which are measured are called dependent variables (because they are presumed to depend on the value of the independent variable).

ANOVA jargon:

•Way = an independent variable, so a one-way ANOVA has one independent variable, two-way ANOVA has two independent variables, etc. Simple ANOVA tests the hypothesis that means from two or more samples are equal (drawn from populations with the same mean). Student's t-test is actually a particular application of one-way ANOVA (two groups compared).

•Factor = a test or measurement. Single-factor ANOVA tests whether the means of the groups being compared are equal and returns a yes/no answer, two-factor ANOVA simultaneously tests two or more factors, e.g. tumour size after treatment with different drugs and/or radiotherapy (drug treatment is one factor and radiotherapy is another). So, "factor" and "way" are alternative terms for the same thing (inpependent variables).

•Repeated measures: Used when members of a sample are measured under different conditions. As the sample is exposed to each condition, the measurement of the dependent variable is repeated. Using standard ANOVA is not appropriate because it fails to take into account correlation between the repeated measures, violating the assumption of independence. This approach can be used for several reasons, e.g. where research requires repeated measures, such as longitudinal research which measures each sample member at each of several ages - age is a repeated factor. This is comparable to a paired t-test.

The array of options for different ANOVA tests in SPSS is confusing, so I'll go through the most important bits using some examples.

One-Way / Single-Factor ANOVA:

Data:

Pain Scores for Analgesics

Drug: Pain Score:

Diclofenac 0, 35, 31, 29, 20, 7, 43, 16

Ibuprophen 30, 40, 27, 25, 39, 15, 30, 45

Paracetamol 16, 33, 25, 32, 21, 54, 57, 19

Asprin 55, 58, 56, 57, 56, 53, 59, 55

Since it would be unethical to withhold pain relief, there is no control group and we are just interested in knowing whether one drug performs better (lower pain score) than another, so we need to perform a one-way/single-factor ANOVA.

We enter this data into SPSS using dummy values (1, 2, 3, 4) for the drugs so this numeric data can be used in the ANOVA:


It's always a good idea to enter descriptive labels for data into the Variable View window, or the output is difficult to interpret!

EDA (Analyzer: Descriptive Statistics: Explore) shows that the data is normally distributed, so we can proceed with the ANOVA:

Analyze: Compare Means: One-Way ANOVA

Dependent variable: Pain Score

Factor: Drug:

•SPSS allows many different post hoc tests. Click Post Hoc and select the Tukey and Games-Howell tests.

◦The Tukey test is powerful and widely accepted, but is parametric in that it assumes that the population variances are equal. It also assumes that the sample sizes are equal. If this is not the case, you should use Gabriel's procedure, or if the sizes are very different, use Hochberg's GT2.

◦Games-Howell does not assume population variances are equal or that sample sizes are equal, so is a good alternative if this turns out to be the case.

•Click Options and select Homogeneity of Variance Test, Brown-Forsythe and Welch. The homogeneity of variance test is important since this is an assumption of ANOVA, but if this assumption turns out to be broken, the Brown-Forsythe and Welch options will display alternative versions of the F statistic which means you may still be able to use the result.

•Click OK to run the tests.

Output:



Test of Homogeneity of Variances: Pain Levene Statistic df1 df2 Sig.

4.837 3 28 .008

The significance value for homogeneity of variances is <.05, so the variances of the groups are significantly different. Since this is an assumption of ANOVA, we need to be very careful in interpreting the outcome of this test:

ANOVA: Pain

Sum of Squares df Mean Square F Sig.

Between Groups 4956.375 3 1652.125 11.967 .000

Within Groups 3865.500 28 138.054

Total 8821.875 31


This is the main ANOVA result. The significance value comparing the groups (drugs) is <.05, so we could reject the null hypothesis (there is no difference in the mean pain scores with the four drugs). However, since the variances are significantly different, this might be the wrong answer. Fortunately, the Welch and Brown-Forsythe statistics can still be used in these circumstances:

Robust Tests of Equality of Means: Pain

Statistic df1 df2 Sig.

Welch 32.064 3 12.171 .000

Brown-Forsythe 11.967 3 18.889 .000

The significance value of these are both <.05, so we still reject the null hypothesis. However, this result does not tell us which drugs are responsible for the difference, so we need the post hoc test results:

Multiple Comparisons

Dependent Variable: Pain

(I) Drug (J) Drug Mean Difference (I-J) Std. Error Sig. 95% Confidence Interval

Lower Bound Upper Bound

Tukey HSD 1 2 -8.750 5.875 .457 -24.79 7.29

3 -9.500 5.875 .386 -25.54 6.54

4 -33.500(*) 5.875 .000 -49.54 -17.46

2 1 8.750 5.875 .457 -7.29 24.79

3 -.750 5.875 .999 -16.79 15.29

4 -24.750(*) 5.875 .001 -40.79 -8.71

3 1 9.500 5.875 .386 -6.54 25.54

2 .750 5.875 .999 -15.29 16.79

4 -24.000(*) 5.875 .002 -40.04 -7.96

4 1 33.500(*) 5.875 .000 17.46 49.54

2 24.750(*) 5.875 .001 8.71 40.79

3 24.000(*) 5.875 .002 7.96 40.04

Games-Howell 1 2 -8.750 6.176 .513 -27.05 9.55

3 -9.500 7.548 .602 -31.45 12.45

4 -33.500(*) 5.194 .001 -50.55 -16.45

2 1 8.750 6.176 .513 -9.55 27.05

3 -.750 6.485 .999 -20.09 18.59

4 -24.750(*) 3.471 .001 -36.03 -13.47

3 1 9.500 7.548 .602 -12.45 31.45

2 .750 6.485 .999 -18.59 20.09

4 -24.000(*) 5.558 .014 -42.26 -5.74

4 1 33.500(*) 5.194 .001 16.45 50.55

2 24.750(*) 3.471 .001 13.47 36.03

3 24.000(*) 5.558 .014 5.74 42.26

* The mean difference is significant at the .05 level.



The Tukey test relies on homogeneity of variance, so we ignore these results. The Games-Howell post-hoc test does not rely on homogeneity of variance (this is why we used two different post-hoc tests) and so can be used. SPSS kindly flags (*) which differences are significant!

Result: Drug 4 (Asprin) produces significantly different result from the other three drugs:



Formal Reporting: When we report the outcome of an ANOVA, we cite the value of the F ratio and give the number of degrees of freedom, outcome (in a neutral fashion) and significance value. So in this case:



There is a significant difference between the pain scores for asprin and the other three drugs tested, F(3,28) = 11.97, p < .05.









Two-Factor ANOVA

Do anti-cancer drugs have different effects in males and females?

Data:



Drug: cisplatin vinblastine 5-fluorouracil

Gender:

Female Male Female Male Female Male

Tumour

Size: 65 50 70 45 55 35

70 55 65 60 65 40

60 80 60 85 70 35

60 65 70 65 55 55

60 70 65 70 55 35

55 75 60 70 60 40

60 75 60 80 50 45

50 65 50 60 50 40



We enter this data into SPSS using dummy values for the drugs (1, 2, 3) and genders (1,2) so the coded data can be used in the ANOVA:







It's always a good idea to enter descriptive labels for data into the Variable View window, or the output is difficult to interpret!



EDA (Analyze: Descriptive Statistics: Explore) shows that the data is normally distributed, so we can proceed with the ANOVA:



Analyze: General Linear Model: Univariate



Dependent variable: Tumour Diameter

Fixed Factors: Gender, Drug:







Also select:



Post Hoc: Tukey and Games-Howell:







Options:



Display Means for: Gender, Drug, Gender*Drug

Descriptive Statistics

Homogeneity tests:







Output:



Levene's Test of Equality of Error Variances(a)

Dependent Variable: Diameter F df1 df2 Sig.

1.462 5 42 .223

Tests the null hypothesis that the error variance of the dependent variable is equal across groups.

a Design: Intercept+Gender+Drug+Gender * Drug



The significance result for homogeneity of variance is >.05, which shows that the error variance of the dependent variable is equal across the groups, i.e. the assumption of the ANOVA test has been met.



Tests of Between-Subjects Effects

Dependent Variable: Diameter Source Type III Sum of Squares df Mean Square F Sig.

Corrected Model 3817.188(a) 5 763.438 10.459 .000

Intercept 167442.188 1 167442.188 2294.009 .000

Gender 42.188 1 42.188 .578 .451

Drug 2412.500 2 1206.250 16.526 .000

Gender * Drug 1362.500 2 681.250 9.333 .000

Error 3065.625 42 72.991

Total 174325.000 48

Corrected Total 6882.813 47

a R Squared = .555 (Adjusted R Squared = .502)



The highlighted values are significant (<.05), but there is no effect of gender (p = 0.451). Again, this does not tell us which drugs behave differently, so again we need to look at the post hoc tests:



Multiple Comparisons

Dependent Variable: Diameter

(I) Drug (J) Drug Mean Difference (I-J) Std. Error Sig. 95% Confidence Interval

Lower Bound Upper Bound

Tukey HSD cisplatin vinblastine -1.25 3.021 .910 -8.59 6.09

5-flourouracil 14.38(*) 3.021 .000 7.04 21.71

vinblastine cisplatin 1.25 3.021 .910 -6.09 8.59

5-flourouracil 15.63(*) 3.021 .000 8.29 22.96

5-flourouracil cisplatin -14.38(*) 3.021 .000 -21.71 -7.04

vinblastine -15.63(*) 3.021 .000 -22.96 -8.29

Games-Howell cisplatin vinblastine -1.25 3.329 .925 -9.46 6.96

5-flourouracil 14.38(*) 3.534 .001 5.64 23.11

vinblastine cisplatin 1.25 3.329 .925 -6.96 9.46

5-flourouracil 15.63(*) 3.699 .001 6.50 24.75

5-flourouracil cisplatin -14.38(*) 3.534 .001 -23.11 -5.64

vinblastine -15.63(*) 3.699 .001 -24.75 -6.50

Based on observed means.

* The mean difference is significant at the .05 level.



In this example, we can use the Tukey or Games-Howell results. Again, SPSS helpfully flags which results have reached statistical significance. We already know from the main ANOVA table that the effect of gender is not significant, but the post hoc tests show which drugs produce significantly different outcomes.



Formal Reporting: When we report the outcome of an ANOVA, we cite the value of the F ratio and give the number of degrees of freedom, outcome (in a neutral fashion) and significance value. So in this case:



There is a significant difference between the tumour diameter for 5-flourouracil and the other two drugs tested, F(5,47) = 10.46, p < .05.













Repeated Measures ANOVA

Remember that one of the assumptions of ANOVA is independence of the groups being compared. In lots of circumstances, we want to test the same thing repeatedly, e.g:



•Patients with a chronic disease after 3, 6 and 12 months of drug treatment

•Repeated sampling from the same location, e.g. spring, summer, autumn and winter

•etc

This type of study reduces variability in the data and so increases the power to detect effects, but violates the assumption of independence, so as with the paired t-test, we need to use a special form of ANOVA called repeated measures. In a parametric test, the assumption that the relationship between pairs of groups is equal is called "sphericity". Violating sphericity means that the F statistic cannot be compared to the normal tables of F, and so software cannot calculate a significance value. SPSS includes a procedure called Mauchly's test which tells us if the assumption of sphericity has been violated:



•If Mauchly’s test statistic is significant (i.e. p 0.05) we conclude that the condition of sphericity has not been met.

•If, Mauchly’s test statistic is nonsignificant (i.e. p >.05) it is reasonable to conclude that the variances of differences are not significantly different.

If Mauchly’s test is significant then we cannot trust the F-ratios produced by SPSS unless we apply a correction (which, fortunately, SPSS helps us to do).



One-Way Repeated Measures ANOVA

i.e. one independent variable, e.g. pain score after surgery:



Patient1 Patient2 Patient3

1 3 1

2 5 3

4 6 6

5 7 4

5 9 1

6 10 3



This data can be entered directly into SPSS. Note that each column represents a repeated measures variable (patients in this case). There is no need for a coding variable (as with between-group designs, above):







It's always a good idea to enter descriptive labels for data into the Variable View window, or the output is difficult to interpret! Next:



Analyze: General Linear Model: Repeated Measures







Within-Subject factor name: Patient

Number of Levels: 3 (because there are 3 patients)

Click Add, then Define (factors):







There are no proper post hoc tests for repeated measures variables in SPSS. However, via the Options button, you can use the paired t-test procedure to compare all pairs of levels of the independent variable, and then apply a Bonferroni correction to the probability at which you accept any of these tests. The resulting probability value should be used as the criterion for statistical significance. A ‘Bonferroni correction’ is achieved by dividing the probability value (usually 0.05) by the number of tests conducted, e.g. if we compare all levels of the independent variable of these data, we make three comparisons and so the appropriate significance level is 0.05/3 = 0.0167. Therefore, we accept t-tests as being significant only if they have a p value <0.0167.







Output:



Mauchly's Test of Sphericity Within Subjects Effect Mauchly's W Approx. Chi-Square df Sig. Epsilon

Greenhouse-Geisser Huynh-Feldt Lower-bound

patient .094 9.437 2 .009 .525 .544 .500



Mauchly’s test is significant (p <.05) so we conclude that the assumption of sphericity has not been met.



Tests of Within-Subjects Effects Source

Type III Sum of Squares df Mean Square F Sig.

patient Sphericity Assumed 44.333 2 22.167 8.210 .008

Greenhouse-Geisser 44.333 1.050 42.239 8.210 .033

Huynh-Feldt 44.333 1.088 40.752 8.210 .031

Lower-bound 44.333 1.000 44.333 8.210 .035

Error(patient) Sphericity Assumed 27.000 10 2.700

Greenhouse-Geisser 27.000 5.248 5.145

Huynh-Feldt 27.000 5.439 4.964

Lower-bound 27.000 5.000 5.400



Because the significance values are <.05, we conclude that there was a significant difference between the three patients, but this test does not tell us which patients differed from each other. The next issue is which of the three corrections to use. Going back to Mauchly's test:



•If epsilon is >0.75, use the Huynh-Feldt correction.

•If epsilon is <0.75, or nothing is known about sphericity at all, use the Greenhouse-Geisser correction.

•In this example, the epsilon values from Mauchly's test values are 0.525 and 0.544, both <0.75, so we use the Greenhouse-Geisser corrected values. Using this correction, F is still significant because its p value is 0.033, which is <.05.

Post Hoc Tests:



Pairwise Comparisons (I) patient (J) patient Mean Difference (I-J) Std. Error Sig.(a) 95% Confidence Interval for Difference(a)

Lower Bound Upper Bound

1 2 -2.833(*) .401 .003 -4.252 -1.415

3 .833 .946 1.000 -2.509 4.176

2 1 2.833(*) .401 .003 1.415 4.252

3 3.667 1.282 .106 -.865 8.199

3 1 -.833 .946 1.000 -4.176 2.509

2 -3.667 1.282 .106 -8.199 .865

Based on estimated marginal means

* The mean difference is significant at the .05 level.

a Adjustment for multiple comparisons: Bonferroni.



Formal reporting:



Mauchly’s test indicated that the assumption of sphericity had been violated (chi-square = 9.44, p <.05), therefore degrees of freedom were corrected using Greenhouse-Geisser estimates of sphericity (epsilon = 0.53). The results show that the pain scores of the three patients differed significantly, F(1.05, 5.25) = 8.21, p <.05. Post hoc tests revealed that although the pain score of Patient2 was significantly higher than that of than Patient1 (p<.001), Patient3's score was not significantly differently from either of the other patients (both p>.05).









Two-Way Repeated Measures ANOVA

i.e. two independent variables:



In a study of the best way to keep fields free of weeds for an entire growing season, a farmer treated test plots in 10 fields with either five different concentrations of weedkiller (independent variable 1) or five different length blasts with a flamethrower (independent variable 2). At the end of they growing season, the number of weeds per square metre were counted. To exclude bias (e.g. pre-existing seedbank in the soil), the following year, the farmer repeated the experiment but this time the treatments the fields received were reversed:



Treatment: Weedkiller Flamethrower

Severity: 1 2 3 4 5 1 2 3 4 5

Field1 10 15 18 22 37 9 13 13 18 22

Field2 10 18 10 42 60 7 14 20 21 32

Field3 7 11 28 31 56 9 13 24 30 35

Field4 9 19 36 45 60 7 14 9 20 25

Field5 15 14 29 33 37 14 13 20 22 29

Field6 14 13 26 26 49 5 12 17 16 33

Field7 9 12 19 37 48 5 15 12 17 24

Field8 9 18 22 31 39 13 13 14 17 17

Field9 12 14 24 28 53 12 13 21 19 22

Field10 7 11 21 23 45 12 14 20 21 29



SPSS Data View:







It's always a good idea to enter descriptive labels for data into the Variable View window, or the output is difficult to interpret:







Analyze: General Linear Model: Repeated Measures

Define Within Subject Factors (remember, "factor" = test or treatment):



Treatment, (2 treatments, weedkiller or flamethrower) (SPSS only allows 8 characters for the name)

Severity (5 different severities):







Click Define and define Within Subject Variables:







As above, there are no post hoc tests for repeated measures ANOVA in SPSS, but via the Options button, we can apply a Bonferroni correction to the probability at which you accept any of the tests:







Output:



Mauchly's Test of Sphericity(b)

Measure: MEASURE_1 Within Subjects Effect Mauchly's W Approx. Chi-Square df Sig. Epsilon

Greenhouse-Geisser Huynh-Feldt Lower-bound

treatmen 1.000 .000 0 . 1.000 1.000 1.000

severity .092 17.685 9 .043 .552 .740 .250

treatmen * severity .425 6.350 9 .712 .747 1.000 .250



The outcome of Mauchly’s test is significant (p <.05) for the severity of treatment, so we need to correct the F-values for this, but not for the treatments themselves.



Tests of Within-Subjects Effects Source

Type III Sum of Squares df Mean Square F Sig.

treatmen Sphericity Assumed 1730.560 1 1730.560 34.078 .000

Greenhouse-Geisser 1730.560 1.000 1730.560 34.078 .000

Huynh-Feldt 1730.560 1.000 1730.560 34.078 .000

Lower-bound 1730.560 1.000 1730.560 34.078 .000

Error(treatmen) Sphericity Assumed 457.040 9 50.782

Greenhouse-Geisser 457.040 9.000 50.782

Huynh-Feldt 457.040 9.000 50.782

Lower-bound 457.040 9.000 50.782

severity Sphericity Assumed 9517.960 4 2379.490 83.488 .000

Greenhouse-Geisser 9517.960 2.209 4309.021 83.488 .000

Huynh-Feldt 9517.960 2.958 3217.666 83.488 .000

Lower-bound 9517.960 1.000 9517.960 83.488 .000

Error(severity) Sphericity Assumed 1026.040 36 28.501

Greenhouse-Geisser 1026.040 19.880 51.613

Huynh-Feldt 1026.040 26.622 38.541

Lower-bound 1026.040 9.000 114.004

treatmen * severity Sphericity Assumed 1495.240 4 373.810 20.730 .000

Greenhouse-Geisser 1495.240 2.989 500.205 20.730 .000

Huynh-Feldt 1495.240 4.000 373.810 20.730 .000

Lower-bound 1495.240 1.000 1495.240 20.730 .001

Error(treatmen*severity) Sphericity Assumed 649.160 36 18.032

Greenhouse-Geisser 649.160 26.903 24.129

Huynh-Feldt 649.160 36.000 18.032

Lower-bound 649.160 9.000 72.129



Since there was no violation of sphericity, we can look at the comparison of the two treatments without any correction. The significance value shows (0.000) that there was a significant difference between the two treatments, but does not tell us which treatments produced this effect.

The output also tells us the effect of the severity of treatments, but remember there was a violation of sphericity here, so we must look at the corrected F-ratios. All of the corrected values are highly significant and so we can use the Greenhouse-Geisser corrected values as these are the most conservative.



Pairwise Comparisons (I) severity (J) severity Mean Difference (I-J) Std. Error Sig.(a) 95% Confidence Interval for Difference(a)

Lower Bound Upper Bound

1 2 -4.200(*) .895 .011 -7.502 -.898

3 -10.400(*) 1.190 .000 -14.790 -6.010

4 -16.200(*) 1.764 .000 -22.709 -9.691

5 -27.850(*) 2.398 .000 -36.698 -19.002

2 1 4.200(*) .895 .011 .898 7.502

3 -6.200(*) 1.521 .028 -11.810 -.590

4 -12.000(*) 1.280 .000 -16.723 -7.277

5 -23.650(*) 2.045 .000 -31.197 -16.103

3 1 10.400(*) 1.190 .000 6.010 14.790

2 6.200(*) 1.521 .028 .590 11.810

4 -5.800 1.690 .075 -12.036 .436

5 -17.450(*) 2.006 .000 -24.852 -10.048

4 1 16.200(*) 1.764 .000 9.691 22.709

2 12.000(*) 1.280 .000 7.277 16.723

3 5.800 1.690 .075 -.436 12.036

5 -11.650(*) 1.551 .000 -17.373 -5.927

5 1 27.850(*) 2.398 .000 19.002 36.698

2 23.650(*) 2.045 .000 16.103 31.197

3 17.450(*) 2.006 .000 10.048 24.852

4 11.650(*) 1.551 .000 5.927 17.373

* The mean difference is significant at the .05 level.

a Adjustment for multiple comparisons: Bonferroni.



This shows that there was only one pair for which there was no significant difference: 40% weedkiller followed by 2 minutes flame thrower, and 2 minutes flame thrower followed by 40% weedkiller. The differences for all the other pairs are significant. It does not matter if the farmer uses weedkiller or a flamethrower, but how much weedkiller and how long a burst of flame does make a difference to weed control.



Formal report:



There was a significant main effect of the type of treatment, F(1, 9) = 34.08, p < .001.

There was a significant main effect of the severity of treatment, F(2.21, 19.88) = 83.49, p <.001.

Monday, January 18, 2010

Commands in Matlab

MATLAB常用命令大全(2009-11-15 00:19:08)


标签:matlab 多项式 位线 条件数 样条函数 校园 分类:学与得

[sizeabs 绝对值、模、字符的ASCII码值







A a

acos 反余弦

acosh 反双曲余弦

acot 反余切

acoth 反双曲余切

acsc 反余割

acsch 反双曲余割

align 启动图形对象几何位置排列工具

all 所有元素非零为真

angle 相角

ans 表达式计算结果的缺省变量名

any 所有元素非全零为真

area 面域图

argnames 函数M文件宗量名

asec 反正割

asech 反双曲正割

asin 反正弦

asinh 反双曲正弦

assignin 向变量赋值

atan 反正切

atan2 四象限反正切

atanh 反双曲正切

autumn 红黄调秋色图阵

axes 创建轴对象的低层指令

axis 控制轴刻度和风格的高层指令





B b



bar 二维直方图

bar3 三维直方图

bar3h 三维水平直方图

barh 二维水平直方图

base2dec X进制转换为十进制

bin2dec 二进制转换为十进制

blanks 创建空格串

bone 蓝色调黑白色图阵

box 框状坐标轴

break while 或for 环中断指令

brighten 亮度控制

C c



capture (3版以前)捕获当前图形

cart2pol 直角坐标变为极或柱坐标

cart2sph 直角坐标变为球坐标

cat 串接成高维数组

caxis 色标尺刻度

cd 指定当前目录

cdedit 启动用户菜单、控件回调函数设计工具

cdf2rdf 复数特征值对角阵转为实数块对角阵

ceil 向正无穷取整

cell 创建元胞数组

cell2struct 元胞数组转换为构架数组

celldisp 显示元胞数组内容

cellplot 元胞数组内部结构图示

char 把数值、符号、内联类转换为字符对象

chi2cdf 分布累计概率函数

chi2inv 分布逆累计概率函数

chi2pdf 分布概率密度函数

chi2rnd 分布随机数发生器

chol Cholesky分解

clabel 等位线标识

cla 清除当前轴

class 获知对象类别或创建对象

clc 清除指令窗

clear 清除内存变量和函数

clf 清除图对象

clock 时钟

colorcube 三浓淡多彩交叉色图矩阵

colordef 设置色彩缺省值

colormap 色图

colspace 列空间的基

close 关闭指定窗口

colperm 列排序置换向量

comet 彗星状轨迹图

comet3 三维彗星轨迹图

compass 射线图

compose 求复合函数

cond (逆)条件数

condeig 计算特征值、特征向量同时给出条件数

condest 范 -1条件数估计

conj 复数共轭

contour 等位线

contourf 填色等位线

contour3 三维等位线

contourslice 四维切片等位线图

conv 多项式乘、卷积

cool 青紫调冷色图

copper 古铜调色图

cos 余弦

cosh 双曲余弦

cot 余切

coth 双曲余切

cplxpair 复数共轭成对排列

csc 余割

csch 双曲余割

cumsum 元素累计和

cumtrapz 累计梯形积分

cylinder 创建圆柱

D d



dblquad 二重数值积分

deal 分配宗量

deblank 删去串尾部的空格符

dec2base 十进制转换为X进制

dec2bin 十进制转换为二进制

dec2hex 十进制转换为十六进制

deconv 多项式除、解卷

delaunay Delaunay 三角剖分

del2 离散Laplacian差分

demo Matlab演示

det 行列式

diag 矩阵对角元素提取、创建对角阵

diary Matlab指令窗文本内容记录

diff 数值差分、符号微分

digits 符号计算中设置符号数值的精度

dir 目录列表

disp 显示数组

display 显示对象内容的重载函数

dlinmod 离散系统的线性化模型

dmperm 矩阵Dulmage-Mendelsohn 分解

dos 执行DOS 指令并返回结果

double 把其他类型对象转换为双精度数值

drawnow 更新事件队列强迫Matlab刷新屏幕

dsolve 符号计算解微分方程





E e



echo M文件被执行指令的显示

edit 启动M文件编辑器

eig 求特征值和特征向量

eigs 求指定的几个特征值

end 控制流FOR等结构体的结尾元素下标

eps 浮点相对精度

error 显示出错信息并中断执行

errortrap 错误发生后程序是否继续执行的控制

erf 误差函数

erfc 误差补函数

erfcx 刻度误差补函数

erfinv 逆误差函数

errorbar 带误差限的曲线图

etreeplot 画消去树

串演算指令

in 跨空间串演算指令

exist 检查变量或函数是否已定义

exit 退出Matlab环境

exp 指数函数

expand 符号计算中的展开操作

expint 指数积分函数

expm 常用矩阵指数函数

expm1 Pade法求矩阵指数

expm2 Taylor法求矩阵指数

expm3 特征值分解法求矩阵指数

eye 单位阵

ezcontour 画等位线的简捷指令

ezcontourf 画填色等位线的简捷指令

ezgraph3 画表面图的通用简捷指令

ezmesh 画网线图的简捷指令

ezmeshc 画带等位线的网线图的简捷指令

ezplot 画二维曲线的简捷指令

ezplot3 画三维曲线的简捷指令

ezpolar 画极坐标图的简捷指令

ezsurf 画表面图的简捷指令

ezsurfc 画带等位线的表面图的简捷指令

F f



factor 符号计算的因式分解

feather 羽毛图

feedback 反馈连接

f 执行由串指定的函数

fft 离散Fourier变换

fft2 二维离散Fourier变换

fftn 高维离散Fourier变换

fftshift 直流分量对中的谱

fieldnames 构架域名

figure 创建图形窗

fill3 三维多边形填色图

find 寻找非零元素下标

findobj 寻找具有指定属性的对象图柄

findstr 寻找短串的起始字符下标

findsym 机器确定内存中的符号变量

finverse 符号计算中求反函数

fix 向零取整

flag 红白蓝黑交错色图阵

fliplr 矩阵的左右翻转

flipud 矩阵的上下翻转

flipdim 矩阵沿指定维翻转

floor 向负无穷取整

flops 浮点运算次数

flow Matlab提供的演示数据

fmin 求单变量非线性函数极小值点(旧版)

fminbnd 求单变量非线性函数极小值点

fmins 单纯形法求多变量函数极小值点(旧版)

fminunc 拟牛顿法求多变量函数极小值点

fminsearch 单纯形法求多变量函数极小值点

fnder 对样条函数求导

fnint 利用样条函数求积分

fnval 计算样条函数区间内任意一点的值

fnplt 绘制样条函数图形

fopen 打开外部文件

for 构成for环用

format 设置输出格式

fourier Fourier 变换

fplot 返函绘图指令

fprintf 设置显示格式

fread 从文件读二进制数据

fsolve 求多元函数的零点

full 把稀疏矩阵转换为非稀疏阵

funm 计算一般矩阵函数

funtool 函数计算器图形用户界面

fzero 求单变量非线性函数的零点





G g



gamma 函数

gammainc 不完全 函数

gammaln 函数的对数

gca 获得当前轴句柄

gcbo 获得正执行"回调"的对象句柄

gcf 获得当前图对象句柄

gco 获得当前对象句柄

geomean 几何平均值

get 获知对象属性

getfield 获知构架数组的域

getframe 获取影片的帧画面

ginput 从图形窗获取数据

global 定义全局变量

gplot 依图论法则画图

gradient 近似梯度

gray 黑白灰度

grid 画分格线

griddata 规则化数据和曲面拟合

gtext 由鼠标放置注释文字

guide 启动图形用户界面交互设计工具





H h



harmmean 调和平均值

help 在线帮助

helpwin 交互式在线帮助

helpdesk 打开超文本形式用户指南

hex2dec 十六进制转换为十进制

hex2num 十六进制转换为浮点数

hidden 透视和消隐开关

hilb Hilbert矩阵

hist 频数计算或频数直方图

histc 端点定位频数直方图

histfit 带正态拟合的频数直方图

hold 当前图上重画的切换开关

horner 分解成嵌套形式

hot 黑红黄白色图

hsv 饱和色图





I i



if-else-elseif 条件分支结构

ifft 离散Fourier反变换

ifft2 二维离散Fourier反变换

ifftn 高维离散Fourier反变换

ifftshift 直流分量对中的谱的反操作

ifourier Fourier反变换

i, j 缺省的"虚单元"变量

ilaplace Laplace反变换

imag 复数虚部

image 显示图象

imagesc 显示亮度图象

imfinfo 获取图形文件信息

imread 从文件读取图象

imwrite 把图象写成文件

ind2sub 单下标转变为多下标

inf 无穷大

info MathWorks公司网点地址

inline 构造内联函数对象

inmem 列出内存中的函数名

input 提示用户输入

inputname 输入宗量名

int 符号积分

int2str 把整数数组转换为串数组

interp1 一维插值

interp2 二维插值

interp3 三维插值

interpn N维插值

interpft 利用FFT插值

intro Matlab自带的入门引导

inv 求矩阵逆

invhilb Hilbert矩阵的准确逆

ipermute 广义反转置

isa 检测是否给定类的对象

ischar 若是字符串则为真

isequal 若两数组相同则为真

isempty 若是空阵则为真

isfinite 若全部元素都有限则为真

isfield 若是构架域则为真

isglobal 若是全局变量则为真

ishandle 若是图形句柄则为真

ishold 若当前图形处于保留状态则为真

isieee 若计算机执行IEEE规则则为真

isinf 若是无穷数据则为真

isletter 若是英文字母则为真

islogical 若是逻辑数组则为真

ismember 检查是否属于指定集

isnan 若是非数则为真

isnumeric 若是数值数组则为真

isobject 若是对象则为真

isprime 若是质数则为真

isreal 若是实数则为真

isspace 若是空格则为真

issparse 若是稀疏矩阵则为真

isstruct 若是构架则为真

isstudent 若是Matlab学生版则为真

iztrans 符号计算Z反变换





J j , K k



jacobian 符号计算中求Jacobian 矩阵

jet 蓝头红尾饱和色

jordan 符号计算中获得 Jordan标准型

keyboard 键盘获得控制权

kron Kronecker乘法规则产生的数组





L l



laplace Laplace变换

lasterr 显示最新出错信息

lastwarn 显示最新警告信息

leastsq 解非线性最小二乘问题(旧版)

legend 图形图例

lighting 照明模式

line 创建线对象

lines 采用plot 画线色

linmod 获连续系统的线性化模型

linmod2 获连续系统的线性化精良模型

linspace 线性等分向量

ln 矩阵自然对数

load 从MAT文件读取变量

log 自然对数

log10 常用对数

log2 底为2的对数

loglog 双对数刻度图形

logm 矩阵对数

logspace 对数分度向量

lookfor 按关键字搜索M文件

lower 转换为小写字母

lsqnonlin 解非线性最小二乘问题

lu LU分解





M m



mad 平均绝对值偏差

magic 魔方阵

maple &nb, sp; 运作 Maple格式指令

mat2str 把数值数组转换成输入形态串数组

material 材料反射模式

max 找向量中最大元素

mbuild 产生EXE文件编译环境的预设置指令

mcc 创建MEX或EXE文件的编译指令

mean 求向量元素的平均值

median 求中位数

menuedit 启动设计用户菜单的交互式编辑工具

mesh 网线图

meshz 垂帘网线图

meshgrid 产生"格点"矩阵

methods 获知对指定类定义的所有方法函数

mex 产生MEX文件编译环境的预设置指令

mfunlis 能被mfun计算的MAPLE经典函数列表

mhelp 引出 Maple的在线帮助

min 找向量中最小元素

mkdir 创建目录

mkpp 逐段多项式数据的明晰化

mod 模运算

more 指令窗中内容的分页显示

movie 放映影片动画

moviein 影片帧画面的内存预置

mtaylor 符号计算多变量Taylor级数展开





N n



ndims 求数组维数

NaN 非数(预定义)变量

nargchk 输入宗量数验证

nargin 函数输入宗量数

nargout 函数输出宗量数

ndgrid 产生高维格点矩阵

newplot 准备新的缺省图、轴

nextpow2 取最接近的较大2次幂

nnz 矩阵的非零元素总数

nonzeros 矩阵的非零元素

norm 矩阵或向量范数

normcdf 正态分布累计概率密度函数

normest 估计矩阵2范数

norminv 正态分布逆累计概率密度函数

normpdf 正态分布概率密度函数

normrnd 正态随机数发生器

notebook 启动Matlab和Word的集成环境

null 零空间

num2str 把非整数数组转换为串

numden 获取最小公分母和相应的分子表达式

nzmax 指定存放非零元素所需内存



O o



ode1 非Stiff 微分方程变步长解算器

ode15s Stiff 微分方程变步长解算器

ode23t 适度Stiff 微分方程解算器

ode23tb Stiff 微分方程解算器

ode45 非Stiff 微分方程变步长解算器

odefile ODE 文件模板

odeget 获知ODE 选项设置参数

odephas2 ODE 输出函数的二维相平面图

odephas3 ODE 输出函数的三维相空间图

odeplot ODE 输出函数的时间轨迹图

odeprint 在Matlab指令窗显示结果

odeset 创建或改写 ODE选项构架参数值

ones 全1数组

optimset 创建或改写优化泛函指令的选项参数值

orient 设定图形的排放方式

orth 值空间正交化





P p



pack 收集Matlab内存碎块扩大内存

pagedlg 调出图形排版对话框

patch 创建块对象

path 设置Matlab搜索路径的指令

pathtool 搜索路径管理器

pause 暂停

pcode 创建预解译P码文件

pcolor 伪彩图

peaks Matlab提供的典型三维曲面

permute 广义转置

pi (预定义变量)圆周率

pie 二维饼图

pie3 三维饼图

pink 粉红色图矩阵

pinv 伪逆

plot 平面线图

plot3 三维线图

plotmatrix 矩阵的散点图

plotyy 双纵坐标图

poissinv 泊松分布逆累计概率分布函数

poissrnd 泊松分布随机数发生器

pol2cart 极或柱坐标变为直角坐标

polar 极坐标图

poly 矩阵的特征多项式、根集对应的多项式

poly2str 以习惯方式显示多项式

poly2sym 双精度多项式系数转变为向量符号多项式

polyder 多项式导数

polyfit 数据的多项式拟合

polyval 计算多项式的值

polyvalm 计算矩阵多项式

pow2 2的幂

ppval 计算分段多项式

pretty 以习惯方式显示符号表达式

print 打印图形或SIMULINK模型

printsys 以习惯方式显示有理分式

prism 光谱色图矩阵

procread 向MAPLE输送计算程序

profile 函数文件性能评估器

propedit 图形对象属性编辑器

pwd 显示当前工作目录





Q q



quad 低阶法计算数值积分

quad8 高阶法计算数值积分(QUADL)

quit 推出Matlab 环境

quiver 二维方向箭头图

quiver3 三维方向箭头图





R r



rand 产生均匀分布随机数

randn 产生正态分布随机数

randperm 随机置换向量

range 样本极差

rank 矩阵的秩

rats 有理输出

rcond 矩阵倒条件数估计

real 复数的实部

reallog 在实数域内计算自然对数

realpow 在实数域内计算乘方

realsqrt 在实数域内计算平方根

realmax 最大正浮点数

realmin 最小正浮点数

rectangle 画"长方框"

rem 求余数

repmat 铺放模块数组

reshape 改变数组维数、大小

residue 部分分式展开

return 返回

ribbon 把二维曲线画成三维彩带图

rmfield 删去构架的域

roots 求多项式的根

rose 数扇形图

rot90 矩阵旋转90度

rotate 指定的原点和方向旋转

rotate3d 启动三维图形视角的交互设置功能

round 向最近整数圆整

rref 简化矩阵为梯形形式

rsf2csf 实数块对角阵转为复数特征值对角阵

rsums Riemann和





S s



save 把内存变量保存为文件

scatter 散点图

scatter3 三维散点图

sec 正割

sech 双曲正割

semilogx X轴对数刻度坐标图

semilogy Y轴对数刻度坐标图

series 串联连接

set 设置图形对象属性

setfield 设置构架数组的域

setstr 将ASCII码转换为字符的旧版指令

sign 根据符号取值函数

signum 符号计算中的符号取值函数

sim 运行SIMULINK模型

simget 获取SIMULINK模型设置的仿真参数

simple 寻找最短形式的符号解

simplify 符号计算中进行简化操作

simset 对SIMULINK模型的仿真参数进行设置

simulink 启动SIMULINK模块库浏览器

sin 正弦

sinh 双曲正弦

size 矩阵的大小

slice 立体切片图

solve 求代数方程的符号解

spalloc 为非零元素配置内存

sparse 创建稀疏矩阵

spconvert 把外部数据转换为稀疏矩阵

spdiags 稀疏对角阵

spfun 求非零元素的函数值

sph2cart 球坐标变为直角坐标

sphere 产生球面

spinmap 色图彩色的周期变化

spline 样条插值

spones 用1置换非零元素

sprandsym 稀疏随机对称阵

sprank 结构秩

spring 紫黄调春色图

sprintf 把格式数据写成串

spy 画稀疏结构图

sqrt 平方根

sqrtm 方根矩阵

squeeze 删去大小为1的"孤维"

sscanf 按指定格式读串

stairs 阶梯图

std 标准差

stem 二维杆图

step 阶跃响应指令

str2double 串转换为双精度值

str2mat 创建多行串数组

str2num 串转换为数

strcat 接成长串

strcmp 串比较

strjust 串对齐

strmatch 搜索指定串

strncmp 串中前若干字符比较

strrep 串替换

strtok 寻找第一间隔符前的内容

struct 创建构架数组

struct2cell 把构架转换为元胞数组

strvcat 创建多行串数组

sub2ind 多下标转换为单下标

subexpr 通过子表达式重写符号对象

subplot 创建子图

subs 符号计算中的符号变量置换

subspace 两子空间夹角

sum 元素和

summer 绿黄调夏色图

superiorto 设定优先级

surf 三维着色表面图

surface 创建面对象

surfc 带等位线的表面图

surfl 带光照的三维表面图

surfnorm 空间表面的法线

svd 奇异值分解

svds 求指定的若干奇异值

switch-case-otherwise 多分支结构

sym2poly 符号多项式转变为双精度多项式系数向量

symmmd 对称最小度排序

symrcm 反向Cuthill-McKee排序

syms 创建多个符号对象





T t



tan 正切

tanh 双曲正切

taylortool 进行Taylor逼近分析的交互界面

text 文字注释

tf 创建传递函数对象

tic 启动计时器

title 图名

toc 关闭计时器

trapz 梯形法数值积分

treelayout 展开树、林

treeplot 画树图

tril 下三角阵

trim 求系统平衡点

trimesh 不规则格点网线图

trisurf 不规则格点表面图 triu 上三角阵 try-catch 控制流中的Try-catch结构 type 显示M文件

U u

uicontextmenu 创建现场菜单

uicontrol 创建用户控件

uimenu 创建用户菜单

unmkpp 逐段多项式数据的反明晰化

unwrap 自然态相角

upper 转换为大写字母





V v



var 方差

varargin 变长度输入宗量

varargout 变长度输出宗量

vectorize 使串表达式或内联函数适于数组运算

ver 版本信息的获取

view 三维图形的视角控制

voronoi Voronoi多边形

vpa 任意精度(符号类)数值





W w



warning 显示警告信息

what 列出当前目录上的文件

whatsnew 显示Matlab中 Readme文件的内容

which 确定函数、文件的位置

while 控制流中的While环结构

white 全白色图矩阵

whitebg 指定轴的背景色

who 列出内存中的变量名

whos 列出内存中变量的详细信息

winter 蓝绿调冬色图

workspace 启动内存浏览器





X x , Y y , Z z



xlabel X轴名

xor 或非逻辑

yesinput 智能输入指令

ylabel Y轴名

zeros 全零数组

zlabel Z轴名

zoom 图形的变焦放大和缩小

ztrans 符号计算Z变换

Friday, January 15, 2010

Travelling in Norway,May,2009


Snow Scenery in Aberdeen


Autumn scenery in Scotland


Chinese treasures in The British Museum


Scene in Aberdeen


How Jave get data from EXCELL-02

Java解释Excel数据(jxl.jar包的使用)
关键字: java excel jxl.jar
jxl.jar 包
下载地址:
http://www.andykhan.com/jexcelapi/
真实下载地址:
http://www.andykhan.com/jexcelapi/download.html

网站上对它的特征有如下描述:
● 支持Excel 95-2000的所有版本
● 生成Excel 2000标准格式
● 支持字体、数字、日期操作
● 能够修饰单元格属性
● 支持图像和图表
应该说以上功能已经能够大致满足我们的需要。最关键的是这套API是纯Java的,并不依赖Windows系统,即使运行在Linux下,它同样能够正确的处理Excel文件。另外需要说明的是,这套API对图形和图表的支持很有限,而且仅仅识别PNG格式。
搭建环境
将下载后的文件解包,得到jxl.jar,放入classpath,安装就完成了。

基本操作

一、创建文件
拟生成一个名为“测试数据.xls”的Excel文件,其中第一个工作表被命名为“第一页”,大致效果如下:

Java代码
1./*
2. * Created on Dec 30, 2007
3. *
4. * To change the template for this generated file go to
5. * Window>Preferences>Java>Code Generation>Code and Comments
6. */
7.package JExcelTest.standard;
8.
9.import java.io.*;
10.import jxl.*;
11.import jxl.write.*;
12.
13./**
14. * @author Ken
15. *
16. * To change the template for this generated type comment go to
17. * Window>Preferences>Java>Code Generation>Code and Comments
18. */
19.public class CreateXLS {
20.
21. public static void main(String[] args) {
22. try {
23. //open file.
24. WritableWorkbook book = Workbook.createWorkbook(new File("d:/Test.xls"));
25.
26. //create Sheet named "Sheet_1". 0 means this is 1st page.
27. WritableSheet sheet = book.createSheet("Sheet_1", 0);
28.
29. //define cell column and row in Label Constructor, and cell content write "test".
30. //cell is 1st-Column,1st-Row. value is "test".
31. Label label = new Label(0, 0, "test");
32. //add defined cell above to sheet instance.
33. sheet.addCell(label);
34.
35. //create cell using add numeric. WARN:necessarily use integrated package-path, otherwise will be throws path-error.
36. //cell is 2nd-Column, 1st-Row. value is 789.123.
37. jxl.write.Number number = new jxl.write.Number(1, 0, 789.123);
38. //add defined cell above to sheet instance.
39. sheet.addCell(number);
40.
41. //add defined all cell above to case.
42. book.write();
43. //close file case.
44. book.close();
45. } catch (Exception e) {
46. e.printStackTrace();
47. }
48. }
49.}
/*
* Created on Dec 30, 2007
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
package JExcelTest.standard;

import java.io.*;
import jxl.*;
import jxl.write.*;

/**
* @author Ken
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public class CreateXLS {

public static void main(String[] args) {
try {
//open file.
WritableWorkbook book = Workbook.createWorkbook(new File("d:/Test.xls"));

//create Sheet named "Sheet_1". 0 means this is 1st page.
WritableSheet sheet = book.createSheet("Sheet_1", 0);

//define cell column and row in Label Constructor, and cell content write "test".
//cell is 1st-Column,1st-Row. value is "test".
Label label = new Label(0, 0, "test");
//add defined cell above to sheet instance.
sheet.addCell(label);

//create cell using add numeric. WARN:necessarily use integrated package-path, otherwise will be throws path-error.
//cell is 2nd-Column, 1st-Row. value is 789.123.
jxl.write.Number number = new jxl.write.Number(1, 0, 789.123);
//add defined cell above to sheet instance.
sheet.addCell(number);

//add defined all cell above to case.
book.write();
//close file case.
book.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
编译执行后,会在当前位置产生一个Excel文件。

二、读取文件
以刚才我们创建的Excel文件为例,做一个简单的读取操作,程序代码如下:

Java代码
1./*
2. * Created on Dec 30, 2007
3. *
4. * To change the template for this generated file go to
5. * Window>Preferences>Java>Code Generation>Code and Comments
6. */
7.package JExcelTest.standard;
8.
9.import java.io.*;
10.import jxl.*;
11.
12./**
13. * @author Ken
14. *
15. * To change the template for this generated type comment go to
16. * Window>Preferences>Java>Code Generation>Code and Comments
17. */
18.public class ReadXLS {
19.
20. public static void main(String[] args) {
21. try {
22. Workbook book = Workbook.getWorkbook(new File("d:/Test.xls"));
23. //get a Sheet object.
24. Sheet sheet = book.getSheet(0);
25. //get 1st-Column,1st-Row content.
26. Cell cell = sheet.getCell(0, 0);
27. String result = cell.getContents();
28. System.out.println(result);
29. book.close();
30. } catch (Exception e) {
31. e.printStackTrace();
32. }
33.
34. }
35.}
/*
* Created on Dec 30, 2007
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
package JExcelTest.standard;

import java.io.*;
import jxl.*;

/**
* @author Ken
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public class ReadXLS {

public static void main(String[] args) {
try {
Workbook book = Workbook.getWorkbook(new File("d:/Test.xls"));
//get a Sheet object.
Sheet sheet = book.getSheet(0);
//get 1st-Column,1st-Row content.
Cell cell = sheet.getCell(0, 0);
String result = cell.getContents();
System.out.println(result);
book.close();
} catch (Exception e) {
e.printStackTrace();
}

}
}
程序执行结果:test

三、修改文件
利用jExcelAPI可以修改已有的Excel文件,修改Excel文件的时候,除了打开文件的方式不同之外,其他操作和创建Excel是一样的。下面的例子是在我们已经生成的Excel文件中添加一个工作表:
修改Excel的类,添加一个工作表

Java代码
1./*
2. * Created on Dec 30, 2007
3. *
4. * To change the template for this generated file go to
5. * Window>Preferences>Java>Code Generation>Code and Comments
6. */
7.package JExcelTest.standard;
8.
9.import java.io.*;
10.import jxl.*;
11.import jxl.write.*;
12.
13./**
14. * @author Ken
15. *
16. * To change the template for this generated type comment go to
17. * Window>Preferences>Java>Code Generation>Code and Comments
18. */
19.public class UpdateXLS {
20.
21. public static void main(String[] args) {
22. try {
23. //get file.
24. Workbook wb = Workbook.getWorkbook(new File("d:/Test.xls"));
25. //open a copy file(new file), then write content with same content with Test.xls.
26. WritableWorkbook book =
27. Workbook.createWorkbook(new File("d:/Test.xls"), wb);
28. //add a Sheet.
29. WritableSheet sheet = book.createSheet("Sheet_2", 1);
30. sheet.addCell(new Label(0, 0, "test2"));
31. book.write();
32. book.close();
33. } catch (Exception e) {
34. e.printStackTrace();
35. }
36. }
37.}
/*
* Created on Dec 30, 2007
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
package JExcelTest.standard;

import java.io.*;
import jxl.*;
import jxl.write.*;

/**
* @author Ken
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public class UpdateXLS {

public static void main(String[] args) {
try {
//get file.
Workbook wb = Workbook.getWorkbook(new File("d:/Test.xls"));
//open a copy file(new file), then write content with same content with Test.xls.
WritableWorkbook book =
Workbook.createWorkbook(new File("d:/Test.xls"), wb);
//add a Sheet.
WritableSheet sheet = book.createSheet("Sheet_2", 1);
sheet.addCell(new Label(0, 0, "test2"));
book.write();
book.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

高级操作

一、 数据格式化
在Excel中不涉及复杂的数据类型,能够比较好的处理字串、数字和日期已经能够满足一般的应用。
字串格式化
字符串的格式化涉及到的是字体、粗细、字号等元素,这些功能主要由WritableFont和WritableCellFormat类来负责。假设我们在生成一个含有字串的单元格时,使用如下语句,为方便叙述,我们为每一行命令加了编号:
WritableFont font1= new WritableFont(WritableFont.TIMES,16,WritableFont.BOLD);

//设置字体格式为excel支持的格式
WritableFont font3=new WritableFont(WritableFont.createFont("楷体_GB2312"),12,WritableFont.NO_BOLD );
① WritableCellFormat format1=new WritableCellFormat(font1);
② Label label=new Label(0,0,”data 4 test”,format1)
③ 其中
I.指定了字串格式:字体为TIMES,字号16,加粗显示。WritableFont有非常丰富的构造子,供不同情况下使用,jExcelAPI的java-doc中有详细列表,这里不再列出。
II.处代码使用了WritableCellFormat类,这个类非常重要,通过它可以指定单元格的各种属性,后面的单元格格式化中会有更多描述。
III.处使用了Label类的构造子,指定了字串被赋予那种格式。 在WritableCellFormat类中,还有一个很重要的方法是指定数据的对齐方式,比如针对我们上面的实例,可以指定:
//把水平对齐方式指定为居中
format1.setAlignment(jxl.format.Alignment.CENTRE);
//把垂直对齐方式指定为居中
format1.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
//设置自动换行
format1.setWrap(true);

二、单元格操作
Excel中很重要的一部分是对单元格的操作,比如行高、列宽、单元格合并等,所幸jExcelAPI提供了这些支持。这些操作相对比较简单,下面只介绍一下相关的API。
1、 合并单元格
WritableSheet.mergeCells(int m,int n,int p,int q);
作用是从(m,n)到(p,q)的单元格全部合并,比如:
WritableSheet sheet=book.createSheet(“第一页”,0);
//合并第一列第一行到第六列第一行的所有单元格
sheet.mergeCells(0,0,5,0);
合并既可以是横向的,也可以是纵向的。合并后的单元格不能再次进行合并,否则会触发异常。
2、 行高和列宽
WritableSheet.setRowView(int i,int height);
作用是指定第i+1行的高度,比如:
//将第一行的高度设为200
sheet.setRowView(0,200);
WritableSheet.setColumnView(int i,int width);
作用是指定第i+1列的宽度,比如:
//将第一列的宽度设为30
sheet.setColumnView(0,30);
三、操作图片

Java代码
1.public static void write()throws Exception{
2. WritableWorkbook wwb=Workbook.createWorkbook(new File("c:/1.xls"));
3. WritableSheet ws=wwb.createSheet("Test Sheet 1",0);
4. File file=new File("C:\\jbproject\\PVS\\WebRoot\\weekhit\\1109496996281.png");
5. WritableImage image=new WritableImage(1, 4, 6, 18,file);
6. ws.addImage(image);
7. wwb.write();
8. wwb.close();
9. }
public static void write()throws Exception{
WritableWorkbook wwb=Workbook.createWorkbook(new File("c:/1.xls"));
WritableSheet ws=wwb.createSheet("Test Sheet 1",0);
File file=new File("C:\\jbproject\\PVS\\WebRoot\\weekhit\\1109496996281.png");
WritableImage image=new WritableImage(1, 4, 6, 18,file);
ws.addImage(image);
wwb.write();
wwb.close();
}很简单和插入单元格的方式一样,不过就是参数多了些,WritableImage这个类继承了Draw,上面只是他构造方法的一种,最后一个参数不用了说了,前面四个参数的类型都是double,依次是 x, y, width, height,注意,这里的宽和高可不是图片的宽和高,而是图片所要占的单位格的个数,因为继承的Draw所以他的类型必须是double,具体里面怎么实现的我还没细看:)因为着急赶活,先完成功能,其他的以后有时间慢慢研究。以后会继续写出在使用中的心得给大家。

读:
读的时候是这样的一个思路,先用一个输入流(InputStream)得到Excel文件,然后用jxl中的Workbook得到工作薄,用Sheet从工作薄中得到工作表,用Cell得到工作表中得某个单元格.
InputStream->Workbook->Sheet->Cell,就得到了excel文件中的单元格

Java代码
1.String path="c:\\excel.xls";//Excel文件URL
2.InputStream is = new FileInputStream(path);//写入到FileInputStream
3.jxl.Workbook wb = Workbook.getWorkbook(is); //得到工作薄
4.jxl.Sheet st = wb.getSheet(0);//得到工作薄中的第一个工作表
5.Cell cell=st.getCell(0,0);//得到工作表的第一个单元格,即A1
6.String content=cell.getContents();//getContents()将Cell中的字符转为字符串
7.wb.close();//关闭工作薄
8.is.close();//关闭输入流
String path="c:\\excel.xls";//Excel文件URL
InputStream is = new FileInputStream(path);//写入到FileInputStream
jxl.Workbook wb = Workbook.getWorkbook(is); //得到工作薄
jxl.Sheet st = wb.getSheet(0);//得到工作薄中的第一个工作表
Cell cell=st.getCell(0,0);//得到工作表的第一个单元格,即A1
String content=cell.getContents();//getContents()将Cell中的字符转为字符串
wb.close();//关闭工作薄
is.close();//关闭输入流
我们可以通过Sheet的getCell(x,y)方法得到任意一个单元格,x,y和excel中的坐标对应.
例如A1对应(0,0),A2对应(0,1),D3对应(3,2).Excel中坐标从A,1开始,jxl中全部是从0开始.
还可以通过Sheet的getRows(),getColumns()方法得到行数列数,并用于循环控制,输出一个sheet中的所有内容.

写:
往Excel中写入内容主要是用jxl.write包中的类.
思路是这样的:
OutputStream<-WritableWorkbook<-WritableSheet<-Label
这里面Label代表的是写入Sheet的Cell位置及内容.

Java代码
1.OutputStream os=new FileOutputStream("c:\\test.xls");//输出的Excel文件URL
2.WritableWorkbook wwb = Workbook.createWorkbook(os);//创建可写工作薄
3.WritableSheet ws = wwb.createSheet("sheet1", 0);//创建可写工作表
4.Label labelCF=new Label(0, 0, "hello");//创建写入位置和内容
5.ws.addCell(labelCF);//将Label写入sheet中
6.Label的构造函数Label(int x, int y,String aString)xy意同读的时候的xy,aString是写入的内容.
7.WritableFont wf = new WritableFont(WritableFont.TIMES, 12, WritableFont.BOLD, false);//设置写入字体
8.WritableCellFormat wcfF = new WritableCellFormat(wf);//设置CellFormat
9.Label labelCF=new Label(0, 0, "hello");//创建写入位置,内容和格式
10.Label的另一构造函数Label(int c, int r, String cont, CellFormat st)可以对写入内容进行格式化,设置字体及其它的属性.
11.现在可以写了
12.wwb.write();
13.写完后关闭
14.wwb.close();
15.输出流也关闭吧
16.os.close;
OutputStream os=new FileOutputStream("c:\\test.xls");//输出的Excel文件URL
WritableWorkbook wwb = Workbook.createWorkbook(os);//创建可写工作薄
WritableSheet ws = wwb.createSheet("sheet1", 0);//创建可写工作表
Label labelCF=new Label(0, 0, "hello");//创建写入位置和内容
ws.addCell(labelCF);//将Label写入sheet中
Label的构造函数Label(int x, int y,String aString)xy意同读的时候的xy,aString是写入的内容.
WritableFont wf = new WritableFont(WritableFont.TIMES, 12, WritableFont.BOLD, false);//设置写入字体
WritableCellFormat wcfF = new WritableCellFormat(wf);//设置CellFormat
Label labelCF=new Label(0, 0, "hello");//创建写入位置,内容和格式
Label的另一构造函数Label(int c, int r, String cont, CellFormat st)可以对写入内容进行格式化,设置字体及其它的属性.
现在可以写了
wwb.write();
写完后关闭
wwb.close();
输出流也关闭吧
os.close;
OK,只要把读和写结合起来,就可以在N个Excel中读取数据写入你希望的Excel新表中,还是比较方便的.

下面是程序一例:

Java代码
1.sql = "select * from tablename";
2.rs = stmt.executeQuery(sql);
3.
4.//新建Excel文件
5.String filePath=request.getRealPath("aaa.xls");
6.File myFilePath=new File(filePath);
7.if(!myFilePath.exists())
8.myFilePath.createNewFile();
9.FileWriter resultFile=new FileWriter(myFilePath);
10.PrintWriter myFile=new PrintWriter(resultFile);
11.resultFile.close();
12.
13. //用JXL向新建的文件中添加内容
14. OutputStream outf = new FileOutputStream(filePath);
15. jxl.write.WritableWorkbook wwb = Workbook.createWorkbook(outf);
16. jxl.write.WritableSheet ws = wwb.createSheet("sheettest", 0);
17.
18.int i=0;
19. int j=0;
20.
21.for (int k = 0; k < rs.getMetaData().getColumnCount(); k++) {
22. ws.addCell(new Label(k,0,rs.getMetaData().getColumnName(k+1)));
23.}
24.
25.while(rs.next()){
26. out.println(rs.getMetaData().getColumnCount());
27.
28.for (int k = 0; k < rs.getMetaData().getColumnCount(); k++) {
29.ws.addCell(new Label(k,j+i+1,rs.getString(k+1)));
30. }
31.
32. i++;
33.}
34.wwb.write();
35. wwb.close();
36.}catch(Exception e){e.printStackTrace();}
37.finally{
38.
39.rs.close();
40.conn.close();
41.}
42.
43.response.sendRedirect("aaa.xls");

How Java get data from Excell?

ava Excel API既可以从本地文件系统的一个文件(.xls),也可以从输入流中读取Excel数据表。读取Excel数据表的第一步是创建Workbook(术 语:工作薄),下面的代码片段举例说明了应该如何操作:

import java.io.*;

import jxl.*;

… … … …

try

{

//从输入流创建Workbook

InputStream is = new FileInputStream(sourcefile);

jxl.Workbook workbook = Workbook.getWorkbook(is);

}

catch (Exception e)

{

e.printStackTrace();

}




上面是从输入流中读取Excel数据表,下面则是从本地文件中读取:

////直接从本地文件创建Workbook

Workbook workbook = Workbook.getWorkbook(new File(excelfile));


一旦创建了Workbook,我们就可以通过它来访问Excel Sheet(术语:工作表)。参考下面的代码片段:

//获取第一张Sheet表

Sheet rs = workbook.getSheet(0);




我们既可能通过Sheet的名称来访问它,也可以通过下标来访问它。如果通过下标来访问的话,要注意的一点是下标从0开始,就像数组一样。

一旦得到了Sheet,我们就可以通过它来访问Excel Cell(术语:单元格)。参考下面的代码片段:

//获取第一行,第一列的值

Cell c00 = rs.getCell(0, 0);

String strc00 = c00.getContents();

//获取第一行,第二列的值

Cell c10 = rs.getCell(1, 0);

String strc10 = c10.getContents();



//获取第二行,第二列的值

Cell c11 = rs.getCell(1, 1);

String strc11 = c11.getContents();



System.out.println("Cell(0, 0)" + " value : " + strc00 + "; type : " + c00.getType());

System.out.println("Cell(1, 0)" + " value : " + strc10 + "; type : " + c10.getType());

System.out.println("Cell(1, 1)" + " value : " + strc11 + "; type : " + c11.getType());




如果仅仅是取得Cell的 值,我们可以方便地通过getContents()方法,它可以将任何类型的Cell值都作为一个字符串返回。示例代码中Cell(0, 0)是文本型,Cell(1, 0)是数字型,Cell(1,1)是日期型,通过getContents(),三种类型的返回值都是字符型。

如果有需要知道Cell内容的确切类型,API也提供了一系列的方法。参考下面的代码片段:

String strc00 = null;

double strc10 = 0.00;

Date strc11 = null;



Cell c00 = rs.getCell(0, 0);

Cell c10 = rs.getCell(1, 0);

Cell c11 = rs.getCell(1, 1);



if(c00.getType() == CellType.LABEL)

{

LabelCell labelc00 = (LabelCell)c00;

strc00 = labelc00.getString();

}

if(c10.getType() == CellType.NUMBER)

{

NmberCell numc10 = (NumberCell)c10;

strc10 = numc10.getValue();

}

if(c11.getType() == CellType.DATE)

{

DateCell datec11 = (DateCell)c11;

strc11 = datec11.getDate();

}



System.out.println("Cell(0, 0)" + " value : " + strc00 + "; type : " + c00.getType());

System.out.println("Cell(1, 0)" + " value : " + strc10 + "; type : " + c10.getType());

System.out.println("Cell(1, 1)" + " value : " + strc11 + "; type : " + c11.getType());




在得到Cell对象后,通过 getType()方法可以获得该单元格的类型,然后与API提供的基本类型相匹配,强制转换成相应的类型,最后调用相应的取值方法getXXX(),就可以得到确定类型的值。API提供了以下基本类型,与Excel的数据格式相对应,如下图所示:



每种类型的具体意义,请参见Java Excel API Document。

我是循环取出全部数据,并转化为相应格式:

int rows = sheet.getRows();

for (int i = 1; i < rows; i++) {

Cell cb1 = sheet.getCell(0, i);

Cell cb2 = sheet.getCell(1, i);

Cell num3 = sheet.getCell(2, i);

Cell num4 = sheet.getCell(3, i);



String user = "";

String rule = "";

int numNew = 0;

int numEdit = 0;

if (cb1.getType() == CellType.LABEL) {

LabelCell lc = (LabelCell) cb1;

user = lc.getString();

}

if (cb2.getType() == CellType.LABEL) {

LabelCell lc = (LabelCell) cb2;

rule = lc.getString();

}



if (num3.getType() == CellType.NUMBER_FORMULA) {

NumberFormulaCell nc = (NumberFormulaCell) num3;

try {

numNew = Double.valueOf(nc.getFormula()).intValue();

} catch (FormulaException e) {

e.printStackTrace();

}

}

if (num4.getType() == CellType.NUMBER_FORMULA) {

NumberFormulaCell nc = (NumberFormulaCell) num4;

try {

numEdit = Double.valueOf(nc.getFormula()).intValue();

} catch (FormulaException e) {

e.printStackTrace();

}

}

}


当你完成对Excel电子表格数据的处理后,一定要使用close()方法来关闭先前创建的对象,以释放读取数据表的过程中所占用的内存空间,在读取大量数据时显得尤为重要。参考如下代码片段:

//操作完成时,关闭对象,释放占用的内存空间

workbook.close();




Java Excel API提供了许多访问Excel数据表的方法,在这里我只用到了几个,其它的,以后可以慢慢看API吧.我也会慢慢看的.以后看看写Excel的操作再写上来吧.