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

4 次查看(过去 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 中查找有关 Resizing and Reshaping Matrices 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by