create an new cell array everytime a loop cycles through in matlab - arrays

I am new to MatLab but I have some experience with C#. I have a large dataset <169360x97> that I need to break up into 464 cell arrays. I currently have a loop that will cycle through the dataset and make a cell array, but I can not figure out how to have the loop create a new cell array every time instead of just rewriting over the same data. Here is the loop I have written.
b=5476;
e=5840;
while(b<169360)
dataset2cell(JeaAddressKwh(b:e,1:97));
b=e+1;
e=e+365;
end
I have tried the following, but i get an error message every time:
n=16;
b=5476;
e=5840;
while(b<169360)
n=dataset2cell(JeaAddressKwh(b:e,1:97));
n+1;
b=e+1;
e=e+365;
end
So basically what I am trying to get as an output is a different cell array called 16 through 464. I would appreciate any help. Thanks.

In the first loop you are not saving the cell array and in the second loop you over overwriting the previous cell array and trying to add 1 to it, without saving the result.
Try something like this:
n=cell(16,1);
b=5476;
e=5840;
i = 1;
while(b<169360)
n{i}=dataset2cell(JeaAddressKwh(b:e,1:97));
i = i+1;
b=e+1;
e=e+365;
end

Related

Assigning values in an array into another array in MATLAB

I want to assign part of a matrix into another matrix using a for loop in MATLAB. I've tried different ways but none of them worked. I want to know what's wrong with this one:
fullGrid = complex(zeros(FFTLen, numSym, numTx),zeros(FFTLen, numSym, numTx));
for i=0:(numSym/2)-1
for j=0:(FFTLen/2)-1
A(i,j)=[fullGrid(i,j)];
end
end
You made a very basic mistake. The index position in a matrix/array in
Matlab starts from 1 and not 0. So replace all the for loops from 1 to
required length.
Corrected code is given below.
fullGrid = complex(zeros(FFTLen, numSym, numTx),zeros(FFTLen, numSym, numTx));
for i=1:(numSym/2)-1
for j=1:(FFTLen/2)-1
A(i,j)=[fullGrid(i,j)];
end
end

Matlab, save output in array , loop

I want to create an array that can store the outputs each time that doing a loop. I think the problem is because in a every new iteration the numbers starts counting from the beginning so it stores only the last iteration! In each iteration the output is an array(7x3) so in total I have to have (28,3).But I tried a lot and i AM GETTING AN ARRAY (28,3) all with zeros except the last 7 rows.
Thank you very much
You can see the code below:
for t=1:ncell % in my case I have 4 cells
ti=sort(T,2)
tt= sort(Cell{t}.ExBot,2)
tq= sort(Cell{t}.ExTop,2)
te= sort(Cell{t}.ExBT,2)
%k=0
z=0
cc=[]
%%%%% for exbottom
I=ones(size(ti,1),1);
for j=1:size(tt,1)
for i=1:size(ti,1)
if tt(j,:)==ti(i,:)
k=k+1 ;
%c(k,:)=[ti(j,:), ti(j+1,:)]
I(i)=0;
cc(k,:)=Y(i,:);
cc(size(tt,1)+1,:)=cc(1,:)
else
end
end
end
end
Although more info would help as mentioned in the comments, from the information you've given, the problem is most likely in setting cc to empty when you start processing each cell.
cc=[];
On exiting the outermost loop you will only have results for the last iteration.
On a related note you may want to use isequal or all for the comparison of vectors i.e. if isequal(tt(j,:),ti(i,:))

How to save dynamic variable from workspace in a separate file in matlab?

I'm working on a problem where I have an array A of 100 elements.
All these 100 elements are changing with time.
So in my workspace, I only get the final values of all these elements after the entire time cycle has run.
I'm trying to save the values with time in a separate file (.txt or .mat) so that I can access that file in order to check how the variable varies with time.
I'm trying the following command:
save('file.mat','A','-append');
But this command overwrites the existing values in my file.
Kindly suggest me a way to save these values without overwriting them and also guide me how to access them in MATLAB.
You can also change the output filename to be unique for each iteration:
for iter=1:n
A = rand(10);
save(sprintf('file%d.mat',iter), 'A');
end
That way each iteration creates one file.
The reason that saving to a file (even using the -append) flag didn't work is because the variable A already exists in the file and will be over-written each time through the loop. You would need to create a new file or new variable name every time through the loop in order for this to not happen.
Saving the results in a file is probably not the best way to store the time-varying values of A. You would be better off using a cell array to store all intermediate values of A.
A_over_time = cell();
for k = 1:n
%// Get A somehow
A_over_time{k} = A;
end
Depending on what A is, you could also store the values of A in a numeric array or matrix.
%// Using an array
A_over_time = zeros(N, 1);
for k = 1:N
A_over_time(k) = A;
end
%// Using a matrix
A_over_time = zeros(N, numel(A));
for k = 1:N
A_over_time(k,:) = A;
end

Pulling out specific column when sometimes, the array is empty

I am trying to pull out a column from inside a cell. However, sometimes, the cell is empty.
For example, if in this line, I try to pull out the last column inside PM25_win{i}, it sometimes has an array inside of size nx28. However, sometimes, the array is zero.
for i = 1:length(years)-1
PM25 = table2array(PM25_win{i}(:,end));
end
When the array is empty, the code stops and I get the error
Subscript indices must either be real positive integers or logicals.
How can I account for both cases so that the code will simply create the PM25 variable as an empty array if PM25_win{i} is empty?
You could simply add an if-else statement in the for loop.
for i = 1:length(years)-1
if isempty(PM25_win{i}(:,end))
PM25 = [];
else
PM25 = table2array(PM25_win{i}(:,end));
end
end

How to divide cell array into array and vector

This is my first time posting so i hope you can help me. I am trying to write a function in matlab.
I have laded data from a file into a cell array. First column contains statements and the second contains T for true og F for false. I now want to split this array into a cell array with the statements and a logical vector with 1 for True and -1 for false.
I use the fgetl within a loop to read all the lines into the cellarray
Try to write it a bit more neatly next time, and consider including a small example.
Here is what you seem to be looking for:
Suppose you have a matrix M and want to split that into M_true and M_false
M = {1,'T';
22,'F';
333,'T'}
idx_T=strcmp(M(:,2),'T')
M_true = M(idx_T,1)
M_false = M(~idx_T,1)

Resources