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).
Related
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
Not sure what I am doing wrong here. I've seen online that characters in a string can be accessed using normal indexing (e.g a_string_variable(1:5)) however when I do this I get a 1x0 empty string array or even worst an error.
I've attached a screen shot of my Matlab command window below to show you what I'm doing and the error I am getting.
A string formed with the double quotes " is treated as if the entire string is one element of the variable. Kind of like a cell array. This is a type of classdef OOP object.
A string formed with the single quotes ' is treated as an array of characters, with each character being one element of the variable.
To get the behavior you are apparently wanting, use the single ' quotes.
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.
I have a file that looks like this in notepad++
I can easily see the spaces (being the orange dots), and tabs (being the orange arrows). I can also right click this in MATLAB and import it in a variety of ways. The problem is firstly the delimiters are not consistent. It seems to go TAB then some spaces to make sure the total field equals 6 characters...
The only way I understand reading a file in is if you already know how it is delimited. But in this case I would like to parse each line so MATLAB has some 'token' of what goes where eg:
Line1: Text Space Text Space Text Tab Space Space Text NEWLINE
(Notepad++ seems to know just fine so surely MATLAB can get this info too?).
Is this possible? Then it would be nice to use this information to save the imported data back out to a file with exactly the same formatting.
The data is below. For some reason copying this into notepad++ does not preserve its delimiting, you will need to add the tabs in yourself so it looks like the file in the screenshot.
Average Counts : 56.2
Time : 120
Thanks
If you use textscan, the default behaviour should probably suit your needs:
Within each row of data, the default field delimiter is white space. White space can be any combination of space (' '), backspace ('\b'), or tab ('\t') characters. If you do not specify a delimiter, textscan interprets repeated white-space characters as a single delimiter.
The output is a cell array, where each column is saved as a cell. So C{1} would contain the strings, C{2} the colons, and C{2} the values.
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)