57517

Convert string to num with cellfun

Question:

I have the next problem:

A=[2014;20;2012;32155];

Then I want to convert A in a cell array with string characters, with the character A prepended to each of the strings. In other words, I wish to do this if I were to create a manual cell array:

A={'A2014';'A20';'A2012';'A32155'};

Can anybody make this without using a loop? Thanks!

Answer1:

With your edit, you can do this to first convert your numeric array into a cell array of strings like you said:

A = num2cell(A); A = cellfun(@num2str, A, 'UniformOutput', false);

Once you do this, if my understanding is correct, you want to insert an A character before each string. As such, you can do this:

A = cellfun(@(x) ['A' x], A, 'UniformOutput', false);

You would thus get:

A = 'A2014' 'A20' 'A2012' 'A32144'

Answer2:

Another way would be

formatSpec = 'A%d'; B = strtrim(cellstr(num2str(A(:), formatSpec))); B = reshape(B, size(A)); <ul><li><a href="http://www.mathworks.com/help/matlab/ref/strtrim.html" rel="nofollow">strtrim reference</a></li> <li><a href="http://www.mathworks.com/help/matlab/ref/cellstr.html" rel="nofollow">cellstr reference</a></li> <li><a href="http://www.mathworks.com/help/matlab/ref/num2str.html" rel="nofollow">num2str reference</a></li> </ul>

Note that you can modify the format of the resulting strings by changing the 'A%d' format specifier to something else. Read the documentation of num2str for more details.

Answer3:

Like @rayryeng said this works :

A = num2cell(A); A = cellfun(@num2str, A, 'UniformOutput', false)

Recommend

  • Im looking for a way to add up elements of two arrays
  • How to sum parts of a matrix of different sizes, without using for loops?
  • how to delete empty elements in the cell in a way i want
  • Parallelize or vectorize all-against-all operation on a large number of matrices?
  • MATLAB - Plot means on boxplot for different categories of data
  • Storing data in nested functions in GUIDE, Matlab
  • Legends with multiple rows in Matlab
  • pass FORTRAN READ arguments into a string
  • MATLAB uicontrol callback return matrix
  • AFNetworking 3 AFMultipartFormData getting bytes data in response
  • Find mean of an array with both numbers and strings with Matlab
  • Fibonacci numbers using matlab [duplicate]
  • How to set wallpaper (viewpager)
  • Sum of Data(end) in a cell array of timeseries
  • Raphael - event when mouse near element
  • SAS: Define type when importing .xlsx with PROC IMPORT
  • Can you use the lapply() function to alter the value of input?
  • Updating numerical types using NoRM and other tools changes numerical type metadata on MongoDb
  • SQLite HAVING comparison error
  • Assign different values to cell arrays in MATLAB at once
  • big integers when reading file with readr in r
  • how to calculate month difference in R
  • How to produce a Code 39 that can be reliably read after faxing
  • How to check a string does not start with a number in Batch?
  • How to write .bin file in R and open it in Fortran
  • Remove past Matplotlib plots in the same cell in Jupyter Notebook involving interactive widgets
  • How to negative match regex in JavaScript string replace? [duplicate]
  • Plot a table with R
  • How to lookup value with multiple criteria in excel 2007 and newer
  • addressing in assembler
  • Neo4j: Filter nodes based on aggregate function
  • What is corresponding c++ data type to SQL numeric(18,0) data type?
  • Mysterious problem with floating point in LISP - time axis generation
  • How to know when stdin is empty if it contains EOF?
  • Fetching methods from BroadcastReceiver to update UI
  • Symfony2: How to get request parameter
  • Timeout for blocking function call, i.e., how to stop waiting for user input after X seconds?
  • GridView Sorting works once only
  • WPF Applying a trigger on binding failure
  • Reading document lines to the user (python)