列中值的平均值/跳过行(文本扫描)
我有一个 .txt 文件,如下所示:(
******text*******
(30 lines containing text and *)
******text*******
a b c
a b c
a b c
a b c
a b c
a b c
a b c
我正在创建一个绘图,其中 a 作为 x 和 b,c 作为 y1 和 y2)
如何使用 textscan 跳过这 30 行?我有这个,但它不起作用:
[x y1 y2] = textscan('file_name.txt', '%f %f %f', 30);
还有更多:如何计算第三列值的平均值?
I have a .txt file which looks like this:
******text*******
(30 lines containing text and *)
******text*******
a b c
a b c
a b c
a b c
a b c
a b c
a b c
(I'm creating a plot with a as x and b and c as y1 and y2)
How do I skip those 30 lines with textscan? I had this but it didn't work:
[x y1 y2] = textscan('file_name.txt', '%f %f %f', 30);
And more: how to I make the average of the values of third column?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如何跳过某些行的处理?
关于跳行,您有几个选项:
如果行数始终是静态的,并且始终位于文件的开头:
传递值为 N 的
HeaderLines
,其中 N 是您不希望处理的数字。[x y1 y2]=textscan('file_name.txt', '%f %f %f', 'HeaderLines', 30 + 2);
如果所有行以相同的字符串开头
*传递值为 ABC 的
CommentStyle
,其中ABC
是评论样式。如果要跳过的所有行均以
*
开头,则将'*'
传递给textscan
。[x y1 y2]=textscan('file_name.txt', '%f %f %f', 'CommentStyle', '*');
如何获取某个数组的平均值?
要获取某个数组的平均值,请使用
mean
< /a>:textscan
文档:mean
的文档How do I skip certain lines from being processed?
You have a few options regarding line skipping:
If the number of lines are always static, and always in the beginning of the file:
Pass
HeaderLines
with a value of N, with N being the numbers you'd like not to process.[x y1 y2] = textscan ('file_name.txt', '%f %f %f', 'HeaderLines', 30 + 2);
If all lines start with the same character string
*Pass
CommentStyle
with a value of ABC whereABC
is the comment style.If all lines to skip start with
*
, pass'*'
totextscan
.[x y1 y2] = textscan ('file_name.txt', '%f %f %f', 'CommentStyle', '*');
How do I get the average of some array?
To get the average of some array, use
mean
:Documentation of
textscan
:Documentation of
mean
如果您知道要跳过多少行,请使用 中的
HeaderLines
参数TEXTSCAN 函数:当您在格式化字符串后使用整数参数时,这意味着您想要应用格式化字符串一定次数(在您的情况下要读取的行数)。所以这与你想要的相反。
要获得平均值,请使用 MEAN 函数:
y2_avg = Mean(y2);
If you know how many lines to skip use
HeaderLines
parameter in TEXTSCAN function:When you use integer argument after the formatting string, it means you want to apply the formatting string this number of times (number of lines to read in your case). So it's opposite to what you want.
To get the average use MEAN function:
y2_avg = mean(y2);