append cell row to matrix - excel

I'm reading an excelfile in matlab with
[NUM,TXT,RAW]=xlsread(DATENEXCEL,sSheet_Data);
In the excelfile are different datamatrices in different sheets in the following form
Date Firm1 Firm2 Firm3 ...
1.1.16 12 12 12
... ... ... ...
Currently I'm handling the pure data with the NUM object and the header row with the TXT object. My first issue is how to combine the header row with the data rows. Looping does not work, since I predefine the data matrix with
daten=zeros([length(sDatesequence) size(RAW,2)]);
because I want to be able to add more data from different sources to that object. Predefining with zeros, however, leads Matlab to expect doubles and not characters. Converting the cell array TXT with cell2mat delivers unsatisfying results:
cell2mat(TXT(1,:))=Firm1Firm2Firm3...
hence only a long string vector.
Question: Is there another way to combine character vectors and double matrices?
Regards,
Richard

You can combine them in a cell array.
c{1,1} = 'Firm1';
c{1,2} = datavector;
c{2,1} = 'Firm2';
c{2,2} = datavector;
But as far as I know it is not possible to add text headers to a numerical matrix, unless you do something with typcasting. But I would not recommend that.
d(1:8)='Firm1 '; %must have exactly eight characters (a double has a length of 8 bytes)
y = typecast(uint8(d),'double') %now you have a number that would fit in a matrix of doubles
x=char(typecast(y,'uint8')) %now it's converted back to text

Related

Import/convert list of space separated numbers to list of arrays in Matlab

I'm new to Matlab and I want to convert a column with space separated numbers (source is an Excel file) to a list of arrays.
In a first step I want to create a list of arrays like this:
Then I want to transpose the list like this:
Whats the correct command for this conversion?
I know it's a simple question, but I couldn't find a similar one.
First use xlsread to read in the raw text. The text will be read in as a cell array where each row of text is placed in a cell. Once you do this, it's a matter of splitting up the strings by spaces to create an additional cell array of cells per row, then inputting these cells into a function that creates an array of numbers. You can use cellfun combined with strsplit and str2double. Assuming your Excel file is called list.xls, do something like this:
[~,~,RAW] = xlsread('list.xls');
list = cellfun(#str2double, cellfun(#strsplit, RAW, 'uni', 0), 'uni', 0).';
list contains the desired output. I've also transposed the result as this is what you desire. I created an Excel file that's in the same fashion as how you've mentioned in your post. This is what I get when I run the code. First I'll show what list looks like, then we'll examine what the actual contents are:
>> list
list =
[1x4 double] [1x5 double] [1x6 double]
>> celldisp(list)
list{1} =
5405 5414 5420 9999
list{2} =
5405 5414 5430 5341 9999
list{3} =
5405 5419 5419 5419 5412 9999
Here's also what the MATLAB Variable Editor looks like:

How to print a number within a string in matlab

I would like to use the command text to type numbers within 57 hexagons. I want to use a loop:
for mm=1:57
text(x(m),y(m),'m')
end
where x(m) and y(m) are the coordinates of the text .
The script above types the string "m" and not the value of m. What am I doing wrong?
Jubobs pretty much told you how to do it. Use the num2str function. BTW, small typo in your for loop. You mean to use mm:
for mm=1:57
text(x(mm),y(mm),num2str(mm));
end
The reason why I've even decided to post an answer is because you can do this vectorized without a loop, which I'd also like to write an answer for. What you can do place each number into a character array where each row denotes a unique number, and you can use text to print out all numbers simultaneously.
m = sprintfc('%2d', 1:57);
d = reshape([m{:}], 2, 57).';
text(x, y, d);
The (undocumented!) function sprintfc takes a formatting specifier and an array and creates a cell array of strings where each cell is the string version of each element in the array you supply. In order to ensure that the character array has the same number of columns per row, I ensure that each string takes up 2 characters, and so any number less than 10 will have a blank space at the beginning. I then convert the cell array of strings into a character array by converting the cell array into a comma-separated list of strings and I reshape the matrix into an acceptable form, and then I call text with all of the pairs of x and y, with the corresponding labels in m together on the screen.

How can I convert a cell array to a sparse matrix?

I'm trying to convert an Excel spreadsheet (link) to a MATLAB sparse matrix.
How can I import it to MATLAB as a working sparse matrix? So far, all I've been able to do is convert it to a cell array...
I tried to convert my name matrix first with this function:
function [names,links]=Changing(WorldReligion)
i=0;
names={};
for i=1:337
if WorldReligion(i,1)~=WorldReligion(i+1,1)
names(i)=WorldReligion(i,1);
end
end
However, I get this error:
[names , links]=Changing(WorldReligion) Undefined function 'ne' for
input arguments of type 'cell'.
Any help would be appreciated.
That's a short answer because it looked bad as a comment, but if you want to fetch the unique religion names from your spreadsheet, you can use the unique function which works on cell arrays of strings. In your case to get the 1st column of the spreadsheet, use this:
clear
clc
[~, text, ~] = xlsread('WorldReligion.xlsx');
UniqueNames = unique(text(:,1));
whos
Output of whos:
Name Size Bytes Class Attributes
UniqueNames 117x1 15654 cell
text 388x10 452500 cell
This retrieves the 117 unique names form the 1st column of the 'text' data, which I think is what you wanted to do with the above for-loop. Is that the case?

How can I write looping data matrix into same excel file?

I have a looping data matrix in matlab, and I want to write it into same excel file. If I use the xlswrite('name.xls', M) it'll make a 'name.xls' excel, contained one matrix (value of the last looping). How can I write all my looping matrix (lets say it has 10 matrix with 13 columns) in one file excel and that excel file will contain all my matrix with 13 columns (so there'll be 10 rows with 13 columns). Please help, Thanks. -Machmum
In each loop iteration add your newest vector to a single matrix. Then only after the loop, write this matrix to a .xls file:
M = zeros(10,13); %// Pre-allocation like this is essential for speed
for k = 1:10
... %Your code
M(k,:) = ... %//Put your new 1-by-13 vector that you create each iteration here
end
xlswrite(file_name, M)
Although it would be better to create the matrix first and then write it in one go as Dan has suggested, it is possible to specify where xlswrite starts writing, which allows you to append data to existing files. If only specifying a start location, you must also give xlswrite a sheet name. This will be slower than pre-calculating the matrix and then just calling xlswrite once, though.
Simple example:
M = 1:10;
for n = 1:10
t = sprintf('A%d',n); % starting cell A1 through A10
xlswrite('testdata.xls',M*n,'Sheet1',t); % writes one row
end

Data storing in haskell - excel like rows and cols

I have a following quest:
I have to write program in Haskell which will allow me to create something like excel sheet.
There are columns and rows, and each cell can hold number or string or some function (sum, mean, multiply etc). Each of the functions take as parameters a list of cells which are summed etc.
Now I am trying to figure out how to store this data into my program...
I was thinking about something like this:
data CellPos = CellPos Int Int -- row and col of Cell
data DataType = Text | String | SumFunction | ...... deriving (Enum)
data Cell = Cell CellPos DataType -- but here is a problem , how to put here data with type which depends on DataType???
I wanted just to have big list of Cell and search in it for specified column/row etc
But there must be some better solution for this – maybe some two dimensional array which auto adjust its size or something?
I will have to save/load a sheet to /from file...
Let's answer one question at a time:
data Cell = Cell CellPos DataType
"but here is a problem , how to put here data with type which depends on DataType???"
Put that data into DataType:
data DataType = Text String | Number Double | Function CellPos (DataType -> DataType)
"I wanted just to have big list of Cell and search in it for specified column/row etc. But there must be some better solution for this - maybe some two dimmensional array which auto adjust its size or something?"
I suggest a Map CellPos DataType.
"I will have to save/load a sheet to /from file..."
The simplest thing will probably be to derive Show and Read and use the resulting functions together with readFile and writeFile. The only caveat here (with respect to DataType as defined earlier in this answer) is that functions cannot be serialized. To get around this, make a more explicit type for the functions in cells -- perhaps an abstract syntax tree for some simple expression language.

Resources