Matlab: convert int array into string array? - arrays

In Matlab I have integer array a=[1 2 3]. I need to convert them into one string, separated by ',':
c = '1,2,3'
If somehow I can have a string array b=['1' '2' '3'], then I can use
c = strjoin(b, ',')
to achieve the goal.
So my question is: How to convert integer array a=[1 2 3] into a string array b=['1' '2' '3']?
The int2str() is not working. It will give out
'1 2 3'
and it is not a "string array", so the strjoin can not apply to it to achieve '1,2,3'

You can simply use sprintf():
a = 1:3;
c = sprintf('%d,',a);
c = c(1:end-1);

There's a function in the file exchange called vec2str that'll do this.
You'll need to set the encloseFlag parameter to 0 to remove the square brackets. Example:
a = [1 2 3];
b = vec2str(a,[],[],0);
Inside b you'll have:
b =
'1,2,3'

I found one solution myself:
after getting the string (not array), split it:
b = int2str(); %b='1 2 3'
c = strsplit(b); %c='1' '2' '3'
Then I can get the result c=strjoin(c, ',') as I wanted.

You can use:
c = regexprep(num2str(a), '\s*', ',');

Related

MATLAB cellfun() to map contains() to cell array

a={'hello','world','friends'};
I want to see if for every word in the cell array contains the letter 'o', how to use cellfun() to achieve the following in a compact expression?
b = [ contains(a(1),'o') contains(a(2),'o') contains(a(3),'o')]
You don't need cellfun, if you read the documentation, contains works natively on cell arrays of characters:
a = {'hello', 'world', 'friends'};
b = contains(a, 'o');
Which returns:
b =
1×3 logical array
1 1 0

Get indices of string occurrences in cell-array

I have a cell array that contains a long list of strings. Most of the strings are in duplicates. I need the indices of instances of a string within the cell array.
I tried the following:
[bool,ind] = ismember(string,var);
Which consistently returns scalar ind while there are clearly more than one index for which the contents in the cell array matches string.
How can I have a list of indices that points to the locations in the cell array that contains string?
As an alternative to Divakar's comment, you could use strcmp. This works even if some cell doesn't contain a string:
>> strcmp('aaa', {'aaa', 'bb', 'aaa', 'c', 25, [1 2 3]})
ans =
1 0 1 0 0 0
Alternatively, you can ID each string and thus have representative numeric arrays corresponding to the input cell array and string. For IDing, you can use unique and then use find as you would with numeric arrays. Here's how you can achieve that -
var_ext = [var string]
[~,~,idx] = unique(var_ext)
out = find(idx(1:end-1)==idx(end))
Breakdown of the code:
var_ext = [var string]: Concatenate everything (string and var) into a single cell array, with the string ending up at the end (last element) of it.
[~,~,idx] = unique(var_ext): ID everything in that concatenated cell array.
find(idx(1:end-1)==idx(end)): idx(1:end-1) represents the numeric IDs for the cell array elements and idx(end) would be the ID for the string. Compare these IDs and use find to pick up the matching indices to give us the final output.
Sample run -
Inputs:
var = {'er','meh','nop','meh','ya','meh'}
string = 'meh'
Output:
out =
2
4
6
regexp would solve this problem better and the easy way.
string = ['my' 'bat' 'my' 'ball' 'my' 'score']
expression = ['my']
regexp(string,expresssion)
ans = 1 6 12

Convert array to string in MATLAB?

I have a vector a = [1 2 3 4 5] how would I make it so b = '12345'?
I have tried b = num2str(a) but it outputs 1 2 3 4 5.
You can specify the format in num2str, much as you would in C's function sprintf:
b = num2str(a,'%i');
Or use sprintf:
b = sprintf('%i',a);
If a contains only single-digit numbers, you can also convert to char directly:
b = char(a+'0');
You need to convert yor vector to a single number first (assuming all elements are in range 0..9):
a = 1:5;
num = ( 10.^((numel(a)-1):-1:0) ) * a'; %'
b = num2str( num )
You can try this code here.

textscan() reading result is a nested cell array?

I have a data file containing 100 lines with the following format
0,device1,3
1,device2,33
2,device3,3
3,device4,34
...
99,device100,36
Now I wish to read them into a 100x3 cell array in MATLAB. I did the following:
allData = textscan(fID,'%s %s %f', 'delimiter', ',');
Then, I noticed that allData is a 1x3 cell array with each item being another 100x1 cell array. (The first two columns are string-type cell arrays, whereas the third column is double-type cell array)
In other words, the reading result is a nested array, which I don't want.
How may I achieve 100x3 cell array directly while reading?
With that textscan, the variable allData looks something like (just 4 rows) this:
allData =
{4x1 cell} {4x1 cell} [4x1 double]
You can only merge into a single cell array directly with textscan via the 'CollectOutput' option when all data has the same type.
One possible workaround, which unfortunately converts all numeric data to double (not a problem in your case),
C = cell(numel(allData{1}),numel(allData));
areCells = cellfun(#iscell,allData);
C(:,areCells) = [allData{areCells}];
C(:,~areCells) = num2cell([allData{~areCells}])
C =
'0' 'device1' [ 3]
'1' 'device2' [33]
'2' 'device3' [ 3]
'3' 'device4' [34]
Again, the drawback of this is that the last statement will convert all non-cell types (e.g. uint8, char, etc.) into doubles. To avoid this possible conversion:
% after copying cell array data (areCells) as above, but before ~areCells data
Cn = arrayfun(#(ii)num2cell(allData{ii}),find(~areCells),'uni',0);
C(:,~areCells) = [Cn{:}];
Code -
sz = 100; % Line count
out=cell(sz,size(allData,2));
for k = 1:size(allData,2)
t1 = allData(k);
t2 = [t1{:}];
if isnumeric(t2) % Takes care of floats
out(:,k) = num2cell(t2);
else
out(:,k) = t2
end
end
Thus, the first four lines would be shown as -
out =
'0' 'device1' [ 3]
'1' 'device2' [33]
'2' 'device3' [ 3]
'3' 'device4' [34]

Difference of two cell arrays of strings in Matlab

I have a cell array like
a={'potential'; 'impact'; 'of'; 'stranded'; 'assets'; 'and'; 'other'; 'necessary'; 'regulatory'; 'approvals'; 'assets'}
and want to subtract from it an array like b={'a'; 'and'; 'of'; 'my'; '#'}.
Using setdiff(a,b) sorts my array after the difference is computed. What I want is to eliminate from a all the elements present in b without sorting a. Also the repetitions should be preserved, for eg. 'assets' in array a should appear at two locations in final array.
The following code I am using does the job:
for i = 1:length(b)
tf = ~strcmp(b(i),a)
a = a(tf,:)
end
But the problem is that array b contains more than 200 string elements which slows down my code considerably. Is there a better way to do this?
tf = ismember(a,b);
a = a(~tf)
EDU>> a
a =
'potential'
'impact'
'of'
'stranded'
'assets'
'and'
'other'
'necessary'
'regulatory'
'approvals'
'assets'
EDU>> b
b =
'a'
'and'
'of'
'my'
'#'
[I,J]=setdiff(a,b);
Now do
EDU>> a(sort(J),:)
ans =
'potential'
'impact'
'stranded'
'other'
'necessary'
'regulatory'
'approvals'
'assets'

Resources