PART 1-Output matrix data to spreadsheet/text files using built-in functions Cre
ID: 3745136 • Letter: P
Question
PART 1-Output matrix data to spreadsheet/text files using built-in functions Create a subtraction table in a numeric 2D array of size 15x15 where each location in the array is the row index minus the column index. Show how to create the matrix using two methods: 1) use nested for loops to traverse through the rows and columns one-by-one; and 2) use the meshgrid function to generate row & column template matrices (similar to the approach used when plotting a function in 3D) Using the x1swrite function, export the data from either matrix, to an excel spreadsheet (XLSX), Also function to export to a text file (.TXT) using the hash-tag delimiter (#), instead of a comma. Open both the XLSX and.CSV files in Excel to ensure,that they were created successfully. Also open the e the csvwrite function to export to a Comma Separated Variable file (CSV) and the dimwrite CSV and .TXT files in a simple editor (such as Notepad or Wordpad) so you can see the structure of the text fles. Now, read only the .TXT file back into a different matrix variable in MATLAB using dlmread PART 2 - Output matrix data to text files using low-level File WO What happens when you try to use the dimwrite function with a delimiter that is more than one character? For example, say we require that the fle must have double-ended arrows () between each value? For this part you will need to output a magic square to a text file, but you 174-2k- k- 8- 15 will need to use low-level File l/O functions so that you can incorporate arrows between values (using less-than and greater-than signs). Ensure 4Explanation / Answer
Part (1)
Please find the required MATLAB script. Do read the comments very carefully.
%============================
clc;
clear all;
%============ using nested for loops ==============
for i=1:15
for j=1:15
table(i,j)=i-j;
end
end
%============= Using "meshgrid" function =============
% Creating rows and columns of indices from 1 to 15
[col,row]=meshgrid(1:15,1:15);
% Creating the required subtraction table
table = row-col;
% writing the created table
xlswrite('sub_table_xlsx.xlsx',table);
csvwrite('sub_table_csv.csv',table);
dlmwrite('sub_table_dlm.txt',table,'#');
% Reading the text file
read_table=dlmread('sub_table_dlm.txt','#');
%======================================
Part(2)
Following is the output when we attempt to use a delimiter with more than one character.
Error using dlmwrite (line 112)
<-> is not a valid attribute or delimiter. Delimiter must be a single character.
Following is the required MATLAB script. Please read the comments very carefully.
Note: The matrix size, delimeter and file-name can be modifed as per the requirement.
%=============================
clc;
clear all;
% Creating a matrix from magic function
matrix_size=5; % This can be modifed as per the requirement
a = magic(matrix_size);
% Choosing a multicharacter delimeter
delm = '<->';
% Opening an empty file in write mode
fid=fopen('magic_table.txt','w');
for i=1:matrix_size
for j=1:matrix_size
if(j==1)
fprintf(fid,'%s',num2str(a(i,j))); % We dont need delimeter at the beginning
else
fprintf(fid,'%s',strcat(delm,num2str(a(i,j))));
end
end
fprintf(fid,'%c ',''); % After each line adding a newline character
end
% Closing the file
fclose(fid);
%=============================
Hope this helps!!!
PLEASE THUMBS UP!!!!!!!!!!
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.