How to print commas between two numbers in MATLAB? - matlab

Suppose I have:
a=[2;3];
b=[4;5];
c=[1;1];
How can I print the result as
s=
2,4,1
3,5,1
So far I have tried with:
a=[2;3];
b=[4;5];
c=[1;1];
s=sprintf('%d,%d,%d\n',a,b,c)
but I am not getting the desirable result.

At first you can merge them into a matrix and then do the following:
res = [a b c];
str = sprintf('%d,%d,%d\n',res.');

you can do
s = sprintf('%d,%d,%d\n', [a,b,c]')
which gives
s = 2,4,1
3,5,1

Related

How to print a chain of arrows with fprintf

I am looking for a way to set up the fprintf function so that it returns the string 1->2->...->n for any input n. However, I cannot find a way to do so without having an extra arrow attached at the beginning (->1->2->...->n) or the end of the string (1->2->...->n->). Is there a way around this?
You could use strjoin for this...
n = 4;
str = strjoin( arrayfun(#num2str, 1:n, 'uni', 0), '->' );
% str = '1->2->3->4'
Or if you're set on using fprintf (or sprintf), you could manually add the first element (for ease, assume n >= 1)
str = ['1', sprintf('->%.0f', 2:n )];
If you just want to print these to the Command Window, simply use disp on either option instead of (or after) assigning to str. If you're writing to a file with fprintf then simply use fprintf( fid, [str '\n'] ) to print the line to file.
For this type of task, the solution is to print either the first or the last element separately:
n = 8;
fprintf('%d', 1);
fprintf('->%d', 2:n);
fprintf('\n');
Here's another approach to build the desired string:
n = 10;
str = regexprep(num2str(1:n), '\s+', '->');
This gives
str =
'1->2->3->4->5->6->7->8->9->10'

Building file name from cell arrays with variables

I want to build a file name from cell arrays. I wanted to use sprintf function, but it doesn't work for cell inputs (it works well for example for string arrays).
Example:
foldername = sprintf('''\\STH\\Sth %s\\ABC %s;'',firstvariable,secondvariable);
Output:
Error using sprintf
Function is not defined for 'cell' inputs.
Where variables are cell arrays, like that:
firstvariable = {'A','B','C'};
secondvariable = {'D','E','F'};
I also tried to use fullfile function, but it doesn't take any variables. The output from this one look like that: \STH\Sth %s\ABC %s. What more, I also tried to use strcat function, but the result is pretty the same.
Do you have any other ideas how it can be done?
Thanks!
You can pass the two cell arrays to cellfun to loop over them at the same time:
firstvariable = {'A','B','C'};
secondvariable = {'D','E','F'};
foldername = cellfun(#(x,y) ['\STH\Sth ' x '\ABC ' y], firstvariable, secondvariable, 'UniformOutput', false);
This gives
foldername =
1×3 cell array
'\STH\Sth A\ABC D' '\STH\Sth B\ABC E' '\STH\Sth C\ABC F'
Is this what you want?
for ii = 1:length(firstvariable)
foldername{ii} = sprintf('''\\STH\\Sth %s\\ABC %s;''',firstvariable{ii},secondvariable{ii});
end
The output is:
foldername = {''\STH\Sth A\ABC D;''} {''\STH\Sth B\ABC E;''} {''\STH\Sth C\ABC F;''}
To use any of the folder names just do the following:
foldername{index} % Gives back a string
foldername(index) % Gives back a cell

How to sum a cell in Matlab

I am a new learner in Matlab and now want to add up a cell of column elements in Matlab, somehow "sum" function didn't work and it shows "Undefined function 'sum' for input arguments of type 'cell'", is there anyone know how to do it? MANY THANKS!:)
my data is like this:
'218148'
'106856'
'255673'
'156279'
'175589'
'310762'
'87128'
'123339'
'149070'
'104556'
'206346'
'216278'
'235786'
Your cells are strings so you first have to convert them to numeric:
C = { '218148' '106856' '255673' '156279' '175589' '310762' '87128'...
'123339' '149070' '104556' '206346' '216278' '235786' '236087'...
'99137' '123335' '130021' '101655' '98159' '102047' '824411' '63290'};
Csum = sum(str2double(C));
the result:
Csum =
4123952
You can call the content of your cells like this:
your_cell{:}
If all values are numeric, you can then group this result as a vector:
[your_cell{:}]
you can then easily sum this result:
sum([your_cell{:}])
A small example:
c{1} = 1;
c{2} = 3;
c{3} = 6;
sum([c{:}])
result:
ans =
10

Logical elements substitution [3D Matrix]

Let's say that I have 2 3D-Matrix:
A = rand(10,4,100);
B = rand(10,4,100);
L = gt(A,B);
Now I want substitute all elements of B with elements of A only where L==1but this doesn't work:
B(L==1,:,:) = A(L==1,:,:);
Any suggestion?
Our even shorter whithout find
B(L) = A(L);
Sounds like a job for the find() function.
p = find(L);
B(p) = A(p);
EDIT: Just realized you don't need the find() function. Just use logical indexing like this:
B(L==1) = A(L==1);

Concatenate strings of digits in matlab

Suppose I have a series of strings such as:
a = '101010101010'
b = '010101'
c = '000101010'
is there a way in Matlab to concatenate them and produce the binary number 101010101010010101000101010?
Use the concatenation operator [ ], with horizontal concatenation , (vertical concatenation ; will fail here unless you reshape() into column vectors):
[a,b,c]
However, I suggest storing your variables in a cell array:
s = {'101010101010','010101', '000101010'};
[s{:}]
or
cat(2,s{:})
To concatenate strings, you could say:
out = [a b c];
Alternatively:
out = strcat(a,b,c);
Yet another way:
out = sprintf('%s', a,b,c);
I think that this should work:
res = [a,b,c]
or alternatively call
res = strcat(a,b,c)
or, yet
res = cat(2,a,b,c)