Each group is required to choose ONE of the sets below. The
chosen set must be different between the two groups.
Set A
Filename: A.txt
CID Graduates Employed
2419 2339 1976
2416 756 640
2415 856 648
Set B
matrix = [26518 100 2; 32185 58 1; 35689 42 1; 48613 172 3];
Employee ID Salary (thousand, $USD) Status
26518 100 2
32185 58 1
35689 42 1
48613 172 3
Complete the following:
1. [For Set A] Provide the syntax to extract the 2
nd row of numerical data (as highlighted) using
a. fopen, fgetl, close
b. importdata
[For Set B] The following code creates the data set, opens a file
to export fprintf commands and closes the file.
matrix = [26518 100 2; 32185 58 1; 35689 42 1; 48613 172 3];
fid = fopen('export_file.txt','wt');
fprintf(fid,'%8s %8s %8s\n','ID','Salary','Status')
fprintf(fid,'%8.0f %8.0f %8.0f\n', matrix)
fclose(fid);
a. Provide the output of highlighted fprintf lines
2. Explain your illustrations to the other group on your table and
discuss any misunderstandings
3. Take a photo of your work for future reference. The entire table
is to present the work to a demonstrator and
answer questions to get it marked off before moving onto the next
task.
Solution:
Set A:
a) fopen, fgetl, close
% Open file
fileID = fopen('A.txt');
% Read header
lin = fgetl(fileID);
i=0;
% Extract 2nd numeric data
while i<2
lin = fgetl(fileID);
i=i+1;
end
fprintf(" The 2nd row of numeric data is %s\n",lin);
Output:
b. using importdata
% Open file
da=importdata('A.txt', ' ', 1);
% Print ouput
disp('2nd row of numeric data')
da.data(2,:)
Output:
Set B:
a.
Get Answers For Free
Most questions answered within 1 hours.