How to convert an string array into a character array in Matlab? - matlab

Suppose that we have an string array in Matlab like bellow:
a='This is a book'
How can we convert the above string array into a character array by a function in Matlab like bellow?
b={'T' 'h' 'i' 's' ' ' 'i' 's' ' ' 'a' ' ' 'b' 'o' 'o' 'k'}

Your a is not a string array; it's a character array (which also used to be called a string, but starting from R2016b that term has a different meaning). Your b is not a character array, it's a cell array that contains characters.
Anyway, to convert from a to b, use num2cell:
a = 'This is a book';
b = num2cell(a);

If you really really want to convert string (introduced since R2016b) to char array, this is how you do.
s = "My String"; % Create a string with ""
c = char(s); % This is how you convert string to char.
isstring(c)
ans =
logical
0
ischar(c)
ans =
logical
1

Related

How to convert 1d array of chars from CSV to 2d cell array in Matlab

I have a function I cannot change in Matlab that converts CSV file to a 1d array of chars.
This is an example of what it looks like:
array_o_chars = ['1' 'd' ',' ' ' 'a' 'r' 'r' 'a' 'y' '\n' 'o' 'f' ',' ' ' 'c' 'h' 'a' 'r' 's'];
I need to convert it to a 2d "Cell" array for the rest of my code, how do I do it?
The answer is surprisingly simple:
First, you want to break the char array into the "rows" of the CSV file. Depending on your line ending, you will choose one of these as a delimiter: \n, \r, or a combination of the two. For this example, we are using \n.
rows = split(array_o_chars, '\n');
Now that you have your rows, you need to create your columns, this is done in the same manner as your rows, except using , as your delimiter, or in this case , (a comma followed by a space).
cell_array = split(rows, ', ');
Now you have the 2d cell array you desire.
All together now:
% Test 1d array
array_o_chars = ['1' 'd' ',' ' ' 'a' 'r' 'r' 'a' 'y' '\n' 'o' 'f' ',' ' ' 'c' 'h' 'a' 'r' 's'];
% Conversion to cell array
rows = split(array_o_chars, '\n');
cell_array = split(rows, ', ');
% Show result in Command Window
display(cell_array);
Output from Matlab:
cell_array =
2×2 cell array
{'1d'} {'array'}
{'of'} {'chars'}

Replace every string containing '# '

I have a cell array containing strings like dataT1 below. How do I replace all strings containing a '#' with the letter 'O' and nothing else (no numbers in the string)?
dataT1 = {
[275.7770] [169.6630] [89.5380] [48.2740] [24.2400] [12.7510]
[284.3560] [160.4500] [87.3740] [47.4500] [23.9530] [12.4590]
'# 12.304' [129.7730] [66.2630] [34.1540] [15.1730] [ 9.6840]
[267.5270] [152.3700] '# 17.504' [45.2510] [23.3770] [13.0670]
[206.9110] [115.3030] [56.4770] [29.9350] [14.8680] '# 6.504' }
You can use a simple loop.
If you want to replace the # with O then use this:
for c = 1:numel(data)
% check for character array type in case cell also has numeric values
if ischar(data{c})
% replace hashes with 'O'
data{c} = strrep(data{c}, '#', 'O');
end
end
If you want to replace entire strings containing # with just the string 'O' then use this:
for c = 1:numel(data)
% check for character array type in case cell also has numeric values
if ischar(data{c})
% Search for # within string
if strfind( data{c}, '#' ) > 0
% replace string with 'O'
data{c} = 'O';
end
end
end

Effective way to convert/create matrix from mixed cell/string

Sometimes there might be more that one string located somewhere else, so I need a way to find everyone in the cell array. I have a cell array like the one below and I need a fast and effective way to 1) remove the empty columns, 2) convert the cells containing a string with "#" to the number after the "#" (6.504), and finally 3) create or convert the whole cell array to a data matrix like "data" below. Is there a smart way to do all this? Any suggestions are highly appreciated.
array ={
[47.4500] '' [23.9530] '' [12.4590]
[34.1540] '' [15.1730] '' [ 9.6840]
[45.2510] '' [23.3770] '' [13.0670]
[29.9350] '' [14.8680] '' '# 6.504'}
data =[
47.4500 23.9530 12.4590
34.1540 15.1730 9.6840
45.2510 23.3770 13.0670
29.9350 14.8680 6.5040]
Columns with mixed types are tricky to handle, but if the format always follows the regex pattern # \d+(?:\.\d+) you can proceed as follows:
C = {
47.4500 '' 23.9530 '' 12.4590
34.1540 '' 15.1730 '' 9.6840
45.2510 '' 23.3770 '' 13.0670
29.9350 '' 14.8680 '' '# 6.504'
};
% Get rid of empty columns...
C(:,all(cellfun(#ischar,C))) = [];
% Convert numeric strings into numeric values...
C = cellfun(#(x)convert(x),C,'UniformOutput',false);
% Convert the cell matrix into a numeric matrix...
C = cell2mat(C);
Where the convert function is defined as follows:
function x = convert(x)
if (~ischar(x))
return;
end
x = str2double(strrep(x,'# ',''));
end

What does '-' mean in matlab code

Can some one explain what this line here does? This is part of an old matlab code I need to reuse for my work
matdir = [params.ariens '-' num2str(dirtimes(ii))];
I'm especially confused about the '-' part. Thanks a lot in advance.
Single quotes are used to create a string literal so '-' simply creates a string containing the hyphen character. In MATLAB, [ ... ] performs horizontal concatenation so the line that you have shown concatenates the string stored in params.ariens, the character '-' and the number dirtimes(ii) converted to a string using num2str to creat one long string made up of those three strings.
For example:
c = ['abc', '-', 'def']
% abc-def
class(c)
% char
d = ['abc', '-', num2str(10)]
% abc-10

Read specific character from cell-array of string

I have an cell-array of dimensions 1x6 like this:
A = {'25_2.mat','25_3.mat','25_4.mat','25_5.mat','25_6.mat','25_7.mat'};
I want to read for example from the A{1} , the number after the '_' i.e 2 for my example
Using cellfun, strfind and str2double
out = cellfun(#(x) str2double(x(strfind(x,'_')+1:strfind(x,'.')-1)),A)
How does it work?
This code simply finds the index of character one number after the occurrence of '_'. Lets call it as start_index. Then finds the character one number lesser than the index of occurrence of '.' character. Lets call it as end_index. Then retrieves all the characters between start_index and end_index. Finally converts those characters to numbers using str2double.
Sample Input:
A = {'2545_23.mat','2_3.mat','250_4.mat','25_51.mat','25_6.mat','25_7.mat'};
Output:
>> out
out =
23 3 4 51 6 7
You can access the contents of the cell by using the curly braces{...}. Once you have access to the contents, you can use indexes to access the elements of the string as you would do with a normal array. For example:
test = {'25_2.mat', '25_3.mat', '25_4.mat', '25_5.mat', '25_6.mat', '25_7.mat'}
character = test{1}(4);
If your string length is variable, you can use strfind to find the index of the character you want.
Assuming the numbers are non-negative integers after the _ sign: use a regular expression with lookbehind, and then convert from string to number:
numbers = cellfun(#(x) str2num(x{1}), regexp(A, '(?<=\_)\d+', 'match'));