필터 지우기
필터 지우기

How do i extract these arrays from a .m file?

조회 수: 1 (최근 30일)
Devanjith Fonseka
Devanjith Fonseka 2018년 4월 25일
댓글: Ameer Hamza 2018년 4월 26일
I need to get the matrices but I'm unsure how to proceed. The file in question is attached as a picture.
Thanks!

답변 (1개)

Ameer Hamza
Ameer Hamza 2018년 4월 26일
You didn't specify the number of matrices. If it is small, the easiest way is to do it manually like this,
A = [1 0 0;
2 0.5 0;
3 1 0;
.....;
.....];
Similar for other matrices.
  댓글 수: 2
Devanjith Fonseka
Devanjith Fonseka 2018년 4월 26일
there are three matrices. And no it is not possible to hard code them as they are big for complicated cases. Basically I want the first chunk to be called a matrix A, second a Matrix B and the last a matrix C.
1 0 0
2 0.5 0
3 1 0
4 0 0.5
5 0.5 0.5
6 1 0.5
7 0 1
8 0.5 1
9 1 1
1 1 2 5 4
2 2 3 6 5
3 4 5 8 7
4 5 6 9 8
1 1 0
2 1 0.25
3 1 1
4 1 0.25
5 0 0
6 1 1.25
7 1 1
8 1 1.25
9 1 2
Ameer Hamza
Ameer Hamza 2018년 4월 26일
If your file is formatted as above (i.e. matrices are separated by an empty line, try following code
fID = fopen('temp_.m', 'r');
fileData = fread(fID);
fclose(fID);
fileStr = char(fileData');
%%searching for empty line (i.e. 2 new lines)
partitionIndex = strfind(fileStr, [newline newline]);
A = str2num(fileStr(1:partitionIndex(1)-1));
B = str2num(fileStr(partitionIndex(1)+2:partitionIndex(2)-1));
C = str2num(fileStr(partitionIndex(2)+2:end));

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 String Parsing에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by