I am using MATLAB and I wrote a text file and I am trying to enter a 9x9 matrice
ID: 3827479 • Letter: I
Question
I am using MATLAB and I wrote a text file and I am trying to enter a 9x9 matrices and a 9x1 vector from the text file. I was able to get the 9x9 matrices to work properly, however I am having trouble reading the 9 x 1 matrices. I keep getting the error:
Subscripted assignment dimension mismatch.
This is the text file I am using:
My circuit project data
Size of the problem
9
System Matrix
1 0 1 -1 0 0 0 0 0
0 1 -1 0 0 1 0 0 0
-1 -1 0 0 0 0 -1 0 0
0 0 0 0 1 -1 0 -1 0
0 0 0 0 0 0 1 1 1
-18 14 10 0 0 0 0 0 0
0 0 -10 -15 -12 -6 0 0 0
0 -14 0 0 0 6 0 -11 0
0 0 0 0 12 0 0 11 -16
Right hand side
0
0
0
0
0
24
0
-87
76
The code for the 9x9 matrices that works is shown below:
%Script to read the circuit system
fileid = fopen('my_circuit.txt','rt');
title=fgetl(fileid)
title = fgetl(fileid)
[matrix_size,count] = fscanf(fileid,'%d',1)
nothing = fgetl(fileid) %advances to the next line
title = fgetl(fileid);
%initialize A_matrix
A_matrix = zeros(matrix_size, matrix_size)
%Read system A matrix
for ii = 1:matrix_size
[A_matrix(ii,:),count]=fscanf(fileid,'%d',matrix_size)
nothing=fgetl(fileid)
end
The code I am having trouble with is shown below:
%start back here
title = fgetl(fileid)
[matrix_size2,count]=fscanf(fileid,'%d',1)
%nothing = fgetl(fileid)
%initialize B_Matrix
RHS_vector = zeros(matrix_size2,matrix_size2)
%Read System B matrix
for jj = 1:matrix_size2
[RHS_vector(jj,:),count]=fscanf(fileid,'%d',matrix_size2)
%nothing=fgetl(fileid)
end
Can someone give me some direction in my mistake?
Ikeep getting the error:
Error in Project (line 29)
[RHS_vector(jj,:),count]=fscanf(fileid,'%d',matrix_size2)
Explanation / Answer
Use dlmread function. For example,
M = dlmread('my_circuit.txt');
Read the entire file into a single matrix and then separate out the RHS and system matrix from it.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.