I have a problem converting my results to a csv file.
My cell is like this:
[1403x36 double] [1290x36 double] [1813x36 double] [1363x36 double] [1286x36 double]
[1355x36 double] [1194x36 double] [1130x36 double] [1277x36 double] [1494x36 double]
[1447x36 double] [1455x36 double] [1817x36 double] [1434x36 double] [1536x36 double]
I want my CSV file to have (rows x 36).
I tried already cell2csv and i did a loop of fprint also, but neither of them worked.
Thank you in advance.
As with the other users who have commented, it's not clear to me what the desired structure or ordering of the contents of your CSV should be, but here is an example workflow that assumes you would be comfortable first reshaping your cell array into an Q x 1 size (in column-major order), and then concatenating the contents in each cell into an Mx36 matrix.
myCell = {rand(10,36),rand(5,36); rand(4,36),rand(7,36)};
myCell = myCell(:);
myMatrix = cell2mat(myCell);
csvwrite('filename.csv',myMatrix);
I am working on a Semi Markov model. In it my task is to find the interval transition probability which is a recursive procedure.
The code that I used is below. For this to run the initial condition is F(0)=eye(3,3) matrix.
I am unable to call this initial value in a loop. Is the code that I have written is right? I need a proper suggestion.
Other data used is
C =
Columns 1 through 4
[3x3 double] [3x3 double] [3x3 double] [3x3 double]
Column 5
[3x3 double]
Y =
Columns 1 through 4
[3x3 double] [3x3 double] [3x3 double] [3x3 double]
Column 5
[3x3 double]
The code:
F=mat2cell(zeros(3,15),[3],[3,3,3,3,3])
for j=1:5
for m=1:j
if (j-m)==0
F{:,j}=eye(3,3)
end
F{:,j}=Y{:,j}+sum(C{:,m}*F{:,(j-m)})
end
end
While you overwrite F{:,j} when j-m==0, you still try to access F{:,(j-m)} later on. Doesn't it say "Cell contents indices must be greater than 0" or "Subscript indices must either be real positive integers or logicals."? Matlab arrays and cells are indexed from 1 upwards.
You might need something like
if (j-m)==0
F{:,j}=eye(3,3);
else
F{:,j}=Y{:,j}+sum(C{:,m}*F{:,(j-m)});
end
Don't forget your semicolons or you'll get loads of unnecessary output.
And do you really need cells? What you wrote could easily be implemented using multidimensional arrays, in which case your colons are actually needed. In your specific example, I think they are redundant, it would suffice to write, for instance, F{j-m}.
How should I copy all my cell content in a column to another column in the same cell. e.g.
a{1,1}=[1 2];
a{2,1}=[3 4 5];
a =
[1x2 double] []
[1x3 double] []
then,I'd like to copy all cell contents of this column to another column say column 2 without copying all rows separately using for. I used
a{:,3}= a{:,2}
The right hand side of this assignment has too few values to satisfy the left hand side.
it seems that a{:,2} is not working as it returns different values in different run. So here : doesn't work?
As an output I'd like to have the same elements as copying cells to my new cell homes. i.e.
a{1,2}=[1 2];
a{2,2}=[3 4 5];
So, a will be
a =
[1x2 double] [1x2 double]
[1x3 double] [1x3 double]
You need to use brackets instead if braces in this case.
Try it like this:
a = {[0 1];[2 3];[4 5];};
a(:,2) = a(:,1);
I have a double value, A, which is
[1,4,7,6]
I also have B, which is an array that contains many more values. I have a new variable, C, which is essentially a double value of all these numbers (all of them in one cell, vs. five).
[1,4,7,6]
[2,6,9,12]
[3,1,17,13]
[5,7,13,19]
[1,5,9,15]
How do I remove the elements (not the actual values) from C? I want to end up with this.
[2,6,9,12,3,1,17,13,5,7,13,19,1,5,9,15]
How do I get this? I've used these commands:
C(A) = [];
and
C = C(setdiff(1:length(C),A));
The problem is that when I run that command, I get this instead of what I want.
[4,7,2,12,3,1,17,13,5,7,13,19,1,5,9,15]
Clearly that isn't the same as what I have. It's throwing off the rest of my results and I need to fix this specific issue.
Thanks in advance :)
EDIT:
So I figured out that it's spewing the CORRECT numbers out, just in the wrong order. I have to sort it in order for it to work correctly. This is a problem because it causes the next command to be non-functional because the ismember command has issues with the removal (I don't know why, I'm still working on it).
Double array case
If B is a double array, you can use setdiff with 'rows' and 'stable' options, like so -
C = reshape(setdiff(B,A,'rows','stable').',1,[])
With ismember, you can perform the same operation, like so -
C = reshape(B(~ismember(B,A,'rows'),:).',1,[])
You can also use a bsxfun approach as suggested by #Amro -
C = reshape(B(~all(bsxfun(#eq, B, A),2),:).',1,[])
Cell array case
If B is a cell array with number of elements in each cell equal to the number of elements in A, then you can firstly convert it to a double array - B = vertcat(B{:}) and then use either of the above mentioned tools.
Or you can use a cellfun based approach that avoids conversion to a double array, like so -
excl_rows = B(~cellfun(#(x1,x2) isequal(x1,x2), B, repmat({A},size(B,1),1)),:)
C = horzcat(excl_rows{:})
Or another cellfun based approach that avoids repmat -
exclB = B(~cellfun(#(x1) isequal(x1,A), B),:)
C = horzcat(exclB{:})
Example with explanation -
%// Inputs
A = [1,4,7,6]
B = {[1,4,7,6]
[2,6,9,12]
[3,1,17,13]
[5,7,13,19]
[1,5,9,15]}
%// Compare each cell of B with A for equality.
%// The output must be a binary array where one would be for cells that have
%// elements same as A and zero otherwise.
ind = cellfun(#(x1) isequal(x1,A), B)
%// Thus, ~ind would be a binary array where one would reperesent unequal
%// cells that are to be selected in B for the final output.
exclB = B(~ind)
%// exclB is still a cell array with the cells that are different from A.
%// So, concatenate the elements from exclB into a vector as requested.
C = horzcat(exclB{:})
Output -
A =
1 4 7 6
B =
[1x4 double]
[1x4 double]
[1x4 double]
[1x4 double]
[1x4 double]
ind =
1
0
0
0
0
exclB =
[1x4 double]
[1x4 double]
[1x4 double]
[1x4 double]
C =
2 6 9 12 3 1 17 13 5 7 13 19 1 5 9 15
I'm trying to convert a .mat file into csv, preserving the vector/variable names.
This is one example of what I'm dealing with:
mymat =
model_id: [2217x1 double]
own_dummies: [2217x26 double]
id: [2217x1 double]
product: [2217x1 double]
const: [2217x1 double]
mpd: [2217x1 double]
air: [2217x1 double]
mpg: [2217x1 double]
trend: [2217x1 double]
space: [2217x1 double]
hpwt: [2217x1 double]
cdindex: [20x1 double]
cdid: [2217x1 double]
outshr: [2217x1 double]
firmid: [2217x1 double]
share: [2217x1 double]
price: [2217x1 double]
I've tried using csvwrite('test.csv', mymat) but it gives me an error:
??? Undefined function or method 'real' for input arguments of type 'struct'.
Error in ==> dlmwrite at 192
str = sprintf('%.*g%+.*gi',precn,real(m(i,j)),precn,imag(m(i,j)));\
Error in ==> csvwrite at 32
dlmwrite(filename, m, ',', r, c);
I guess the problem is that I'm feeding csvwrite with a struct instead of a matrix.
I can convert the struct variable to variable to a matrix, but then I would loose the variable names.
Surely there is a better way?
how would you actually map the struct to a csv? you cannot do a straight forward map of fieldnames as entries on first line, because the matrices have incompatible dimensions. So this example you'd have to 1. introduce several columns for own_dummies and 2. expand the cdindex.
So you could write a wrapper to make the matrices themselves compatible and to write the columns yourself into the file. something along the (untested,conceptual) lines of
function saveData(filename, data, type)
% at first bring struct data to sensible format
if strcmp(type)='my_mat_type'
data.own_dummies2 = data.own_dummies[2,:]; % split into seperate columns
%...
data.own_dummies26 = data.own_dummies[26,:];
data.own_dummies = data.own_dummies[1,:];
data.cdindex = [data.cdindex -ones(1,2217-length(data.cdindex)]; % pad any missing values as -1
end;
FD = fopen(filename, 'w');
%todo did it open?
fields = fieldnames(data);
nfields = length(fields);
% create column name values
columns = strcat(strcat(fields,',')); % creates string = col1,col2,...coln,
columns = columns(1:length(columns)-1); %remove trailing comma
CRNL = char([10 13]); % or so
% print columns and newline
fprintf(FD,strcat(columns,CRNL));
dataout = cell2mat(struct2cell(data));
% use your method to write the data to the file
fclose(FD);
naturally, you could also just split the individual fields into separate files where the fieldname is contained within the filename, which might be simpler, generally speaking.