run the for loop only once Matlab

Hi everybody, i want to know if its possible to run the for-loop only once.
total_Route = zeros(4,4);
for i=1:4
total_Route(i,1)=Distance_Traveled_CM;
total_Route(i,2)=Hauptantrieb_Verbrauchte_Energie_CM;
total_Route(i,3)=Nebenaggregate_Verbrauch_Real_CM;
total_Route(i,4)= i;
Total_Distance_Traveled_CM = sum(total_Route(:,1));
set(handles.edit3, 'string',Total_Distance_Traveled_CM);
Total_Hauptantrieb_Verbrauchte_Energie_CM=sum(total_Route(:,2));
set(handles.edit4, 'string',Total_Hauptantrieb_Verbrauchte_Energie_CM);
Total_Nebenaggregate_Verbrauch_Real_CM=sum(total_Route(:,3));
set(handles.edit5, 'string',Total_Nebenaggregate_Verbrauch_Real_CM);
%%Index
set(handles.edit15, 'string',i);
after the running this code, "i" is then at the end 4 and the for-loop will be runs 4 times
i want to run the for-loop only once and "i" should be incremented after each pass
I am thankful for every help

 採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2016 年 8 月 31 日
編集済み: Azzi Abdelmalek 2016 年 8 月 31 日

0 投票

If you want to run it once, then don't use a for loop, to increment ii, use
ii=mod(ii,4)+1
don't use i, it's used to represent complex numbers

8 件のコメント

Alex
Alex 2016 年 8 月 31 日
Thank u @Azzi for the quick response.
Alex
Alex 2016 年 8 月 31 日
Heey Azzi
with Which value u would initialise i at the beginning, because this code is not working
ii = 0;
ii=mod(ii,4)+1;
%i is in the case always 1
Azzi Abdelmalek
Azzi Abdelmalek 2016 年 8 月 31 日
if isempty(ii)
ii = 0;
end
ii=mod(ii,4)+1;
Alex
Alex 2016 年 8 月 31 日
im using this code in the Gui-Window, and my programm still giving me this feedback:
Variable i is might be used befor it is defined. should ii write the below code in the Callback window ?
if isempty(ii)
ii = 0;
end
Azzi Abdelmalek
Azzi Abdelmalek 2016 年 8 月 31 日
I don't know how many files you are using! are you using functions?
Azzi Abdelmalek
Azzi Abdelmalek 2016 年 8 月 31 日
Try this
id=whos('ii')
if isempty(id)
ii=0
end
ii=ii+1
Alex
Alex 2016 年 9 月 1 日
Hallo Azzi,
im using normal scripts, i creatd a Matrix with 4×4 Element with the Goal to save the results off each my Variable in a element off my Matrix.
Example:
the first pass: ii=0 ->> Distance_Traveled 900(first element (1,1)) the gui will take the sum of the first column wich (900+0+0+0) and write it in a static test
the second pass i=1-->Disantace traveled(2,1) 800 the gui will take the sum of the first column wich (900+800+0+0) and write it in a static test and same thing should happen with the other column
i want through the gui every time to write the summe of a column of my Matrix and write it in a satic text box. i
hope, u catch what i mean
Alex
Alex 2016 年 9 月 1 日
編集済み: Alex 2016 年 9 月 1 日
i think the Problem is that ii doesnt keep his last Value
id=whos('ii')
if isempty(id)
ii=0
end
ii=ii+1
because with this code will be ii always 1.

サインインしてコメントする。

その他の回答 (1 件)

Image Analyst
Image Analyst 2016 年 8 月 31 日

2 投票

Why not simply replace this line
for i=1:4
with this line:
i = 1;
It will do the code just once since it won't even be in a loop.

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

質問済み:

2016 年 8 月 31 日

編集済み:

2016 年 9 月 1 日

Community Treasure Hunt

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

Start Hunting!

Translated by