필터 지우기
필터 지우기

How to transform a matrix from a text file to numeric matrix to perform calculations with.

조회 수: 3 (최근 30일)
Hello.
I have a matrix in a file(which i have attached) and after using fopen i have used fgetl to read the matrix but it reads into a string and i need to perform calculations with every single onde of the elements in the matrix and it has to have the elements in the exact order as they where in the text file. In the end what i basically want is a matrix where afterwards i can use it to acess it's elements and do calculations with them.
I am only allowed to use functions that are in the book "MATLAB A Pratical Introduction to Programming and Problem Solving" so i am having some troubles with this.
while feof(f) == 0
matrix = fgetl(f);
end
Thanks for the help.
  댓글 수: 3
Sofia Batista
Sofia Batista 2016년 12월 14일
Thank you for answering!
Indeed it works but i don´t think I´m allowed to use that function. What i was looking for was a way for fgetl to read into lines and not on a single string line probably using while and some type of instruction and after that use str2num to converted to a numerical matrix.
Guillaume
Guillaume 2016년 12월 14일
I have no idea what is in the book you refer to, but if a book titled "MATLAB A Pratical Introduction to Programming and Problem Solving" does not talk about dlmread or its related csvread then its title is very misleading and the book not worth buying.

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

채택된 답변

Guillaume
Guillaume 2016년 12월 14일
편집: Guillaume 2016년 12월 14일
As per Stephen's comment, I'd recommend using dlmread (or similar csvread) or at a push textscan (which makes the code more complicated that it needs to be). If, for some reason, that is not allowed and you really want to do it the complicated and slow way, then you can indeed use fgetl.
The first problem with your code
while feof(f) == 0
matrix = fgetl(f);
end
is that each time you call fgetl you overwrite the matrix variable (whose name is really misleading, it's just one line of text) with the content of the line.
You can either store each line as row of a char matrix (fragile, will error if a line has more characters than the others):
lines = '';
while feof(f) == 0
lines(end+1, :) = fgetl(f);
end
Or as a cell array of char vectors:
lines = {};
while feof(f) == 0
lines{end+1} = fgetl(f);
end
Or convert as you read the line, and then store in a matrix or cell array of numbers:
matrix = {}; %or matrix = [];
while feof(f) == 0
line = fgetl(f);
matrix{end+1, 1} = str2double(strsplit(line));
%or matrix(end+1, :) = str2double(strsplit(line)); %but will error if not all lines have the same number of elements
end
%only for cell arrays:
try
matrix = cell2mat(matrix);
catch
error('some rows have more numbers than others');
end
Note that I use str2double instead of str2num. str2num isn't safe. It will happily format your hard drive if you pass it 'rmdir('c:\', 's'). Since you have no idea what is in the text file you're parsing, you should assume it can be hostile.
  댓글 수: 4
Sofia Batista
Sofia Batista 2016년 12월 16일
Sorry but i have a question again! I went and showed my code to my teacher and he said he didn´t know the strsplit function so i could not use it. Is there any way to go around that?

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

추가 답변 (1개)

David Barry
David Barry 2016년 12월 13일
Here's an example of how you can import the data as doubles. I'll leave you to reshape the matrix if you need to.
fid = fopen('grafo.txt');
matrix = textscan(fid, '%f');
matrix = matrix{1};
fclose(fid);

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by