I am using textscan to read text file and I get <55x1 cell>
examples:
'aa a aa'
'a aaaa a'
'a = aaaaa'
'aaaaaa'
' a a a aaa'
'aa'
'aaa'
'aaaa'
.
.
.
.
I want to delete the white spaces in each sting
If I have a sting
string = 'I am 24 Years old'
And I use
string(ismember(string,' ')) = [];
it will eliminate the spaces and I will get
'Iam24Yearsold'
But with the cell doesn't work or I don't know how to do it
How can I do that? any suggestions please?
You can use strrep:
a = { 'aa a aa'
'a aaaa a'
'a = aaaaa'
'aaaaaa'
' a a a aaa'
'aa'
'aaa'
'aaaa'
'I am 24 Years old'};
strrep(a, ' ', '')
This results in
ans =
'aaaaa'
'aaaaaa'
'a=aaaaa'
'aaaaaa'
'aaaaaa'
'aa'
'aaa'
'aaaa'
'Iam24Yearsold'
Related
I'm trying to write a postgres function that will sanitize a list of numbers into a list of comma-separated numbers. These numbers are being entered into an input field. I want to allow users to just enter a line of space-separated numbers (ex: 1 3 4 12) and have it change it to 1,3,4,12.
But, if they do enter it correctly (ex: 1,3,4,12 or 1, 3, 4, 12), I still want it to sanitize it to 1,3,4,12. I also have to account for things like (ex: 1, 3 4, 12).
This is what I'm currently doing:
select regexp_replace(trim(list_of_numbers), '[^0-9.] | [^,]', ',', 'g')
If I have a list like this:
select regexp_replace(trim('1, 2, 4, 14'), '[^0-9.] | [^,]', ',', 'g')
it returns : "1,2,4,14" so that's good.
But, if I have a list like this:
select regexp_replace(trim('1 2 4 14'), '[^0-9.] | [^,]', ',', 'g')
it returns : "1,,,4"
If you change your regex to [^0-9.]+ it'll replace all non-numerics (i.e. , ,, ,) with a ,.
Try it out here
I think the best option is to convert to an array using regexp_split_to_array then turn that back into a string:
The following:
with t(input) as (
values
('1 3 4 12'),
('1,3,4,12'),
('1, 3 4, 12'),
('1,3,4,12'),
('1 3 4 , 12'),
(' 1, 2 , 4 12 ')
)
select array_to_string(regexp_split_to_array(trim(input),'(\s+)|(\s*,\s*)'), ',')
from t;
returns:
array_to_string
---------------
1,3,4,12
1,3,4,12
1,3,4,12
1,3,4,12
1,3,4,12
1,3,4,12
You could split the string on any amount of whitespace or commas (,|\s)+ and join it back together using commas:
select array_to_string(regexp_split_to_array('1 2 4 14', '(,|\s)+'), ', ');
jan 0.23%
feb 2.56%
mar 0.76%
apr 4.19% <-- This is the highest value
may -0.36%
jun -1.68% <-- This is the lowest value
jul 1.18%
aug -0.99%
sep -1.43%
nov 2.32%
dec 1.88%
What code can generate exactly the figure above without apostrophe and without being too complicated. Using for loops maybe? Note that the color of all letters and numbers is black.
I tried the following code :
C={'jan' 0.23 ' ';'feb' 2.56 ' ';'mar' 0.72 ' ';'apr' 4.19 '<-- This is the highest value';'may' -0.36 ' ';'jun' -1.68 '<-- This is the lowest value';'jul' 1.18 ' ';'aug' -0.99 ' ';'sep' -1.43 ' ';'oct' 2.00 ' ';'nov' 2.32 ' ';'dec' 1.88 ' '}
And i got this
'jan' [ 0.2300] ' '
'feb' [ 2.5600] ' '
'mar' [ 0.7200] ' '
'apr' [ 4.1900] '<-- This is the highes…'
'may' [-0.3600] ' '
'jun' [-1.6800] '<-- This is the lowest…'
'jul' [ 1.1800] ' '
'aug' [-0.9900] ' '
'sep' [-1.4300] ' '
'oct' [ 2] ' '
'nov' [ 2.3200] ' '
'dec' [ 1.8800] ' '
But it's not even close to what i want, it is very incomplete, contains apostrophes and so.
Firstly, you have to find max and min value in second columns.
Then try using ``printf()'' to print out the columns, at the max and min index print the text.
Sorry, i'm using mobile so I can't write the answer in detail.
I have this cell array
aitransp =
Columns 1 through 14
'27' '26' '25' '24' '23' '22' '21' '20' '19' '18' '17' '16' '15' '14'
Columns 15 through 21
'13' '12' '11' ' 9' ' 8' ' 7' ' 1'
As you can see, the last 4 elements have a space between the ' ', such as ' 9'.
Is there any way to remove any spaces occurring between '' in a cell array?
Thanks in advance..
Just use strtrim, you don't have to convert to string
strtrim(aitransp)
You can use regular expressions for greater flexibility:
result = regexprep(aitrasp, '(^\s*)' , ''); %// remove only leading space
result = regexprep(aitrasp, '(^\s*)|(\s*$)' , ''); %// remove leading or trailing space
result = regexprep(aitrasp, '\s' , ''); %// remove any space
for ii = 1:numel(aitransp)
aitransp{ii}(aitransp{ii} == ' ') = '';
end
This should do the trick.
I have a function that takes in the personalia of a person
function person = prompt_person ()
name = input ( ' Whats your name ? ' , 's' ) ;
day = input ( ' What day of the month were you born ? ') ;
month = input (' What month were you born ? ') ;
year = input ( ' What year were you born? ') ;
phone = input ( ' Whats your telephone number ? ') ;
date_of_birth = struct ('day', day, 'month', month, 'year', year) ;
person = struct ( 'name' , name, 'date_of_birth' , date_of_birth , 'phone' , phone ) ;
end
But I keep getting the error message "Invalid field name "name" ", "Error message in prompt_person (line 8) And I have no idea whats wrong because I've tried creating a simpler little test function:
function [out] = tes( )
word=input('Insert word here ','s');
num=input('Insert number here ');
out= struct('Number1', word, 'Number2', num);
end
And it works just fine, even though it seems to be the exact same code which gets me in toruble in the first function. Any ideas what's going on here?
I tried changing the variable name to personName as suggested and I accidentaly found out whats wrong:
Turn out I needed to use a variable name with no space between the '' and the text.
E.g. 'PersonName' instead of ' PersonName '.
You might consider using strtrim to strip leading and trailing whitespeace from a string. For example:
>> name = ' John Doe ';
>> name = strtrim(name)
name =
John Doe
If you need to remove all spaces, try strrep(name,' ','').
>> name = strrep(name,' ','')
name =
JohnDoe
I had a problem with scraping info from a web page, which was solved by someone, but i would also like to get the data from that structure and put it into normal string variables, i commented saying this and was told to create a new question, so here it is!
code:
use Web::Query 'wq';
my $football_matches = wq($mech->content)
->find('tr.match')
->map(sub {
my (undef, $e) = #_;
return 'v' eq $e->find('td.score')->text
? [
$e->attr('id'),
map { $e->find("td.$_")->text }
(qw(tournament dateTime homeTeam score awayTeam prices))
]
: ();
});
use Data::Dumper; print Dumper $football_matches;
output:
$VAR1 = [
['tn7gc635476', '', ' Mo 12Mar 2012 ', 'Arsenal', 'v', 'Newcastle', ' '],
['tn7gc649937', '', ' Tu 13Mar 2012 ', 'Liverpool', 'v', 'Everton', ' '],
['tn7gc635681', '', ' Sa 17Mar 2012 ', 'Fulham', 'v', 'Swansea', ' '],
['tn7gc635661', '', ' Sa 17Mar 2012 ', 'Wigan', 'v', 'West Brom', ' '],
['tn7gc635749', '', ' Su 18Mar 2012 ', 'Wolves', 'v', 'Man Utd', ' '],
['tn7gc635556', '', ' Su 18Mar 2012 ', 'Newcastle', 'v', 'Norwich', ' ']
];
What i would like to do is take each date, home team and away team and put them into normal variables/arrays.
Thanks in advance!
You may want to check perldsc and perlref. Look for the part arrays-of-arrays and arrayref