How can I define string scalars for a name(for example, one for my name and one for today's date) in command windows?
2 views (last 30 days)
Show older comments
Sorry I'm new to matlab.
Is this what I'm supposed to do?
a=['Tim'];
b=['09/13'];
When I tried creating a column vector consisting of a and b, it said the matrices size don't match or something like that. Using the scalars defined above, how can you build and define a column vector of the info?
0 Comments
Accepted Answer
Star Strider
on 14 Sep 2014
Use a cell array:
ab = {'Tim' datestr(now,'mm/dd')}
produces:
ab =
'Tim' '09/13'
The command:
ab{:}
produces:
ans =
Tim
ans =
09/13
and:
celldisp(ab)
produces:
ab{1} =
Tim
ab{2} =
09/13
and:
sprintf('%s\n\n', ab{:})
produces:
ans =
Tim
09/13
There may be other possibilities as well, but I can’t think of them just now.
6 Comments
Star Strider
on 14 Sep 2014
Very much so!
-------------------------------------------------------------------------------------------------------------------------------------------------
Note Beginning with MATLAB® Version 7.0 software, you can access the contents of cell arrays and structure fields without using the deal function. See Example 3, below. -------------------------------------------------------------------------------------------------------------------------------------------------
The ‘guts’ of Example 3 (without the explanation) being:
[a,b,c,d] = C{:}
[name1,name2] = A(:).name
This comes in handy when you pass an argument list to a function without passing the individual arguments as a list in the function itself:
qc = {3 5 7};
qf = @(a,b,c) a.*b.^c;
qfc = qf(qc{:})
elutes:
qfc =
234.3750e+003
Makes it possible to so a lot of wonderful magic with a minimum of typing.
We can thank Tim for getting us off on this cell-array tangent! I actually learned some stuff while writing it.
More Answers (1)
Image Analyst
on 14 Sep 2014
That will work, though they're called "string literals" http://en.wikipedia.org/wiki/String_literal not "string scalars" and you don't need the square brackets. If you want a column vector, you can either create a cell array with a in the first cell and b in the second cell:
a= 'Tim';
b = '09/13';
ca = cell(2, 1); % Initialize a 2 row column vector.
ca{1} = a;
ca{2} = b;
celldisp(ca);
or you can convert a and b to column arrays and concatenate
c = [a(:);b(:)]
2 Comments
Image Analyst
on 14 Sep 2014
See the FAQ for a good intuitive description of cell arrays:
See Also
Categories
Find more on Data Type Conversion in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!