Saving values in a variable( workspace)

2 次查看(过去 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 个评论
rajasekar dhandapani
编辑:rajasekar dhandapani 2019-6-18
Hi Again,
Now i am trying to incorporate the for loop for this sequence.
Like i have to start with 0 with a increment of 4.
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
I tried using the following script. How to increment the row number everytime in for loop.
for ii = 0:4:16 jj=0:1:10;
S(end+jj,:)=DataIn(ii+1).CDTimeArray + DataIn(ii+3).CDTimeArray;
S1=DataIn(ii+2).CDTimeArray + DataIn(ii+4).CDTimeArray;
end
Error Message:
Subscripted assignment dimension mismatch.
Error in Run_integratem (line 2)
S(end+jj,:)=DataIn(ii+1).CDTimeArray + DataIn(ii+3).CDTimeArray;

请先登录,再进行评论。

采纳的回答

Bob Thompson
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 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by