error when assign a matrix
조회 수: 1 (최근 30일)
이전 댓글 표시
Hi there,
I have four data files with a name 4.dat, 5.dat, 6.dat and 7.dat respectively in a folder. After treating these data by codes, I wish to assign the data name and computed result to a nX2 matrix named "result". Here is my code:
cd('C:\fitting\test\');
Allname=struct2cell(dir);
[m,n]=size(Allname);
for i=3:n
iname=Allname{1,i};
... ... %treatment of data, the interesting result is named "a"
result=zeros(n,2)
result(i,1:2)=[iname, a]
end
An error of "??? Subscripted assignment dimension mismatch" appears and the output is
result =
0 0
0 0
0 0
0 0
0 0
0 0
0 0
I don't know why this can happen.
Many thanks.
댓글 수: 0
답변 (2개)
Jan
2013년 4월 19일
편집: Jan
2013년 4월 19일
The relevant information is missing in your question: In which line does the error appear? What is the type and value of a and iname?
I guess, that the problem is in the line:
result(i,1:2)=[iname, a]
If iname and a are not scalars, you cannot write them to the [1 x 2] vector on the left hand side of this assignment. Perhaps you want result to be a cell array, when you want to mix the string(!) iname and the variable a.
댓글 수: 0
Fei
2013년 4월 21일
댓글 수: 1
Walter Roberson
2013년 4월 21일
Initialize
result = cell(n, 2);
then in the loop,
result(i,1:2) = {iname, a};
assuming you want a cell array whose first element in a row is the file name and whose second element is the numeric value as a numeric value.
You could change it to num2str(a) if you want the second column to be a represented as a string, but still want columns.
If what you want is one row of characters, then initialize
result = cell(n,1);
and then in the loop,
result{i} = [inname, ' ', num2str(a)];
and after the loop,
result = char(result);
참고 항목
카테고리
Help Center 및 File Exchange에서 Line Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!