I am trying to reshape a matrix from the text file to 45x50 , but when i do the reshape it shows that the number of the elements should not be changes. is there any problem with my coding?
% fileid = fopen ('2016 Sept 7 - Vortex 1_0001')
x = textscan (fileid, '%n %n %n %n','headerlines',3,'delimiter',',')
cc = reshape (x, 44,45)

답변 (2개)

Stephen23
Stephen23 2017년 9월 23일
편집: Stephen23 2017년 9월 23일

1 개 추천

A simple way to get the numeric data in one array is to use textscan's option 'CollectOutput':
[fid,msg] = fopen(...,'rt');
assert(fid>=3,msg)
opt = {'HeaderLines',3, 'Delimiter',',', 'CollectOutput',true};
C = textscan(fid,'%n%n%n%n', opt{:});
fclose(fid);
M = C{1};
Note that in this code I display the error message if fopen can't open the file, and also put the textscan options into a cell array to make them easier to keep track of.
Cedric
Cedric 2017년 9월 23일
편집: Cedric 2017년 9월 23일

0 개 추천

The output of TEXTSCAN is a row cell array with 4 cells and each one of them contains a numeric array (column vector) of 2250 values.
>> class( x )
ans =
cell
>> size( x )
ans =
1 4
>> class( x{1} )
ans =
double
>> size( x{1} )
ans =
2250 1
It is difficult to guess what you want to achieve with the reshape, but you probably want to merge the content of all cells into a 2250x4 numeric array first:
>> data = [x{:}] ;
>> class( data )
ans =
double
>> size( data )
ans =
2250 4
where x{:} is a comma-separated list (CSL), [..] is a concatenation, and [x{:}] is equivalent to [x{1},x{2},x{3},x{4}].

댓글 수: 2

Stephen23
Stephen23 2017년 9월 23일
Or just set textscan's option 'CollectOutput' to true.
Cedric
Cedric 2017년 9월 23일
Stephen, move this as an answer and I vote for it ;-)

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

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

질문:

2017년 9월 23일

편집:

2017년 9월 23일

Community Treasure Hunt

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

Start Hunting!

Translated by