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:
Cell array to double

Subject: Cell array to double

From: Wei

Date: 10 Aug, 2011 22:27:27

Message: 1 of 2

Hi,

I have to resave a bunch of files in csv into a folder.
Reason behind this is I'm downloading data from a program that automatically saves as csv format. However, when opening with Notepad, I notice that every element has quotation marks around them. (ex: A=["3" "452" "2005/5/8" "c545"; "3" "523" "2005/08/12" "c324"] )

This file is impossible to deal with in Matlab because of these quotation marks because they have trouble storing things in double.
I noticed that without quotation marks, my code works. And when I resave the original csv file (also into another csv file), I lose the quotation marks (have no idea why, but it works for me.)

Since I have a lot of files to deal with, I was trying to find a way to save all these files throught matlab with a for loop.

I tried copyfile but it doesn't work...

Can I get some help?

Thanks in advance!!

Subject: Cell array to double

From: Sven

Date: 11 Aug, 2011 00:07:23

Message: 2 of 2

"Wei" wrote in message <j1v0kf$qhj$1@newscl01ah.mathworks.com>...
> Hi,
>
> I have to resave a bunch of files in csv into a folder.
> Reason behind this is I'm downloading data from a program that automatically saves as csv format. However, when opening with Notepad, I notice that every element has quotation marks around them. (ex: A=["3" "452" "2005/5/8" "c545"; "3" "523" "2005/08/12" "c324"] )
>
> This file is impossible to deal with in Matlab because of these quotation marks because they have trouble storing things in double.
> I noticed that without quotation marks, my code works. And when I resave the original csv file (also into another csv file), I lose the quotation marks (have no idea why, but it works for me.)
>
> Since I have a lot of files to deal with, I was trying to find a way to save all these files throught matlab with a for loop.
>
> I tried copyfile but it doesn't work...
>
> Can I get some help?
>
> Thanks in advance!!

One thing you could do is just replace all double quotation marks with singles:
>> myStr = 'A=["3" "452" "2005/5/8" "c545"; "3" "523" "2005/08/12" "c324"] ';
>> strrep(myStr,'"','''')
ans =
A=['3' '452' '2005/5/8' 'c545'; '3' '523' '2005/08/12' 'c324']

Keep in mind that your actual example won't run anyway... placing numbers in square brackets [] tries to make a matrix, but you've got mixed input of strings and numbers so that won't work. Instead you need curly braces {} to make a cell of inputs.

I "think" that you're trying to make your text files executable as a MATLAB script. If this is the case, what I would do is:
1. Loop through your files, replace " with ', and [] with {}
2. Execute your script
3. Choose the columns you want to replace strings with numbers.

So in your example you would have:
ORIGINAL FILE:
A=["3" "452" "2005/5/8" "c545"; "3" "523" "2005/08/12" "c324"]

FILE AFTER STEP 1.
A={'3' '452' '2005/5/8' 'c545'; '3' '523' '2005/08/12' 'c324'}

VARIABLE "A" AFTER RUNNING STEP 2.
A =
    '3' '452' '2005/5/8' 'c545'
    '3' '523' '2005/08/12' 'c324'

VARIABLE "A" AFTER STEP 3.
>> nums1to2 = cellfun(@str2double, A(:,1:2));
>> A(:,1:2) = num2cell(nums1to2)

A =
    [3] [452] '2005/5/8' 'c545'
    [3] [523] '2005/08/12' 'c324'

Thanks,
Sven.

Tags for 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