THIS IS FOR MATLAB A file “namedept.dat” stores first names and departments for
ID: 3685892 • Letter: T
Question
THIS IS FOR MATLAB
A file “namedept.dat” stores first names and departments for some employees of a car dealership (separated by the pound sign). For example, the file might store:
A script is being written to read the information from the file into a vector variable (“employees”) of structures; each structure has fields for the name and department. In order to be efficient, the vector is preallocated to have 50 elements (although this should later be reduced to the actual number of elements used). You are to write the script to read each line from the file using fgetl, and put the information from each line into a structure that is stored in the vector.
Explanation / Answer
fid = fopen( ' namedept.dat ' );
employees = struct;
for i=1:50
employees(i)=struct;
end
i=1;
while ~feof(fid) && i <=50
aline=fgetl(fid);
words=strsplit(aline,'#' );
employees(i).name=words(1,1);
employees(i).department=words(1,2);
i=i + 1;
end
fclose(fid)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.