Filter löschen
Filter löschen

create one cell in excel after one iteration

1 Ansicht (letzte 30 Tage)
Michael
Michael am 15 Okt. 2013
Kommentiert: Cedric am 17 Okt. 2013
Hi guys,
this is the code:
M = cell(28,1);
for i=1:3;
M{i} = UM(i,:);
IPV = M{i,1}(1);
IB = M{i,1}(2);
IWP = M{i,1}(3);
IK = M{i,1}(4);
IWS = M{i,1}(5);
for p = 1:2;
Bundesland=p;
OptimierungPV_B_BANDU;
Kapitalwert(1,1)=num2cell('ZFW%d', i);
if Bundesland == 1;
xlswrite('testsheet.xlsx', ...
Kapitalwert,...
'Kapitalwerte','A1:A28');
elseif Bundesland == 2;
xlswrite('testsheet.xlsx', ...
Kapitalwert, ...
'Kapitalwerte','B1:B28');
end;
end;
end;
end;
After each iteration in the script "Optimierung_.." the value of ZFW is calculated by the script. this value should be written into a excel file, depending on "Bundesland".
when i run the code, i receive this error:
Subscripted assignment dimension mismatch.
Error in Kapitalwertvergleich_28_Moeglichkeiten (line 28)
Kapitalwert(1,1)=num2cell('ZFW%d', i);
thanks for your support.

Akzeptierte Antwort

Cedric
Cedric am 15 Okt. 2013
Bearbeitet: Cedric am 15 Okt. 2013
What are you trying to achieve with the line?
Kapitalwert(1,1)=num2cell('ZFW%d', i);
Type
doc num2cell
you'll see that it doesn't work like S/FPRINTF. It just takes a numeric array and converts it to cell array. You might want to do something like the following (I guess):
Kapitalwert{1,1} = sprintf('ZFW%d', i) ;
(note the curly brackets for addressing the content of the cell and not the cell itself).
Another point is that it is generally a bad idea to export to XLS(X) in a loop, as this is a time consuming operation. We usually build a full cell array for export purpose in the loop, and we export it in one shot after the loop.
  11 Kommentare
Michael
Michael am 17 Okt. 2013
thanks for this little teaching. it helped me.
but i can not change the variable Bundesland, because in the script there 2 cases. If i extend the second loop to 3 or 4, it will not help me because Bundesland is restricted from 1 to 2.
So how can i solve the problem ?
Cedric
Cedric am 17 Okt. 2013
If I understand well, Bundesland is the column index, so there is no need to increase it. You have the following nested loop
for rId = 1 : nRows
...
for Bundesland = 1 : nCols
...
xlsContent{rId,Bundesland} = ZFW ;
end
end
which will compute
xlsContent{1,1} = ZFW
xlsContent{1,2} = ZFW
xlsContent{2,1} = ZFW
xlsContent{2,2} = ZFW
xlsContent{3,1} = ZFW
xlsContent{3,2} = ZFW
xlsContent{4,1} = ZFW
xlsContent{4,2} = ZFW
...
in that order, as the column loop (Bundesland) is the inner loop. At this point, it would be interesting to start using the debugger to observe what happens (so you can control each step of the execution). For that, place your cursor on the line
nRows = 28 ;
and press F12 for setting a break point at this location. If your code starts with a "clear all" statement, comment it out as it would clear the break point. Run your code (Run arrow or by pressing F5), you'll see that it will stop at the break point and a green arrow will appear indicating where the program is standing; the command window prompt will also change from >> to K>>. Then press F10 to move forward (or F11 if you want to enter scripts of functions which are called, but at this point I'd say stick to F10). Each time you press F10, the program moves forward and you can display variables' content in the command window. At any moment you can ask MATLAB to finish the execution (or go to the next break point) by pressing F5 again, or interrupt the debugging session by pressing Shift+F5. Using the debugger, you'll see how the empty cell is built initially and then what happens during the execution, step by step. In particular, you'll be able to see how the cell array is filled in.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Michael
Michael am 15 Okt. 2013
ok thanks, you are right. i want the value ZFW to be put into an excelfile after each iteration by this line:
Kapitalwert(1,1)=num2cell('ZFW%d', i);
now, i changed the code according to your suggestions: but then i do have the following problem, that i can not distignuish anymore. the if call does not work anymore.
M = cell(28,1);
for i=1:3;
M{i} = UM(i,:);
IPV = M{i,1}(1);
IB = M{i,1}(2);
IWP = M{i,1}(3);
IK = M{i,1}(4);
IWS = M{i,1}(5);
for p = 1:2;
Bundesland=p;
OptimierungPV_B_BANDU;
Kapitalwert{1,1}=sprintf('ZFW%d', i);
end;
end;
if Bundesland == 1;
xlswrite('testsheet.xlsx', ...
Kapitalwert,...
'Kapitalwerte','A1:A28');
elseif Bundesland == 2;
xlswrite('testsheet.xlsx', ...
Kapitalwert, ...
'Kapitalwerte','B1:B28');
end;

Kategorien

Mehr zu Get Started with MATLAB 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!

Translated by