加载并绘制 csv 文件
我是一个新的 matlab 用户,试图将我的 145 个 .csv 文件加载到 matlab 中。这些文件具有相似的名称,并且都包含两列和 3000 行。我需要能够分别处理每个文件的第 1 列和第 2 列并绘制它们(第 2 列超过 1 列)。 到目前为止,我尝试了以下操作(对于包含所有文件的文件夹):
clear ;
direc = dir('*.csv');
for i=1:length(direc)
x = csvread(direc(i).name,1);
end
我的 x 只有 3000x2 双,但我需要第三个维度......并且我不知道如何访问“direct”文件夹。我还尝试通过以下方式额外定义每个文件的第 1 列和第 2 列:
time(i,:,:)=x(:,:,1) and
signal(i,:,:)=x(:,:,2)
并绘制它;但它仅返回 1 个数据集(1 个文件)的图。
有人可以帮我吗?我希望我提供了所有必要的信息。
谢谢!
I am a new matlab user trying to load my 145 .csv-files into matlab. The files have similar names and all contain two columns and 3000 rows. I need to be able to work with column 1 and 2 separately for each file and to plot them (column 2 over 1).
So far, I tried the following (for the folder containing all files):
clear ;
direc = dir('*.csv');
for i=1:length(direc)
x = csvread(direc(i).name,1);
end
My x is only of 3000x2 double, but I need the third dimension...and I do not know how to access the 'direc' folder. I also tried to define column 1 and 2 of each file extra by:
time(i,:,:)=x(:,:,1) and
signal(i,:,:)=x(:,:,2)
and to plot it; but it returns a plot only for 1 dataset (1file).
Can someone help me with that? I hope I gave all necessary information.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定“如何访问 'direc' 文件夹”是什么意思(
direc
是一个结构数组,您已经通过direc(index).fieldname
访问它code> 如预期的那样),但我认为主要问题是在 for 循环中,每次迭代都会覆盖 x 之前的内容,所以最终得到的是上次读取文件的内容。如果所有 csv 文件都具有完全相同的尺寸,那么您需要的似乎是
您可以通过
squeeze(x(:,1,:))
访问所有第一列 - 仅x( :,1,:)
将返回大小为 3000x1x145 的三维数组(使用上面给出的数字),squeeze
将生成更方便的 3000x145 矩阵。如果不是所有的 csv 文件都具有相同的行数和列数,则需要使用元胞数组,并且无法使用方便的索引(顺便说一句:did
signal(i,:,:)=x(: ,:,2)
真的适用于二维 x 吗?),或者用零填充,这可能需要一个中间步骤来确定 3D 数组最终应该有多大。I am not sure what you mean by "how to access the 'direc' folder" (
direc
is a struct array, and you are already accessing it bydirec(index).fieldname
as intended), but I think the main problem is that in the for loop, every iteration overwrites the previous content of x, so what you get in the end is the content of the last read file.If all your csv files have exactly the same dimensions, what you need seems to be
You can then access all first columns by
squeeze(x(:,1,:))
- onlyx(:,1,:)
will return a three-dimensional array of size 3000x1x145 (using the numbers you give above),squeeze
will produce a more convenient 3000x145 matrix.If not all of your csv files have the same number of rows and columns, you need to use a cell array and the convenient indexing can not be used (btw: did
signal(i,:,:)=x(:,:,2)
really work on a two-dimensional x?), or fill with zeros, which may require an intermediate step to find how large the 3D-array should be in the end.