Creating a loop in MATLAB to split up a list of sentences by a delimiter - matlab

I want to be able to run a loop through my current code. I have a cell array with 423 cells. Each cell is a long string of smaller strings that I am trying to separate by the delimiter (;) and I want it to loop through each cell, split the string according to the delimiter and write the cell to the excel sheet i have created. But it only does the most recent cell rather than aggregating all of the cells.
I have an excel sheet (see screenshot), in column C I have lots of small sentences that are all separated with the delimiter ';'. I want the code to go through each row in excel and split up this big bunch of text so that each sentence between the delimiters has its own cell rather than being all together. I have managed to make the code work for one row, so the code seperates all of the small strings in to its own new row or new cell. But I cannot do this for every single row in excel, the code simply overwrites each one. I want to create a loop and iterate the process. I then want it to write this big list to a new excel sheet. I have tried to now create an iteration loop but it is not working correctly; it writes the strings to excel but it is not capturing all of them for some reason.
Here is the code as plaintext:
clc
clear
close all
T = readtable("********","range", "C1:C424");
C = table2cell(T);
for i=2:423
splitlist = unique(strsplit(C{i:i},';'))
writecell(splitlist','not_list.xlsx')
end

You need to add WriteMode', 'append' as input to writecell.
I have only tested this online, but it appears to work fine.
for i = 1:numel(C)
split_str = unique(strsplit(C{i},';'))
writecell(split_str, 'not_list.xlsx', 'WriteMode','append');
end

Related

How do I search for a value in a different sheet, possibly within a string of text

I have values I need to search for within another sheet (4 columns), the fourth column's cells containing strings of text. I need a formula that is able to search for and count how many times the value shows up in the other sheet, even within a string of text. Preferably I'd like it find the exact match of the text. Sometimes a cell will be 'abcd' and it will count 'abcd1.'
=COUNTIF(Sheet2!A:D, A30)
So far this only gets me the amount of times the value shows up when on its own in a cell, but not within the string of text. I believe there will also be some concatenating involved for some of the values. Suggestions?
try:
=COUNTIF(Sheet2!A:D, "*"&A30&"*")
or:
=INDEX(SUM(IFERROR(REGEXMATCH(Sheet2!A:D&"", ".*"&A30&".*")*1, 0)))

Copying Nattable cells and pasting in excel does not work properly

My nattable looks like the one below.
When I copy the cells and paste in excel the cells look distorted as below.
The line break is not captured properly in the process.
The bit of code referred for copying is the same as that in the Nattable example
Is this a bug and solved in the next versions or am I missing on something.
I would not say that this is a bug in NatTable. Pasting a line break into Excel inside a cell is not that simple. You can search for this topic and see the real issue. When you copy content from NatTable, the content includes the line breaks of your cell data. The paste operation in Excel takes those line breaks and interpretes them as new row and not new line inside a cell.
You can of course implement and register a custom CopyDataCommandHandler that performs special operations to replace a line breaks in NatTable content with something that Excel handles as line breaks inside a cell.
The solution as of now is as follows :-
In Excel if the content that is sent to the Clipboard is present in double quotes and the \n is included in the double quotes Excel interprets this as a single cell content and adds the line break in the cell
Alternatively since this is a table while copying content to the clipboard we can convert it to html tags appropriately and convert it to html format which excel reads and converts appropriately.
Refer below image
However while copying between cells from Nattable to Nattable this is taken care of.

Matlab dataimport

My matlab code for dataimport is giving me different results for what appear to be similar text files as input. Input1 gives me a normal cell with all lines from the text file as entries in the cell which i can reference using {i}.
Input2 gives me a scalar data structure where all numeric entries in my text file are converted to the input.data structure. I want all files to be converted to regular cell entries and I do not understand why for some files they are converted to scalar data structures.
Code: input = importdata(strcat(direct,'\',filename));
Input1 example: Correctly working dataimport, with text file on the right
File link: https://drive.google.com/open?id=1aHK4xivqEqJEmaA8Dv8Y0uW5giG-Bbip
Input2 example: Incorrectly working data import, with text file on the right FIle link: https://drive.google.com/open?id=1nzUj_wR1bNXFcGaSLGva6uVsxrk-R5vA
UTSL!
I'm guessing you are using GNU Octave although you are writing "Matlab" as topic of your question.
In importdata.m around line 178, the code tries to automatically detect the delimiter for your data:
delim = regexpi (row, '[-+\d.e*ij ]+([^-+\de.ij])[-+\de*.ij ]','tokens', 'once');
If you run this against W40A0060; you get A as delimiter because there is basically a number before and after it.
If you run this against W39E0016; you get {} as delimiter(empty) because the E could be part of a number in scientific notation and is therefore excluded.
Solution:
you really should add the correct delimiter to the importdata call and not trust that it's magically detected.
And if you just want the lines in a cell, use
strsplit (fileread ("W39E0016_Input2.txt"), "\n")
Analysis
This looks indeed strange!
EDIT: The cause for this strange looking behaviour has been deciphered by #Andy (See his solution).
When you use all outputs of importdata() function you can see what happens when reading the data:
[dat1,del1,headerrows1]=importdata('Input1.txt')
[dat2,del2,headerrows2]=importdata('Input2.txt')
For your first file it recognizes 69 header riws and no delimiter:
del1 = []
headerrows1 = 69
while in your second file only two header rows and a comma , delimiter is recognized
del2 = ','
headerrows2 = 2
I can not find an obvious reason in your files causing this different interpretation of data.
Suggestion
Your data format is rather complex. It is not a simple table like produced from excel. It has multiple lines with a different number of fields per line and varying data types. importdata() is not designed for this type of data. I suggest to write a specific import function for this kind of file. Have a look at textread() for a first guess. You can use it to read the lines of the files as text and later interpret it with sscanf() or use strsplit() to split the line contents into fields.

Split a string in SSRS if there is a space in between words

I have a field with multiple words in it. I want to separate the words onto different lines rather than have them separated by spaces.
So for example “Not Great”, I want to put “Not” on 1st line and “Great” on 2nd line, like so:
Not
Great
There could be words with “/” character in between i.e. “Great / Good” where I want to put everything after 1st word in 2nd line and everything after “/” in 3rd line i.e.
Great
/
Good
Basically, whenever there is space, I want split that string into multiple lines. How do I do that in SSRS?
Ok, you want the string broken up into different lines.
Do you mean on separate lines within the same tablix cell?
Thats straightfoward see
http://www.kodyaz.com/articles/reporting-services-add-line-break-between-words-custom-code.aspx
If you mean to split the string so the words are on different Tablix cells one approach would be to use a sub report on a list.
Set the list data set to the original data set containing the multiple word string, pass the string to the sub report as a parameter.
On the sub report pass the parameter to a data set that splits the string into individual lines.
Losts of suggestions for how to do that here
Turning a Comma Separated string into individual rows
Simply replace the space with a carriage return and line feed:
=Replace(Fields!SomeWords.Value, " ", vbCrLf)
=Fields!SomeFields.Value.Replace(Space(1), vbCrLf)

Inputing '\' before a character in a string Matlab

I'm trying to insert a '\' into a string array before each '_' in the string. The reason I am trying to do this is to keep the format of the sheetnames I pull from an xls file when i use the sheetnames in the legend of a plot. I tried using:
legend(sheetname{n},'Interpreter','none','Location','East')
but it just adds the two commands as two other labels in the legend. Hense this solution. I have looked into searching through sheetnames and have found how to replace, but I don't want to replace the characters if possible. If there is a way to do this please let me know.
Try this instead:
legend(sheetname(n),'Interpreter','none','Location','East');
Note the use of parentheses instead of curly braces, which passes a cell to LEGEND instead of the contents of the cell (i.e. cell indexing instead of content indexing). This should allow the additional parameter/value pairs to be properly interpreted.
If you want to apply a string replacement function on each cell element, you can do this as follows:
cellfun(#(x) strrep(x,'_','\_'),sheetname,'uniformoutput',false)
this applies strrep(x,'_','\_') to all elements of sheetnames and outputs it as a new cell array (because of 'uniformoutput',false).