MathWorks - Mobile View
  • 碻形冰暨硞 MathWorks 帐憷碻形冰暨硞 MathWorks 帐憷
  • Access your MathWorks Account
    • 我的帐户
    • 我的社区资料
    • 关联许可证
    • 登出
  • 产品
  • 解决方案
  • 学术
  • 支持
  • 社区
  • 活动
  • 获取 MATLAB
MathWorks
  • 产品
  • 解决方案
  • 学术
  • 支持
  • 社区
  • 活动
  • 获取 MATLAB
  • 碻形冰暨硞 MathWorks 帐憷碻形冰暨硞 MathWorks 帐憷
  • Access your MathWorks Account
    • 我的帐户
    • 我的社区资料
    • 关联许可证
    • 登出

视频与网上研讨会

  • MathWorks
  • 视频
  • 视频首页
  • 搜索
  • 视频首页
  • 搜索
  • 联系销售
  • 试用软件
  Register to watch video
  • Description
  • Full Transcript
  • Related Resources

Saving Data from MATLAB to Excel Using a Template

Stuart McGarrity, MathWorks

(Originally posted on Stuart's MATLAB Videos blog.)

I often need to export data from MATLAB to Excel. As the files are used by others, I like to format them nicely for easy viewing and browsing. To do this, I simply create a spreadsheet with all the required formatting as a template and copy it each time.

Play the video in full screen mode for a better viewing experience.

Recorded: 28 Mar 2016

I often need to write data to Excel to be viewed by others. And in those reports, it's nice for the data to be as formatted as clearly as possible. Normally, when you write data into Excel from MATLAB, the columns all have the same width and they're all the same font.

So, what I usually like to do is to match the width of the columns to the data. Also, perhaps make the header bold, freeze the top row, and put on auto-filtering.

So, I use a very simple technique of creating an Excel template. And then just copying that template each time I want to create a new spreadsheet.

So, first of all, I want to grab a table to work with. And I think I know of one in the documentation under Table Properties. Yeah, here, Load Patients.

There we are. So, here is a table with a number of columns in it. So, now if you wanted to write that to Excel, you would just-- well, let's first of all specify a file name. We'll call it Results.XLSX, like that. And then we would write Table T file name.

And then if we opened it up, take a look at it. It will look like this. So, this looks fine. All the data is here. But if someone is looking at this, analyzing this, it's nice for it to be formatted better. For example, it'd be nice if the column headers were bolded.

It would be nice if they had the proper widths. It could be that some of this data actually is pretty wide, and therefore is going to wrap. Sometimes color coding might be nice, too. Freezing the top row is also nice with this.

So, what I do is I make a template of the very first spreadsheet I've made. So, I am going to-- let me see if this is the correct width for some of these. I'm going to meet the top row bold. I'm going to freeze the top row like that. The other thing I like to do is have the filtering on by default. So, I want to filter those. And I'm going to delete the contents like that.

Now, to make sure these headers get removed when I write out to it. So, I'm going to just erase them to make it obvious that they're not being used. And finally, maybe I'll add some color coding to these, which may be appropriate for the data. Like that. And then I would save it to perhaps the file name underscore template. Like that.

So, here's the template I have. And there's the original spreadsheet. So, any time I want to create a formatted spreadsheet, I would do a copy file, file name underscore template. Oh, I need to define file name. File name underscore template, first of all. So, let's do-- file name underscore template equals that.

Then if I want to use the template, I'll do a copy file from the file name template to file name. And that puts the template into this file. And if I want to look at it, you can see that it now has the formatted layout. And then I can just do my write again, and write into the table. And then we'll take a look at it.

And you see, we have the color coding, we have the formatting. We have the auto-filter, and the frozen top row.

Now, if you want to write into multiple tabs, that can be useful too. And I recommend specifying the names of those. So, if I open up the template again, I can give it a name like can Results1. And then I'm actually going to copy it. And call it Results2. Like that.

And if you-- so, we'll make that. And you copy again-- we'll copy the template again. Now, if I write in and I specify the sheet-- and just remember, if you specify the wrong sheet, just call "results," like that. You'll get a warning because that sheet is not known.

So, if you open up the Results, you'll see it's created a new tab over here with the name Results. So you don't want to do that. So, let's copy the template again. And then write in-- and this time I'm going to write into Results1 and Results2. And we'll take a look. And there we have the results nicely formatted.

So, there is a very simple way to write data to Excel and have the Excel file formatted appropriately for others, for easier reading. Now, we could have used the com interface to talk to the application, Excel, itself, and programmatically control the formatting of the different cells.

Now, that might be useful or might be necessary in some situations to give you ultimate control over the formatting. But that is much more complicated than just this very simple process here, which works for me for the most of my use cases.

Related Products

  • MATLAB
Related Information
Stuart's MATLAB Videos

Feedback

Featured Product

MATLAB

  • Request Trial
  • Get Pricing

Up Next:

3:05
Technical Computing with MATLAB, Part 2: Importing from...

Related Videos:

15:27
From Big Engineering Data to Insights Using MATLAB Analytics
5:10
Reading Excel Data into MATLAB with a GUI, Part 4
13:48
Reading Excel Data into MATLAB with a GUI, Part 3
5:05
Reading Excel Data into MATLAB with a GUI, Part 2

View more related videos

MathWorks - Domain Selector

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

Select web site

You can also select a web site from the following list:

How to Get Best Site Performance

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom (English)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
    • 简体中文Chinese
    • English
  • 日本Japanese (日本語)
  • 한국Korean (한국어)

Contact your local office

  • 联系销售
  • 试用软件

了解产品

  • MATLAB
  • Simulink
  • 学生版软件
  • 硬件支持
  • 文件交换

试用或购买

  • 下载
  • 试用软件
  • 联系销售
  • 定价和许可
  • 如何购买

如何使用

  • 文档
  • 教程
  • 示例
  • 视频与网上研讨会
  • 培训

获取支持

  • 安装帮助
  • MATLAB 问答社区
  • 咨询
  • 许可中心
  • 联系支持

关于 MathWorks

  • 招聘
  • 新闻室
  • 社会愿景
  • 联系销售
  • 关于 MathWorks

MathWorks

Accelerating the pace of engineering and science

MathWorks 公司是世界领先的为工程师和科学家提供数学计算软件的开发商。

发现…

  • Select a Web Site United States
  • 专利
  • 商标
  • 隐私权政策
  • 防盗版
  • 应用状态

京ICP备12052471号

© 1994-2021 The MathWorks, Inc.

  • Facebook
  • Twitter
  • Weibo
  • WeChat

    WeChat

  • LinkedIn
  • RSS

关注我们