Create new vector from 2 differnet cells

Hello I have a two cells and I would like to save that data into one vector.
For example:
I have cell: Lok_max (3x2 cell)
and Lok_min(2x2 cell)
If I create vector by hand writing it will be like this:
SOC= [Lok_max{1,1},Lok_min{1,1},Lok_max{2,1},Lok_min{2,1},Lok_max{3,1}];
SOC_time=[Lok_max{1,2},Lok_min{1,2},Lok_max{2,2},Lok_min{2,2},Lok_max{3,2}];
I would like to automatize and I think that I can use for loop.
My idea is:
for interval_max=1:size(Lok_max)
for interval_min=1:size(Lok_min)
xxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxx
end
end
I think that I can use 2 for loops. I don't know how to create vector with for loop from cell.
Please help me.
Best regards

 Accepted Answer

dpb
dpb on 19 Jan 2019
Edited: dpb on 19 Jan 2019
You could use loops, but that's not "the MATLAB way!"...
This also superfically seems like a place cell arrays may be in the way; and I'll use shorter names for brevity and legibility of not having similar-looking names to try to pick out visually --
>> A=reshape(1:6,3,2); % some easy-to make sample data that can still see
>> B=reshape(1:4,2,2)+10; % who's who in the zoo to see where values ended up
N=size(A,1)+size(B,1); % how many rows are in result is total number in each
C(1:2:N,:)=A; % place A in alternate rows from top
C(2:2:N-1,:)=B; % and B alternates but there's a missing row
>> C = % see what's we got...
1 4
11 13
2 5
12 14
3 6
>>
Your desired two vectors are columns 1, 2, respectively.
The above is generic as long as A has one more row than B

5 Comments

Thank you for helping but this can't work in my case. I am sorry that I forgot to said that my cells have more data.
My Lok_max cell:
3x2 Cell :
1x29 single 1x29 single
1x31 single 1x31 single
1x26 single 1x26 single
My 2x2 Cell:
1x24 single 1x24 single
1x24 single 1x24 single
I am working on a project with charging/discharging data for battery state of charge estimation. I had a lot of data. I will have many different measurements and there will be different Max and Min cells. I will have 50 or more charghe/discharge cycles so I need to automatize my code.
I am looking for minimums and maximums in my data. You can see in picture one example:
1.PNG
I need to get all data in one vector for data and one vector for time. I need that because I need to use command spline to connect all points and expand data to the same size as other original measurements such as current, etc.
The resualt will be something like that:
2.PNG
As long as the arrays are such that SIZE(A,1)==SIZE(B,1)+1 the above works whether A, B are cell arrays or doubles--
A =
3×2 cell array
{1×29 double} {1×29 double}
{1×31 double} {1×31 double}
{1×26 double} {1×26 double}
B =
2×2 cell array
{1×24 double} {1×24 double}
{1×24 double} {1×24 double}
N=size(A,1)+size(A,2);
C=cell(N,2);
C(1:2:N,:)=A;
C(2:2:N-1,:)=B;
>> C
C =
5×2 cell array
{1×29 double} {1×29 double}
{1×24 double} {1×24 double}
{1×31 double} {1×31 double}
{1×24 double} {1×24 double}
{1×26 double} {1×26 double}
The problem you have going forward from here is that you can't catenate the double arrays in the cells as is; you'd need to transpose for cell2mat() but you can do the rearrangement pretty concisely given the limited size of the cell array in particular instance.
>> D=[[C{:,1}].' [C{:,2}].'];
>> whos D
Name Size Bytes Class Attributes
D 134x2 2144 double
This is independent of the sizes inside each cell as long as the two are commensurate for each row as here.
Rok Recnik
Rok Recnik on 21 Jan 2019
Edited: Rok Recnik on 25 Jan 2019
Thank you.
Rok Recnik
Rok Recnik on 25 Jan 2019
Edited: Rok Recnik on 25 Jan 2019
But what If I have the same size of A and B. Until now it works perfect but now I have some data with the sam size cells A and B. Is it possible to automatic dedection if A and B is the same size and if my data started with max or with min? I want to fully automatize this.
dpb
dpb on 25 Jan 2019
Edited: dpb on 25 Jan 2019
  1. Just adjust the indexing expression upper limit to match what size tells you.
  2. Probably...if the data sections are the sections between the min/max peaks/valleys in the previous plot, then inspecting the first/last section of those and comparing (say) average magnitudes should tell you what you want to know...the noise will dictate how complicated the comparison must be. If push comes to shove, a polyfit(x,y,1) over the whole section and testing the sign of the slope term should be pretty reliable.

Sign in to comment.

More Answers (0)

Products

Release

R2018b

Asked:

on 19 Jan 2019

Edited:

dpb
on 25 Jan 2019

Community Treasure Hunt

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

Start Hunting!