How to read and process line by line in matlab?
7 views (last 30 days)
Show older comments
I have the text file, where it is contain list of the combination of letter,number and special character. Here character will be the number until first space in the string, and after space it consits of all the character LIKE AN EXAMPLE BELOW
11455 01adsd@#$
1223 AaFg3434
1345 01ADAS FE
14090 01aFAF
1585 !%^r^&&*^
1644 01!ggjGIJiu
178 !Aa8Y348Y23RHF
Here i want to first read number until a space and after that i want to read through each character.
0 Comments
Accepted Answer
Adam Danz
on 28 Sep 2019
Edited: Adam Danz
on 28 Sep 2019
I pasted your example into the attached text file. This reads that file and then parses the number and char array components.
% Read in the text file
ca = strtrim(strsplit(fileread('myTextFile.txt'),'\n'))';
% parse the leading numbers and then the rest after the first space
nums = str2double(regexp(ca,'^\d+ ','match','once'));
chars = cellfun(@(c,x)c(x+1:end),ca,regexp(ca,'^\d+ ','end'),'UniformOutput',false);
Result
nums =
11455
1223
1345
14090
1585
1644
178
chars =
7×1 cell array
{'01adsd@#$' }
{'AaFg3434' }
{'01ADAS FE' }
{'01aFAF' }
{'!%^r^&&*^' }
{'01!ggjGIJiu' }
{'!Aa8Y348Y23RHF'}
0 Comments
More Answers (0)
See Also
Categories
Find more on Text Files in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!