Could someone assist in writing a char matrix to Excel? The following is a <17x6 char>
A =
SPK01a
SPK03a
SPK03b
SPK05a
SPK07a
SPK07b
SPK07c
SPK09a
SPK11a
SPK15a
SPK17a
SPK19a
SPK21a
SPK23a
SPK25a
SPK27a
SPK29a
I was trying to do xlswrite(filename, {A}) but receive the error:
Error using xlswrite (line 220)
ActiveX - Element of a cell array cannot be a character matrix.
I need the strings to be in cells, so in this case, in the first column in Excel, in 17 separate cells.
You can use cellstr to make a properly shaped cell array:
xlswrite(fileName,cellstr(A))
cellstr provides a quick way to place each row of a character array into a separate cell, and it will remove trailing spaces at the end of each row.
Use mat2cell to convert your matrix of characters into a cell array and use that to write into an excel file -
xlswrite(xls_filename,mat2cell(A,ones(1,size(A,1))))
This will write everything to the first column of the excel sheet, but in different rows.
Related
In Column M of a sheet called 'FF' , I have a series of values that can be 16+ numerical digits (numbers in the trillions). As a result, excel is losing precision and not properly displaying the values.
I have realized that if I manually put a single quote in the formula bar, it displays the number properly and saves the CSV properly as well. Is there a way to do this for all of column M if Column M already has numeric values populated?
Conceptually, I was hoping to do something like this where the single quote doesn't actually appear
Sub SingleQuote()
With ThisWorkbook.Worksheets("FF")
.Cells(2, 18).Resize(.Cells(.Rows.Count, 1).End(xlUp).Row).Formula = "=IF(Stage!M2="""","""",='M2"
End With
End Sub
I am creating an excel object from a vb.net listview table simply by creating an array of F(x,y), creating a range and putting the values from the array in the range as follows.
shXL.Range(Startcell,AEndCell).Value = F
However some of the fields are numeric and I want them to be formatted to two decimal places and EXCEL to recognize them as decimals
What I end up with in the excel worksheet is many green triangles telling me they are text fields.
How do I convert a range withing the sheet say A5,I20 to be formated as decimals.
I tried: (x,y).numberformat = "00.00" which works to format to 2dp but still treats the cells as text.
Furthermore, is it possible to Excel Sum a range? How is the possible?
Your help is appreciated!
shXL.Range(Startcell,AEndCell).Value = F
'// Loop over same range and convert to decimal
For Each cell In shXl.Range(Startcell,AEndCell)
With cell
.Value = CDec(.Value)
End With
Next
Furthermore, is it possible to Excel Sum a range? How is the possible?
I don't think Excel would have got very far as a spreadsheet product if it couldn't sum a range!
Assuming you want to do this in vb.net you need to use the instance of the application. I'll assume in your code it's XL
mySumValue = XL.WorksheetFunction.Sum(shXL.Range(Startcell,AEndCell))
a1=ac_tree_birch_NewYork_ext
a2=bc_animal_dog_Washington_des
How do I separate the text in the cells by the "_", since there is varying length of the cell values. I would like to use a formula, and not text to columns.
Thanks
Use the SUBSTITUTE function to change all underscores (e.g. CHAR(95)) to a large number of spaces (typically the entire length of the original string) and peel out the padded pieces with the MID function. Finish off with TRIM and an IFERROR 'wrapper'.
In B1 as,
=IFERROR(TRIM(MID(SUBSTITUTE($A1, CHAR(95), REPT(CHAR(32), LEN($A1))), (COLUMN(A:A)-1)*LEN($A1)+1, LEN($A1))), TEXT(,))
Fill both right and down.
This can likely be done via Flash Fill (Excel 2013+).
For the first row of data, enter your expected outcome in subsequent cells to the right. This is how you want the data broken up:
Then select your first cell of output data and click Flash Fill from the ribbon:
Do this for the remaining columns. This will fill the column based on the pattern recognized by Excel within your original data:
If a VBA solution is acceptable, you can write a wrapper around the VBA Split function:
Public Function Split2(s As String) As String()
Split2 = Split(s, "_")
End Function
Then in your worksheet, select (say) cells B1:F1, enter
=Split2(A1)
as an array function (CTRL-SHIFT-ENTER), and out comes your data.
Hope that helps.
I am trying to export a matrix from Matlab to export with xlswrite. However, my matrix is a cellarray that has strings such as '001', '00323'. When it is exported into Excel, Excel automatically converts them back to numbers and drops the first 2 zeros into '1', and '323'.
Does anyone know how to force excel to accept them as Text as them are being written from Matlab to xlsx?
Thank you!
L.
Excel probably likes to do this because it is exactly what Excel would do if you typed those values in.
One way to fix this is to put '"=001"' in the cell array rather than '001' like the following code. Note that Excel treats the values properly in the resulting file:
myCell= {1, '0001', '="0001"'};
xlswrite('test.xlsx', myCell)
You could write a little function that surrounds all the strings in a cell array with quotes, if needed:
function aCell = fixForExcel(aCell)
for ind = 1:numel(aCell)
myVal = aCell{ind};
if isstr(myVal)
aCell{ind} = sprintf('="%s"', myVal)
end
end
end
I run into this issue with SSNs. I don't know how much control you have with creating the spreadsheet with xlswrite.
You can set the format to Text, and this preserves the leading zeroes.
The problem will remain that when you open the spreadsheet, all leading zeroes will be eliminated. You can create a custom format that specifies that the format for the cell has two leading zeroes. You can use "\0\0#" as your custom expression. The text format will be saved after the first time.
If you have a variable number of 0s, the only way to get around it is to copy the data into excel.
I am writing a cell array of string into Excel from Matlab. I have a cell array data{} that I am trying to write into Matlab. It should writting three large lengths of strings to excel since the strcmp passes 3 times. Currently it is only writing the last set of strings into excel. data = { {1x25} {1x35} {1x20} } looks like this. Also I would like to be able to write the data into three cells instead of getting copyied into as many cells as there are lines in the element of the cell array. Is this possible to do with Matlab to excel?
done = {}
for i = 1:3
q = strcmp(x_gene_ID{i},locus_tags{i});
if q ==1
done{end+1} = data{i};
disp(done);
end
end
w = xlswrite('data.xlsx',done','E2:E400');
Ok that helps I am aware the cell array's are larger than 3 cell range. I am trying to get the Nx1 cell array to fit into one cell in Excel because It needs to correspond to information in an adjacent cell. Is this at all possible to do?
A B C D E
w Rv0146 na Rv0039c (i want the cell array1 to go here)
s Rv0156 na Rv0029c (i want the cell array2 to go here)
s Rv0156 na Rv0029c (i want the cell array2 to go here)
Here is what I am looking to do in excel
UPDATED ANSWER:
If I understand correctly, it appears that your variable data is a cell array where each cell contains a 1-by-N (or perhaps N-by-1) cell array of strings. If you want to try and fit each of these cell arrays of strings into one cell of a spreadsheet, you are going to need to format each into a single long string first.
Here's an example of how you could format the cell arrays of strings by concatenating them together with a newline between them:
data = {{'hello' 'hi' 'hey'} ... %# Sample cell array of 1-by-N
{'world' 'earth' 'everyone'} ... %# cell arrays of strings
{'blah' 'blah'}};
data = cellfun(#(x) {strcat(x,{char(10)})},data); %# Add newline characters
%# to the string ends
data = cellfun(#(x) {deblank([x{:}])},data); %# Concatenate the inner cells and
%# remove the trailing newlines
Now that each cell array of strings is just a single long string, each string can be written to a cell of an Excel spreadsheet as follows:
xlswrite('data.xls',data(:),'Sheet1','E2'); %# Write the data to cells E2 to E4
And here's what the resulting spreadsheet looks like:
If you use a space ' ' instead of a newline character, here's what the spreadsheet looks like (after adjusting the row and column widths):
Functions used in the above code: CELLFUN, STRCAT, CHAR, DEBLANK, XLSWRITE.