Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Anyone have code to take a matrix/cell-array and generate the M code that would re-create it?

Subject: Anyone have code to take a matrix/cell-array and generate the M code that would re-create it?

From: Eric Sampson

Date: 20 May, 2013 21:33:07

Message: 1 of 5

Hi all, do you know if anyone's created a function that would take a matrix, or 2D cell array of numbers/strings, and output M code text that would regenerate the input? I tried looking through the File Exchange, but couldn't find anything...

Thanks!

Subject: Anyone have code to take a matrix/cell-array and generate the

From: Nasser M. Abbasi

Date: 20 May, 2013 21:46:58

Message: 2 of 5

On 5/20/2013 4:33 PM, Eric Sampson wrote:
> Hi all, do you know if anyone's created a function that would take a matrix,
>or 2D cell array of numbers/strings, and output M code text that would
>regenerate the input? I tried looking through the File Exchange, but couldn't find anything...
>
> Thanks!
>

What do you mean by m code to "regenerate the input" ?

I have no idea what that can possibly mean.

So, if I give you the input '12342234937423' You
want the code to generate this? Ok, here is the code

          x='12342234937423'

Please explain more what is it you are trying to do.

--Nasser


  

Subject: Anyone have code to take a matrix/cell-array and generate the M code that would re-create it?

From: Steven_Lord

Date: 20 May, 2013 21:51:33

Message: 3 of 5



"Eric Sampson" <ericDOTsampson@gmail.com> wrote in message
news:kne4qj$nei$1@newscl01ah.mathworks.com...
> Hi all, do you know if anyone's created a function that would take a
> matrix, or 2D cell array of numbers/strings, and output M code text that
> would regenerate the input? I tried looking through the File Exchange, but
> couldn't find anything...

Matrix? MAT2STR.

http://www.mathworks.com/help/matlab/ref/mat2str.html

Cell array? That's more general and so would be a little more difficult.
More details might help.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: Anyone have code to take a matrix/cell-array and generate the

From: Eric Sampson

Date: 20 May, 2013 22:07:09

Message: 4 of 5

"Nasser M. Abbasi" wrote in message <kne5ke$kqt$1@speranza.aioe.org>...
> On 5/20/2013 4:33 PM, Eric Sampson wrote:
> > Hi all, do you know if anyone's created a function that would take a matrix,
> >or 2D cell array of numbers/strings, and output M code text that would
> >regenerate the input? I tried looking through the File Exchange, but couldn't find anything...
> >
> > Thanks!
> >
>
> What do you mean by m code to "regenerate the input" ?
>
> I have no idea what that can possibly mean.
>
> So, if I give you the input '12342234937423' You
> want the code to generate this? Ok, here is the code
>
> x='12342234937423'
>
> Please explain more what is it you are trying to do.
>
> --Nasser
>
>
>

Hi Nasser, I apologize for my poor description. For the matrix case, here's an example of the workflow I'm trying to accomplish:

var = rand(2);
var_as_code = mat2code(var);
fprintf(var_as_code)
[0.834 0.120
 0.432 0.427]

I was just looking around the doc browser and stumbled upon mat2str - that's pretty similar to what I was looking for! I was hoping that the output string would contain EOL characters instead of semicolons, but I can probably fix that with post-processing. It also doesn't handle cell arrays, which would be handy...
 

Subject: Anyone have code to take a matrix/cell-array and generate the M code that would re-create it?

From: Eric Sampson

Date: 20 May, 2013 22:17:10

Message: 5 of 5

"Steven_Lord" <slord@mathworks.com> wrote in message <kne5t5$qd1$1@newscl01ah.mathworks.com>...
>
>
> "Eric Sampson" <ericDOTsampson@gmail.com> wrote in message
> news:kne4qj$nei$1@newscl01ah.mathworks.com...
> > Hi all, do you know if anyone's created a function that would take a
> > matrix, or 2D cell array of numbers/strings, and output M code text that
> > would regenerate the input? I tried looking through the File Exchange, but
> > couldn't find anything...
>
> Matrix? MAT2STR.
>
> http://www.mathworks.com/help/matlab/ref/mat2str.html
>
> Cell array? That's more general and so would be a little more difficult.
> More details might help.
>
> --
> Steve Lord

Hi Steve! Thanks for the pointer to mat2str, I'd never run across that function before... At this point do you think there are any MATLAB functions that you've never heard of? lol :) For cell arrays in my case, they're 2D with a mix of string/numeric data; if you can picture what you get if you Import a spreadsheet into a cell array with some text columns and other numeric columns, that's exactly what I have. The only slight complication is that often the first row of the cell is a header, so all its entries are strings, even though some of the columns are numeric. Like this:

foo =
{'col1' 'col2' 'col3'
'run1' 10.3 'comment1'
'run2' -5.4 'comment2'
...
}

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us