How to Import Data from Files Programmatically
了解如何在MATLAB中以编程方式导入数据®通过使用“导入工具”中的“生成代码”选项创建脚本,或通过从头开始编写代码。该视频显示了如何使用导入功能,例如可读取
,,,,readmatrix
, 和readcell
。
该教程的重点是电子表格,但MATLAB支持各种文件类型。万博1manbetx本视频中的广泛原则也可以应用于其他支持的文件类型。万博1manbetx
您好,欢迎来到另一个MATLAB教程。
Let’s say we have a bunch of data sets and we want to import them the same way every time but using the import button over and over again is just an inefficient use of our time. How would we go about automating our import?
对我们来说很幸运,Matlab让我们通过编程导入数据。现在,有两种主要方法可以使用导入工具导入,然后按生成代码,并将生成脚本。
或者,您可以从头开始编写代码。在文档中,您可以找到一个受支持的文件类型的列表,因为您可以看到从视频到照片再到音乐的种类万博1manbetx繁多,本教程仅着眼于电子表格,但是也可以将广泛的原则应用于导入。
此处列出了我们关注的每个文件类型的导入和导出功能。现在,19a的令人兴奋的发展是,您不再需要使用诸如CSVREAD,DLMREAD,XLSREAD之类的功能。取而代之的是带有明确分隔符的电子表格都使用相同的导入函数。
导入函数读取,然后不管你want to read the spreadsheet in as. So, if you wanted to import the spreadsheet as a matrix, it’s readmatrix, if you want a cell array it’s readcell, if you want a table it’s readtable etc. etc.
因此,如果我输入可读取(文件名)并点击运行,请启动程序导入并吐出表。请注意,您必须在同一目录中仅编写文件名才能正常工作。另外,您可以编写文件位置以易于使用。
MATLABis really good at guessing the options you want for your file. But if you want to change them, you can see your options by typing detectImportOptions(filename). It returns a struct of values, for more information about what all of these different variables correspond to, consult the documentation. But let’s say we want to alter the number rows we’re reading in. Well that’s this variable here, DataLines. So we assign the options to a variable opts and then we can change the number of lines by typing opts.DataLines and assigning a new value.
当我们现在致电可读取时,我们只能进行逗号选择,然后只需繁荣即可。
您还可以通过调用SetVaropts以类似的方式设置变量选项。您只需编写opts = setVaropts(opts,变量名称,要更改的元素,新变量)。您可以通过在“导入”结构中单击此处,访问SetVaropts文档。
感谢您的观看和愉快的编码。
相关产品s manbetx 845
您还可以从以下列表中选择一个网站:
如何获得最佳网站性能
选择中国网站(中文或英语)以获得最佳场地性能。其他Mathworks乡村网站未针对您所在的访问进行优化。
美洲
- América Latina(Español)
- 加拿大(英语)
- 美国(英语)