How can I make this text file in a list in MATLAB?

I have a text file and you want to import it into MATLAB and make it a list:

Person1
name = steven
grade = 11
age= 17

Person2
name = mike
grade = 9
age= 15

Person3
name = taylor
grade = 11
age= 17

There are several hundred entries like the ones above. Each of them is separated by an empty line. I thought I could scan the text and make the information between each empty line into an element in the list. I would also like to be able to search each person by name, as soon as I have a list like the one below.

I need something like:

x = [Person1         Person2       Person3      
     name = steven   name = mike   name = taylor
     grade = 11      grade = 9     grade = 11
     age = 17        age = 15      age = 17]

It seems very direct, but so far I have had problems with this. I can ignore something. Anyone have any ideas or tips?

+3
source share
2

, . , age = ( ), TEXTSCAN:

fid = fopen('people.txt','r');           %# Open the data file
peopleData = textscan(fid,'%s %*s %s');  %# Read 3 columns of strings per row,
                                         %#   ignoring the middle column
fclose(fid);                             %# Close the data file

, 3 1 'name', 'grade' 'age':

nFields = 3;                                       %# Number of fields/person
fields = peopleData{1}(2:nFields+1);               %# Get the field names
peopleData = reshape(peopleData{2},nFields+1,[]);  %# Reshape the data
peopleData(1,:) = [];                              %# Remove the top row
peopleData(2:nFields,:) = cellfun(@str2double,...  %# Convert strings to numbers
                                  peopleData(2:nFields,:),...
                                  'UniformOutput',false);
x = cell2struct(peopleData,fields,1);              %# Put data in a structure

RESHAPE, CELLFUN, STR2DOUBLE CELL2STRUCT.

+5

'person' ',' grade '' age '

fgetl regexp , .

+3

Source: https://habr.com/ru/post/1750173/


All Articles