Storing integers in cell arrays in Matlab - arrays

I have a problem when using cell arrays in an m-file. I create a number of cell arrays using the function given in here. What I store on each of these arrays is numerical values being read from a text file (I convert them to string before I put them into the arrays). The problem is that the some numbers doesn't seem to be strored in the arrays correctly:
The text file contains that:
1976787196
8
1976945848
8
1977105448
8
And the contents of a cell array in which the above is store are the following:
Columns 1 through 3
1976787196 681405151445000 1976945848
Columns 4 through 6
685476780441608 1977105448 685476780441608
As you can see, instead of stroring 8, I get a very big integer.
Actually, I want all the contents of the cell arrays that I create to contain only 32-bit integers. Can I specify that requirement somehow? Thanks in advance!

It depends how you read the data from text file. Try using TEXTSCAN function. Format string '%u32' specifies that you want to read unsigned 32-bit integer data.
filename = 'test.txt';
fid = fopen(filename,'r');
x = textscan(fid,'%u32','delimiter','\t','CollectOutput',1);
x = x{1};
fclose(fid);

Related

MATLAB append two arrays(one string cell and one numeric vector) into a .csv

I have two arrays, X and Y
X has numeric data like this:
12342
1355
2324
...
Y has strings like this:
APPLE
METRO
BLANKET
...
I want to append these arrays into a pre-existing .csv in this format:
X(1,1), Y{1,1}
X(2,1), Y{2,1}
X(3,1), Y{2,1}
...
How should I do so?
unfortunately writing cell arrays to csvs can be tricky. One way or another you need to loop through so you can access the data within each cell. The reason for cells not writing automatically is that a cell can contain anything - like a whole matrix of data.
The most versatile way that will allow for any types of cell/matrix combos is to use fprintf - you just need to specify a format.
http://au.mathworks.com/help/matlab/ref/fprintf.html
nrows = size(x,1);
fid = fopen('newfile.csv','w');
for aa = 1 : nrows
fprintf(fid,'%f,%s\r\n', x(aa,1), y{aa,1});
end
you can use %d instead of %f if you are only chasing decimal numbers instead of floating point precision.
I have found fprintf to be faster than any of the alternatives for this sort of situation. It is a little more low-level but allows you to write anything you like to file.

Matlab MemMapFile for a three dimensional matrix

I have a three dimensional matrix called "Shanto" of size (232,232,3052).
I want to memory map this, and am using the following command:
fileID = fopen('Shanto.dat','w');
fwrite(fileID, Shanto, 'single');
fclose(fileID)
m = memmapfile('Shanto.dat')
However, when I try to access m.Data, I am given a 657083392 x 1 uint8 array.
How do I make it such that I can retain the (232,232,3052) shape of the original matrix?
Thanks,
Ben
When loading your .dat file you can specify the shape/format (the default is uint8 actually).
You also need to specify the correct data format using fwrite:
fileID = fopen('Shanto.dat','w');
fwrite(fileID, Shanto, 'uint8'); %// Instead of 'single' as before.
fclose(fileID)
m = memmapfile('Shanto.dat','Format',{'uint8',[232 232 3052],'MyFancyName'})
You can then access the corresponding 3D array using m.Data.MyFancyName
More info here

matlab cell contents , how to tell what they are?

I have a cell array, c, filled with hexadecimal data and when I view the cell contents by typing c at the matlab prompt, it shows me contents enclosed between ticks, i.e., '0x0009'. But, one element is enclosed in brackets and looks like [650345]. How can I convert the [ ] data to ' ' data? When I do iscellstr on this particular element, matlab returns 0. iscellstr returns 1 for all other elements of c.
I'm reading this data into matlab from excel and I fear that excel 'helped' me by converting one hex value to scientific notation. I can't, as far as I've found, change what excel did. I think the true value is lost and unrecoverable. But I need to convert this one outstanding value, even if incorrect, to be like the other cell values so that I can carry on with my processing. Any suggestions?
If you know the index of wrong value and it's true value, you just do:
c(idx) = {'0x0009'};
I think this does what you want:
ind = cellfun(#isnumeric, c); %// find numeric cells
c(ind) = cellfun(#(s) ['0x' dec2hex(s)], c(ind), 'uniformout', 0); %// convert to
%// hex string and prepend '0x'
Example: input
c = {'0x0009', 650345};
produces the output
c =
'0x0009' '0x9EC69'

cast an array into a string - MATLAB

I have an array that I want to basically capture as text so I can write it to one cell of an Excel file as a column header. It's a range of subjects, and I'll have some data underneath. So the range is:
range = 2:12;
which creates and array, but I want the Excel file header to just read 2:12. I've tried creating another variable to capture this text in one field, using num2str like this:
rangeChar = num2str(range);
and I get:
rangeChar = 2 3 4 5 6 7 8 9 10 11 12
but they are each separate fields, so when exported to Excel they each take up their own cell. The original range is not always sequential - for example I might have
range = cat(2, 2:4, 8, 9:12);
so I can't just do a
rangeChar = sprintf('%d:%d', range(1), range(end));
type of thing either. Any thoughts?
You can do it the other way around and keep the range in the string and extract the vector from that when you need it:
rangeChar = '2:12';
range = eval(rangeChar);
Couldn't you just write :
range = '2:12';
Use a cell array to hold "range" and use the following code :
range = {2:4, 8, 9:12};
range_str=repmat({''}, size(range));
for i=1:length(range)
if length(range{i})==1
range_str{i}=sprintf('%d', range{i});
else
range_str{i}=sprintf('%d:%d', range{i}(1), range{i}(end));
end
end
range_str
Output :
range_str =
'2:4' '8' '9:12'

Integer array to binary array

I have an integer array:
a=[3,4,5,6,7];
I want to convert it to a binary array with four bits each. For the above integer array I would like get the following binary array:
abinary=[0,0,1,1, 0,1,0,0, 0,1,0,1, 0,1,1,0, 0,1,1,1];
Is there any fast way to do it?
Matlab has the built-in function DEC2BIN. It creates a character array, but it's easy to turn that back to numbers.
%# create binary string - the 4 forces at least 4 bits
bstr = dec2bin([3,4,5,6,7],4)
%# convert back to numbers (reshape so that zeros are preserved)
out = str2num(reshape(bstr',[],1))'
You can use the BITGET function:
abinary = [bitget(a,4); ... %# Get bit 4 for each number
bitget(a,3); ... %# Get bit 3 for each number
bitget(a,2); ... %# Get bit 2 for each number
bitget(a,1)]; %# Get bit 1 for each number
abinary = abinary(:)'; %'# Make it a 1-by-20 array
A late answer I know, but MATLAB has a function to do this directly de2bi
out = de2bi([3,4,5,6,7], 4, 'left-msb');

Resources