|
"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.
|