I have a cell array myFile 637x16. The first row of the cell array is made of strings, because they will be the columns' labels in the .xlsx/.csv file.
From the second row on, the cell array is made of some columns with strings, and some columns with numbers.
I would like to export this cell array as a .xlsx or .csv file.
Here is an example of what I have:
'subject' 'PeakA' 'PeakL' 'number' 'epoch' 'code' 'type' 'latency' 'nitem' 'condition' 'ia' 'cover' 'variety' 'init_index' 'init_time' 'urevent'
5 3.50 82 13 1 201011 'pre' 2502 201 1 1 'y' 'h' 13 13.92 13
5 -1.27 112 55 2 61011 'pre' 8213 61 1 1 'y' 'h' 55 53.90 55
5 6.59 99 85 3 124011 'pre' 13924 124 1 1 'y' 'h' 85 82.45 85
5 12.65 105 127 4 178011 'pre' 19635 178 1 1 'y' 'h' 127 122.43 127
5 -0.35 105 157 5 89011 'pre' 25346 89 1 1 'y' 'h' 157 150.98 157
5 10.29 93 163 6 132011 'pre' 31057 132 1 1 'y' 'h' 163 156.69 163
5 4.61 65 193 7 166011 'pre' 36768 166 1 1 'y' 'h' 193 185.25 193
5 1.45 51 199 8 212011 'pre' 42479 212 1 1 'y' 'h' 199 190.96 199
I tried:
xlswrite('filename.xlsx', myFile);
but it gives me this error:
Warning: Could not start Excel server for export.
XLSWRITE will attempt to write file in CSV format.
> In xlswrite (line 174)
Error using xlswrite (line 187)
An error occurred on data export in CSV format.
Caused by:
Error using dlmwrite (line 112)
The input cell array cannot be converted to a matrix.
If you have a sufficiently recent version of Matlab (R2013b or older), writetable is your friend.
%# create a table
tbl = cell2table(yourCellArray(2:end,:),'variableNames',yourCellArray(1,:));
%# write to file
writetable(tbl,'filename.xlsx')
If you want to use xlswrite, it may be worth converting all data to string first, or to write the variable names separately, before you write the rest - I believe Matlab checks data types on the first row, which can cause typecast errors.
Related
I have a 1x1 structure (EEG) with 42 fields. One of these fields is called event and is a 1x180 structure, with 13 different fields, some of which are strings and some numeric values.
The 4th field of EEG.event is type and it contains strings (i.e. 'preo', 'pred', 'to', 'td', 'po', 'pd').
I would like to keep only those rows of the structure that contain 'preo' in the column EEG.event.type.
My ultimate aim is to create a matrix with all the columns from the structure EEG.event and only the rows with 'preo' in EEG.event.type, plus other columns from other variables.
So far I tried:
S = struct2table(EEG.event);
and it correctly returns a 180x13 table.
However I was not able to select only the rows with 'preo' in type. I tried:
A= S(S.type=='preo', :);
and it gives me an error:
Undefined operator '==' for input arguments of type 'cell'.
I also tried:
array(strcmp(S(:, 4), 'preo'), :) = [];
and it gives me this error:
Deletion requires an existing variable.
Then I thought that maybe I should have converted the table into matrix, to directly delete rows from the matrix. I tried:
B = cell2mat(S);
but it returns this error:
Error using cell2mat (line 42)
You cannot subscript a table using only one subscript. Table subscripting requires both row and variable subscripts.
Any suggestion or tip is welcome, because I don't know how to continue.
Example list that I have (only 18 rows here):
13 1 201011 'preo' 2502 201 1 1 'y' 'h' 13 13.9230000000000 13
14 1 201011 'pred' 2684 201 1 1 'y' 'h' 14 14.1049999960000 14
15 1 201012 'to' 2707 201 1 2 'y' 'h' 15 14.1280000000000 15
16 1 201012 'td' 2993 201 1 2 'y' 'h' 16 14.4140000000000 16
17 1 201013 'po' 3019 201 1 3 'y' 'h' 17 14.4400000000000 17
18 1 201013 'pd' 3383 201 1 3 'y' 'h' 18 14.8040000000000 18
55 2 61011 'preo' 8213 61 1 1 'y' 'h' 55 53.9000000000000 55
56 2 61011 'pred' 8522 61 1 1 'y' 'h' 56 54.2089999850000 56
57 2 61012 'to' 8547 61 1 2 'y' 'h' 57 54.2340000000000 57
58 2 61012 'td' 8834 61 1 2 'y' 'h' 58 54.5210000000000 58
59 2 61013 'po' 8858 61 1 3 'y' 'h' 59 54.5450000000000 59
60 2 61013 'pd' 9091 61 1 3 'y' 'h' 60 54.7780000000000 60
85 3 124011 'preo' 13924 124 1 1 'y' 'h' 85 82.4550000000000 85
86 3 124011 'pred' 14159 124 1 1 'y' 'h' 86 82.6899999990000 86
87 3 124012 'to' 14181 124 1 2 'y' 'h' 87 82.7120000000000 87
88 3 124012 'td' 14448 124 1 2 'y' 'h' 88 82.9790000000000 88
89 3 124013 'po' 14470 124 1 3 'y' 'h' 89 83.0010000000000 89
90 3 124013 'pd' 14713 124 1 3 'y' 'h' 90 83.2440000000000 90
Example list that I would like to have (from the 18 rows above):
13 1 201011 'preo' 2502 201 1 1 'y' 'h' 13 13.9230000000000 13
55 2 61011 'preo' 8213 61 1 1 'y' 'h' 55 53.9000000000000 55
85 3 124011 'preo' 13924 124 1 1 'y' 'h' 85 82.4550000000000 85
I found a solution, I post it here for others with my same issue.
I first create a cell array, and then I delete rows from the cell array. At the moment is the best I can think of.
myCell= struct2cell(EEG.event);
%it results in a 3d cell array, with the fields as first dimension (42) x a singleton dimension as second dimension x the number of the rows as third dimension (180)
new_Cell = permute(myCell,[3,1,2]);
%it deletes the singleton dimension and swap the other 2 dimensions, obtaining 180x42.
[r,c] = find(strcmp(new_Cell,'preo'));
%indices as rows (r) and columns (c) of cells with the string 'preo'
y = new_Cell([r],:);
%It keeps only the rows that you want from the original cell array 'myCell'.
I have a none rectangular text file like A which has 10 values in first line, 14 values in 2nd line, 16 values in 3rd line and so on. Here is an example of 4 lines of my text file:
line1:
1.68595314026 -1.48498177528 2.39820933342 27 20 15 2 4 62 -487.471069336 -517.781921387 5 96 -524.886108398 -485.697143555
Line2:
1.24980998039 -0.988095104694 1.89048337936 212 209 191 2 1 989 -641.149658203 -249.001220703 3 1036 -608.681762695 -300.815673828
Line3:
8.10434532166 -4.81520080566 4.90576314926 118 115 96 3 0 1703 749.967773438 -754.015136719 1 1359 1276.73632813 -941.855895996 2 1497 1338.98852539 -837.659179688
Line 4:
0.795098006725 -0.98456710577 1.89322447777 213 200 68 5 0 1438 -1386.39111328 -747.421386719 1 1565 -1153.50915527 -342.951965332 2 1481 -1341.57043457 -519.307800293 3 1920 -1058.8828125 -371.696960449 4 1303 -1466.5802002 -308.764587402
Now, I want to load this text file in to a matrix M in Matlab. I tired to use importdata function for loading it
M = importdata('A.txt');
but it loads the file in a rectangular matrix (all rows have same number of columns!!!) which is not right. The expected created matrix size should be like this:
size(M(1,:))= 1 10
size(M(2,:))= 1 14
size(M(3,:))= 1 16
How can I load this text file in a correct way into Matlab?
As #Jens suggested, you should use a cell array. Assuming your file contains only numeric values separated by whitespaces, for instance:
1 3 6
7 8 9 12 15
1 2
0 3 7
You can parse it into cell array like this:
% Read full file
str = fileread('A.txt');
% Convert text in a cell array of strings
c = textscan(str, '%s', 'Delimiter', '\n');
c = c{1};
% Convert 'string' elements to 'double'
n = cellfun(#str2num, c, 'UniformOutput', false)
You can then access individual lines like this:
>> n{1}
ans =
1 3 6
>> n{2}
ans =
7 8 9 12 15
I have a matrix of 3 digit numbers, for example
102 106 100 100 100 100 100
106 102 100 100 100 100 100
106 101 120 106 109 119 108
104 115 107 106 109 119 108
I would like to combine each row into a single number, like so
102106100100100100100
106102100100100100100
106101120106109
...etc. I would really appreciate any feedback. Thank you :)
I assume the input is a numeric 2D array.
If you want the result in string form (2D char array where each row represents a number):
result = num2str(A, '%i'); %// or change format specifier if the numers are not naturals
If you want the result in numeric form (column vector of numbers):
result = str2num(num2str(A, '%i'));
I'm having an array and when I apply find(im), I get indices for non zero elements. But, I want indices for all elements of array irrespective whether it is zero or non zero.
Here is my array:
im =[94 122 99 101 111 101;
99 92 103 87 107 116;
93 109 113 84 86 106;
5 17 6 54 56 53;
13 11 5 56 44 50;
0 10 5 49 42 51];
when I apply find(im): I get indices: 35(Since the array contain 0 in it). But I need to get 36.
How do i do it?
Since you want the linear indices of all elements in the array, and you know the number of elements in the array, their indices will be:
im = magic(5);
indices = 1:numel(im)
I.e. if you were to loop the array you would be looping all of the elements.
I had two arrays, (data1 is a header array of strings and data2 is the data array of numbers)
data1 = {'#','Area','C Xp','C Yp','Length','B #','R','L','Ch','E1 Xp','E1 Yp','E2 Xp','E2 Yp'};
data2 = [1 939 -397 586 99 2 2 0 -1 -450 588 -352 572
2 1185 -287 294 145 2 1 1 0 -317 359 -235 244
3 592 -242 486 77 3 2 1 0 -278 488 -202 477
4 818 -144 480 60 2 0 2 1 -181 488 -135 451
5 377 -23 -443 37 1 0 1 0 -42 -459 -12 -460
6 923 32 -234 67 1 0 0 0 -3 -260 60 -212
7 812 150 -148 54 1 0 1 0 136 -130 169 -161
8 5968 428 432 402 3 3 0 -1 224 468 622 356
9 617 714 13 63 1 0 1 0 687 35 702 -22
csvwrite('file.xlsx', data1, 0, 0);
csvwrite('file.xlsx', data2, 0, 1);
My first problem is data1 prints to the spreadsheet as an array of chars (example: '#','A','r','e','... each in their own cells). How do I get it to print as the strings I am passing?
My second problem is when I csvwrite data2, data1's info is erased or overwritten. How can I write both to the same file?
Hidden away in the Tips section of the csvwrite documentation:
csvwrite does not accept cell arrays for the input matrix M. To export a cell array that contains only numeric data, use cell2mat to convert the cell array to a numeric matrix before calling csvwrite. To export cell arrays with mixed alphabetic and numeric data, where each cell contains a single element, you can create an Excel® spreadsheet (if your system has Excel installed) using xlswrite. For all other cases, you must use low-level export functions to write your data. For more information, see Export Cell Array to Text File in the MATLAB® Data Import and Export documentation.
I'd say, use xlswrite.
If you can't use xlswrite, it looks like you are stuck doing it manually as described on the page, Export Cell Array to Text File. Something along the lines of:
% write headers
fid = fopen('test.csv','w');
fprintf(fid,'%s,',data1{:});
fprintf(fid,'\n');
% write data...
fprintf(fid,[repmat('%d,',1,numel(data1)) '\n'],data2);
fclose(fid)