error when assign a matrix
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Antworten (2)
Jan
am 19 Apr. 2013
Bearbeitet: Jan
am 19 Apr. 2013
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 Kommentare
Fei
am 21 Apr. 2013
1 Kommentar
Walter Roberson
am 21 Apr. 2013
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);
Siehe auch
Kategorien
Mehr zu Data Types finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!