read each line of a text file and storage each into an array

12 次查看(过去 30 天)
Greetings, I want to read the lines of text that have the words that contain SPARMS and save their information in an array, for example the line "SPARMS_VBE.IND.FREQ:" and the following is stored in an array1 = [10e6 100e6 5e6 150e6 500e6 50e6 600e6 40e9 100e6], the same for "SPARMS_VBE.IND.VCE" in an array2 = [0] and so on, so that for each row variable there is a particular vector.
Then each **** block ***** is stored in a cell so that you can distinguish between one block and another. Since there are other blocks
I have little time to achieve it and I am not an expert in matlab, I was trying with textscan but I do not know how to read by rows, I was seeing maybe I could do it with the readcell command but I have matlab 2017 and readcell was implemented in 2019, so I would have to install matlab 2020 so you can use readcell. I'm not really sure though. Please if anyone knows how to achieve it I will be very grateful.

回答(2 个)

Mohammad Sami
Mohammad Sami 2020-9-8
The easiest way would be to read the entire file, then use string functions to extract what you need.
The following should would get you started.
fid = fopen('somefile.txt','r');
txt = fread(fid,'*char')';
txt = splitlines(txt);
astreisk = startsWith(txt,'****')
blockid = cumsum(astreisk); % assign block id to each row preceded by ****
% rest of your code. use startsWith to identify specific types rows
%

Walter Roberson
Walter Roberson 2020-9-8
S = fileread('TEST.txt');
sparm_lines = regexp(S, '^\s?SPARMS_.*$', 'lineanchors', 'dotexceptnewline', 'match');
This would break each SPARMS line into its own cell. It would not, however, break into blocks.
I had a look at the file, and it is not clear to me that you want the blocks to be delimited by the *** lines. The lines that end in :: look to me to be more like block delimiters.

类别

Help CenterFile Exchange 中查找有关 Data Import and Export 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by