In MATLAB (R2015b) I have a cell C (1x4 cell, with 4 columns):
C = [4x1 double] [4x1 double] {4x1 cell} {4x1 cell}
And when I call C I get the printout above, but I want MATLAB to write it as:
[10] [31.2] 'TRS' 'BLU'
[12] [34.2] 'TRS' 'BLU'
[13] [33.5] 'TRS' 'BLU'
[14] [34.8] 'TRS' 'BLU'
I.e. a 4x4 cell. How can I get this printout? Thanks
You need to convert the first two elements to cell arrays and then you can horizontally contatenate them and display them
[num2cell(c{1}), num2cell(c{2}), c{3:end}]
I think this can work
for i=1:numel(c)
fprintf('%g %g %s %s\n',c{1}(i),c{2}(i),c{3}{i},c{4}{i})
end
Related
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 have a text file.
In the file is approx 20,000 rows of data. Each row has one column & contains 256 characters (which are all numbers).
I need to split each row into a cell array or matrix. So each 8 characters are "one piece" of information. I want to split the first 3 characters into a cell array and the next 5 characters into a double, then same again for the next 8 characters.
example
1653256719812345
myCellArray (1 x 2) myDoubleArray (1 x 2)
[165, 198] [32567, 12345]
What is the best way to do this?
Use textscan.
fid = fopen(MyFileName.txt);
data = textscan(fid, '%3d%5d', 'Delimiter', '');
fclose(fid);
testing:
% Test with string of 256 random digits that all happen to be 1:8 repeated 32 times
x = '1234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678';
>> y = textscan(x, '%3d%5d', 'Delimiter', '')
y =
[32x1 int32] [32x1 int32]
>> y{1}
ans =
123
123
123
123
...
I don't know the exact format of your files, so you may have to do this line-by-line within a loop (in which case you would get each line using fgetl and then replace fid in the textscan statement with the output from fgetl).
In general, whenever you find yourself having to read in data that was produced by FORTRAN code (fixed field width text files), textscan's 'Delimiter, '' and 'Whitespace', '' parameters are your friend.
Use regexp. If the file data.txt contains
1653256719812345
1563256719812345
1233256719812345
1463256719812345
Then the following MATLAB statements will read the numbers.
>> txt = fileread('data.txt') % Read entire file in txt
>> out = regexp(txt,'(\d{3})(\d{5})(\d{3})(\d{5})','tokens') % Match regex capturing groups
out =
{1x4 cell} {1x4 cell} {1x4 cell} {1x4 cell}
Each cell in out is a row from the file containing the parsed numbers as strings.You can use str2double to convert the numbers to a numeric data type in MATLAB
>> nums = cellfun(#str2double,out,'uni',0)
nums =
[1x4 double] [1x4 double] [1x4 double] [1x4 double]
Iterate over your rows one by one and run something like the following code.
k=int2str(1653256719812345);
> myCellArray{1}=k(1:3)
myCellArray =
'165'
>> mydoublearray(1)=str2num(k(4:9))
mydoublearray =
325671
If there's some formulaic pattern you should incorporate that instead of manually hard coding it.
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 an cell including array as below format
a{x,y,z}(i,j)
a is 3 dimensional cell
and
each cell have i*j array
a <79x95x68 cell>
val(:,:,1) =
Columns 1 through 2
[6x6 double] [6x6 double]
[6x6 double] [6x6 double]
[6x6 double] [6x6 double]
i want to rearrange that as below format
a{i,j}(x,y,z)
how to? any good idea? i have to do iteration?
matlab say, a{:,:}(x,y,z) is bad cell referencing.........
here's a suboptimal way to go, it isn't memory efficient, but it is pretty straightforward:
say that x=79,y=95,z=68 are the dimensions of your original cell array, and that each of them gives the same dimensionality ixj matrix (6 by 6). So first you can make a matrix out of the cell array:
b=horzcat(a{:});
then we can reshape it to a 5-d array (this is just for pedagogical purposes) so you already have it ordered as (i,j,x,y,z)...
c=reshape(b,6,6,x,y,z);
then you can either work with c(i,j,x,y,z), or assign c to a cellarray:
d=cell(6,6);
for i=1:6
for j=1:6
d{i,j}=squeeze(c(i,j,:,:,:));
end
end