在MATLAB中将第一个空格的文本的每一行读入一个单元格数组中 [英] read each line of a text up to the first space into a cell array in MATLAB
本文介绍了在MATLAB中将第一个空格的文本的每一行读入一个单元格数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
$ b $ pre $ cell $ {1} {1} = line1_1stword
cell {1} {2} = line2_1stword
。
。
。
我该怎么做?我知道下面的命令将每行读入单元格的一行,但是我只想要第一个单词。
$ $ $ $ $ $ $ $ $ $ $ fopen(`myFile.ext`)
allData = textscan(fid,'%s','Delimiter','\\\
');
解决方案
试试这个 -
fid = fopen('myFile.ext')
allData = textscan(fid,'%s','Delimiter','\\\
' )
%% //从每一行数据读取第一个字
outcellarray = regexp(allData {:},'^([\ w \ - - ] +)' ,'match')
%% //将所有的第一个单词存储到单个单元格的字符串数组
outcellarray = vertcat(outcellarray {:})
受到此代码的启发。
I have a multi line text file that in each line has 3 words separated by some spaces. I want to write the first word of each line into a nX1 cell array, so that:
cell{1}{1}=line1_1stword
cell{1}{2}=line2_1stword
.
.
.
How can I do that? I know that the following command reads each line into a line of the cell, but I want just the first word.
fid = fopen(`myFile.ext`)
allData = textscan(fid,'%s','Delimiter','\n');
解决方案
Try this -
fid = fopen('myFile.ext')
allData = textscan(fid,'%s','Delimiter','\n')
%%// Read in the first word from each row of data
outcellarray = regexp(allData{:},'^([\w\-]+)','match')
%%// Store all the first words into a single cell array of strings
outcellarray = vertcat(outcellarray{:})
Inspired by this code.
这篇关于在MATLAB中将第一个空格的文本的每一行读入一个单元格数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文