Saving values in a variable( workspace)
1 回表示 (過去 30 日間)
古いコメントを表示
I want to save different values which are being calculated in a loop (for example for loop) to a workspace.
But each time it calculates i need to place the values in different column in same workspace variable
the output from the for loop is (1 2 3 4 5 ) --> 1st iteteration
the output from the for loop is (6 7 8 9 10 ) --> 2nd iteteration
These values i need to save in a variable or array in different rows.
5 件のコメント
採用された回答
Bob Thompson
2019 年 6 月 18 日
Both of your questions can be answered with the same concept. Because Matlab is centered around matrices, indexing in those matrices is a crucial part of working with Matlab. In its simplest form, indexing is accomplished by calling specific elements numerically, A(1), but as you have seen it is also possible to call indices with variables, A(i).
In your loop, you want to make sure you are calling the correct indexing, but it should increment with the loop.
'And now i want to Save the values of S and S1 in row1 and row2 also in row 3 and row4 respectively and so on. Also both S and S1 in one common variable in workspace'
for ii = 0:4:16 jj=0:1:10;
S(end+jj,:)=DataIn(ii+1).CDTimeArray + DataIn(ii+3).CDTimeArray;
^ ^
row column
S1=DataIn(ii+2).CDTimeArray + DataIn(ii+4).CDTimeArray;
end
First off, there is no reason to range your values from 0 to anything. You do not specifically use 0 anywhere, so it just complicates your code.
for ii = 1:4:17;
Second, to the best of my knowledge, you cannot create two indexes for a single loop. I'm not entirely sure if that's what you're trying to do with jj = 0:10;, but it isn't going to create a loop, and is just going to make an array of integers from 0:10. Using S(end+jj,:) is just going to write eleven rows of the same row of data. I would suggest just adding a new row.
S(end+1,:) = DataIn(ii).CDTimeArray + DataIn(ii+2).CDTimeArray;
Third, it seems like you're saying you want to combine all of the results from S and S1 into a single array. If that is correct, then you can do so, but it really means you don't need S or S1, just the new array (I am going to call it T). Assigning the results is simply a matter of proper indexing.
for ii = 1:4:17
T(end+1,:) = DataIn(ii).CDTimeArray + DataIn(ii+2).CDTimeArray;
T(end+1,:) = DataIn(ii+1).CDTimeArray + DataIn(ii+3).CDTimeArray:
end
No guarentees that it will work immediately, but the concepts should get you working.
0 件のコメント
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Matrix Indexing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!