使用Matlab从某些文件夹导入多个csv文件时出现错误代码 [英] Error code in importing multiple csv files from certain folder using matlab

查看:166
本文介绍了使用Matlab从某些文件夹导入多个csv文件时出现错误代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的是Matlab编程的新手.我在编码时将某些文件夹中的多个csv文件导入一个文件时遇到问题:

I am really a newbie in matlab programming. I have a problem in coding to import multiple csv files into one from certain folder:

这是我的代码:

%% Importing multiple CSV files

myDir = uigetdir; %gets directory
myFiles = dir(fullfile(myDir,'*.csv')); %gets all csv files in struct
for k = 1:length(myFiles)
    data{k} = csvread(myFiles{k});
end

我使用代码uigetdir以便能够从任何文件夹中选择数据,因为我尝试编写一个自动化程序,因此其他人可以灵活地使用它.我运行的代码仅查找目录并显示列表,而不是将csv文件合并为一个并在导入数据"中读取.我希望将其合并并作为一个文件读取. 我的合并文件应该看起来像这样,以分号分隔,由47个合并在一起的csv文件组成(此图片是我拥有的csv文件之一): 我的合并文件

I use the code uigetdir in order to be able to select data from any folder, because I try to make an automation program so it would be flexible to use by others. The code that I run only look for the directory and shows the list, but not for merging the csv files into one and read it in "import data". I want it to be merged and read as one file. My merged file should look like this with semicolon delimited and consist of 47 csv files merged together (this picture is one of the csv file I have): my merged file

我整天都在为此工作,但是我总是发现错误代码.请帮助我:(.非常感谢您的帮助.

I have been working for it a whole day but I find always error code. Please help me :(. Thank you very much in advance for your help.

推荐答案

错误消息指出,您试图将myFiles引用为单元格数组,而不是它. dir 的输出是一个结构,无法对其建立索引像细胞阵列一样.

As the error message states, you're attempting to reference myFiles as a cell array when it is not. The output of dir is a structure, which cannot be indexed like a cell array.

您想要执行以下操作:

for k = 1:numel(myFiles)
    filepath = fullfile(myFiles(k).folder, myFiles(k).name);
    data{k} = csvread(filepath);
end

这篇关于使用Matlab从某些文件夹导入多个csv文件时出现错误代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆