Info

此问题已关闭。 请重新打开它进行编辑或回答。

Help with importing specific text file

1 次查看(过去 30 天)
Pepe
Pepe 2019-1-23
关闭: MATLAB Answer Bot 2021-8-20
I have a text file that is uploaded.
I removed the html tags with the following code:
str=fileread('code=acaj&period=30&endtime=2015-06-30.txt');
ind3=strfind(str,'201');
str(1:ind3(1)-1)=' ';
C={'<','>','q','w','e','r','t','z','u','i','o','p','a','s','d','f','g','h','j','k','l','y','x','c','v','b','n','m',...
'Q','W','E','R','T','Z','U','I','O','P','A','S','D','F','G','H','J','K','L','Y','X','C','V','B','N','M'...
'!','"','#','$','%','&','/','(',')','=','?','*','+','','\','|','[',']','{','}','@',';','_',...
'^','?','~','?','°','?','`','?','´','?','¨'};
ind=regexp(str,strjoin(C,'|'));
str(ind)=' ';
After using this code you get a string str that looks like this.
2015-05-31 00:00:00 2.43 6.02
2015-05-31 00:01:00 12.5 0.004 2.403 6.044 60 60
2015-05-31 00:02:00 2.381 6.008
2015-05-31 00:03:00 2.372 5.993
2015-05-31 00:04:00 2.373 5.978
2015-05-31 00:05:00 2.42 5.991
2015-05-31 00:06:00 12.5 0.004 2.347 6.019 60 60
2015-05-31 00:07:00 2.402 5.961
2015-05-31 00:08:00 2.382 6.008
...
So there are date&time and then two numbers or six numbers up to another date&time. Six numbers repeat every five rows.
If there are two numbers I need the second one to remember, and if there are six numbers I need the fourth one to remember.
When I say remember I mean to add it into an array. And the date&time should be another array.
I tried with text scan but in textscan I need to specify for example %s %s %f %f. And problem is that its not the same for all the rows because sometimes there are 2 numbers and sometimes 6.
How can i import this data?
  2 个评论
Stephen23
Stephen23 2019-1-23
@Petra: please upload the data file by clicking the paperclip button. Descriptions of files are never as good as having the files themselves.
Pepe
Pepe 2019-1-23
编辑:Pepe 2019-1-23
Thank you. Done. Please take a look.

回答(1 个)

madhan ravi
madhan ravi 2019-1-23
Requires 2013b or later:
opts=detectImportOptions('sample.txt');
T=readtable('sample.txt',opts);
t=table2cell(T);
idx=all(cellfun(@ischar,t),1);
T(:,~idx)
  2 个评论
Pepe
Pepe 2019-1-23
It's not working even though I have 2016b.
Also I rephrased a question a little bit so please take a look.
madhan ravi
madhan ravi 2019-1-23
Then it should work fine download the attached file in my answer and try the code , the output is like (which worked for me):
ans =
10×10 table
Var1 Var2 Var11 Var20 Var29 Var38 Var41 Var47 Var50 Var56
____________________ ________ _____ _____ _____ _____ _____ _____ _____ _____
31-May-2015 00:00:00 00:02:00 NaN NaN NaN NaN 2.381 NaN 6.008 NaN
31-May-2015 00:00:00 00:03:00 NaN NaN NaN NaN 2.372 NaN 5.993 NaN
31-May-2015 00:00:00 00:04:00 NaN NaN NaN NaN 2.373 NaN 5.978 NaN
31-May-2015 00:00:00 00:05:00 NaN NaN NaN NaN 2.42 NaN 5.991 NaN
31-May-2015 00:00:00 00:06:00 12.5 0.004 2.347 6.019 NaN 60 NaN 60
31-May-2015 00:00:00 00:07:00 NaN NaN NaN NaN 2.402 NaN 5.961 NaN
31-May-2015 00:00:00 00:08:00 NaN NaN NaN NaN 2.382 NaN 6.008 NaN
31-May-2015 00:00:00 00:09:00 NaN NaN NaN NaN 2.381 NaN 5.983 NaN
31-May-2015 00:00:00 00:10:00 NaN NaN NaN NaN 2.361 NaN 5.988 NaN
31-May-2015 00:00:00 00:11:00 12.4 0.004 2.359 5.98 NaN 60 NaN 60
Note: The NaNs you see is the empty ' ' since there cannot be a hole in a table or matrix.

此问题已关闭。

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by