Info
이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.
Can I use dlmread() twice within a function?
조회 수: 2 (최근 30일)
이전 댓글 표시
I am pretty new to Matlab. I want to read data from a text file into several matrices. For example, the text file contains
1,2,3,4,5
6,7,8,9,0
1,2,3,4,5
6,7,8,9,0
I want to break these data into two matrices. The first matrix(4*4) is
1 2 3 4
6 7 8 9
1 2 3 4
6 7 8 9
The second matrix(4*1), which is also a vector is
5 0 5 0
My code is
function [Matrix1, Matrix2] = ReadData(training_filename, test_filename)
Matrix1 = dlmread(training_filename, ',', [0 0 3 3]);
Matrix2 = dlmread(training_filename, ',', [0 4 3 4]);
end
Running this code I only get Matrix1 but fail at getting Matrix2. However, if I change the output_args order, like:
function [Matrix2, Matrix1] = ReadData(training_filename, test_filename)
Matrix1 = dlmread(training_filename, ',', [0 0 3 3]);
Matrix2 = dlmread(training_filename, ',', [0 4 3 4]);
end
I get Matrix2 but failed at getting Matrix1. Is there anyone know what the problem is? Thanks.
댓글 수: 3
Walter Roberson
2017년 10월 2일
It is part of MATLAB's design that when a function has multiple outputs and you do not assign the outputs to anything, that the result of executing the function is just the first output. Then, by default, that first output would be displayed.
답변 (1개)
OCDER
2017년 10월 1일
편집: OCDER
2017년 10월 1일
One way to do this is to load all data, and then split into Matrix1 and Matrix2. I assume you want Matrix1 to have all columns except last one. Matrix2 to have just the last column.
function [Matrix1, Matrix2] = ReadData(training_filename, test_filename)
Matrix = dlmread(training_filename, ',');
Matrix1 = Matrix(:, 1:end-1); %Takes 1st to last-1 column
Matrix2 = Matrix(:, end)'; %Takes last column only, and transpose to get 1x4 matrix
%Note: you don't use test_filename here. Consider using it or remove from the input.
이 질문은 마감되었습니다.
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!