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:
CSV with text and number separated by semicolumn

Subject: CSV with text and number separated by semicolumn

From: Morbyd

Date: 18 Apr, 2014 12:38:09

Message: 1 of 6

hello, i'm pretty new in matlab language.
i have to creat a csv like that:
text;0;5;0;4;089;...
text;0;5;0;4;089;...
text;0;5;0;4;089;...
.
.
.

for this i have a big matrix of Double size (X,Y)
and a matrix of text size (X,1)

how to write from this two matrix my csv file, i tried many solution but i lost myself in the stack of matlab functions

i know there is many result on google, but please, if i post, that's beacause i've can't find a solution

thank you

(sorry for that english, i'm french)

Subject: CSV with text and number separated by semicolumn

From: dpb

Date: 18 Apr, 2014 13:24:01

Message: 2 of 6

On 4/18/2014 7:38 AM, Morbyd wrote:
> hello, i'm pretty new in matlab language.
> i have to creat a csv like that:
> text;0;5;0;4;089;...
> text;0;5;0;4;089;...
> text;0;5;0;4;089;...
> .
...

doc dlmwrite

--

Subject: CSV with text and number separated by semicolumn

From: dpb

Date: 18 Apr, 2014 13:33:59

Message: 3 of 6

On 4/18/2014 8:24 AM, dpb wrote:
> On 4/18/2014 7:38 AM, Morbyd wrote:
>> hello, i'm pretty new in matlab language.
>> i have to creat a csv like that:
>> text;0;5;0;4;089;...
>> text;0;5;0;4;089;...
>> text;0;5;0;4;089;...
>> .
> ...
>
> doc dlmwrite
>

BTW, for the problem of getting lost in all the Matlab functions...often
starting with

help

and following the tree will help a great deal. In this case

 >> help
matlab\demos - Examples.
matlab\graph2d - Two dimensional graphs.
matlab\graph3d - Three dimensional graphs.
...
matlab\datafun - Data analysis and Fourier transforms.
...
matlab\funfun - Function functions and ODE solvers.
matlab\general - General purpose commands.
...
matlab\iofun - File input and output.
matlab\lang - Programming language constructs.
...

 >> help iofun
   File input and output.

   File import/export functions.
     matfile - Load or save parts of variables in MAT-files.
     dlmread - Read ASCII delimited file.
     dlmwrite - Write ASCII delimited file.
     csvread - Read a comma separated value file.
     csvwrite - Write a comma separated value file.
     importdata - Load data from a file into MATLAB.
     daqread - Read Data Acquisition Toolbox (.daq) data file.
     matfinfo - Text description of MAT-file contents.
     fileread - Return contents of file as string vector.

   Spreadsheet support.
....

can get you to the right area(s) quickly.

--

Subject: CSV with text and number separated by semicolumn

From: Morbyd

Date: 18 Apr, 2014 13:41:08

Message: 4 of 6

dpb <none@non.net> wrote in message <lir91e$a1c$1@speranza.aioe.org>...
> On 4/18/2014 7:38 AM, Morbyd wrote:
> > hello, i'm pretty new in matlab language.
> > i have to creat a csv like that:
> > text;0;5;0;4;089;...
> > text;0;5;0;4;089;...
> > text;0;5;0;4;089;...
> > .
> ...
>
> doc dlmwrite
>
> --

TY, i tried used dlmwrite, but my text field is divieded for exemple i have (t;e;x;t;)
also i use two lines:
for it = 1:1:numel(DataToRegister1(:,1))
        %fprintf(fid, '%s;', char(enteteNPF(it)));
        dlmwrite(nomFichierSortie, char(enteteNPF(it)),'-append', 'delimiter', ';');
        dlmwrite(nomFichierSortie, DataToRegister1(it,:),'-append', 'delimiter', ';');
    end
I have (t;e;x;t;) on a line
and the values on the line after

Subject: CSV with text and number separated by semicolumn

From: dpb

Date: 18 Apr, 2014 15:08:04

Message: 5 of 6

On 4/18/2014 8:24 AM, dpb wrote:
> On 4/18/2014 7:38 AM, Morbyd wrote:
>> hello, i'm pretty new in matlab language.
>> i have to creat a csv like that:
>> text;0;5;0;4;089;...
>> text;0;5;0;4;089;...
>> text;0;5;0;4;089;...
>> .
> ...
>
> doc dlmwrite
>

Oooh...my bad, I overlooked the mixed metaphors (text and numeric).
Unfortunately, dlmwrite and friends don't have that flexibility, you've
got to use fprintf

[nr,nc]= size(x); % size of the data array
fmt=['%s,' repmat('%f,',1,nc) '\n'];
fid = fopen(filename, 'w');
for ir=1:nr
   fprintf(fid,fmt, txt(ir,:),x(ir,:);
end
fid=fclose(fid);

Above assumes a character array, not cellstring array. If it's a
cellstring array, then replace dereference them as

   fprintf(fid,fmt, txt{ir},x(ir,:);

using the curlies instead.

--

Subject: CSV with text and number separated by semicolumn

From: Morbyd

Date: 22 Apr, 2014 07:06:12

Message: 6 of 6

dpb <none@non.net> wrote in message <lirf4h$pjs$1@speranza.aioe.org>...
> On 4/18/2014 8:24 AM, dpb wrote:
> Oooh...my bad, I overlooked the mixed metaphors (text and numeric).
> Unfortunately, dlmwrite and friends don't have that flexibility, you've
> got to use fprintf
>
> [nr,nc]= size(x); % size of the data array
> fmt=['%s,' repmat('%f,',1,nc) '\n'];
> fid = fopen(filename, 'w');
> for ir=1:nr
> fprintf(fid,fmt, txt(ir,:),x(ir,:);
> end
> fid=fclose(fid);
>
> Above assumes a character array, not cellstring array. If it's a
> cellstring array, then replace dereference them as
>
> fprintf(fid,fmt, txt{ir},x(ir,:);
>
> using the curlies instead.

I have to tell you how wonderful you are, like a god, i tried to used fprintf, but i didn't know the function repmat :/
Really helpfull, thank you very much
(sorry for the delay of my answer)

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