how do I insert a string into a csv file in matlab. i used this code to write some data and create my csv file:
and here is the output of the code:
I'm trying to insert some text in the first 2 columns before the numerical data..
thanks in advance :)
There are several approaches are possible here.
Let's take a look at some of them:
If you need to add string to your csv file.
For example, I create some csv file like your:
q = [1 2 3 4 5 6 7];
csvwrite('csvlist4.csv',q,2,0);
All troubles is to add some string to csv - it's because we need to combine numeric and text data. There are no good functions for it in Matlab except low levels:
c = 'some big text';
fid = fopen('csvlist4.csv','r+');
fprintf(fid,c);
How it works: the data in csv is an array. I put data in 3rd row, so first and second is an empty but they have ','. When you use fprintf it will replace this , with your text. So if text is too long it will overwrite your data.
How to avoid this?
Easiest way - is to do it with help of xlswrite function. For your example:
txt = cell(size(Q))
txt{1} = 'this is your text'
X = [txt; num2cell(Q)]
xlswrite('new.xlsx',X)
Result:
Important moment here: number of cell for text must be the same as your data. But I filled with the text only first cell in my example.
And the one more way: read csv file, modify it's data and write to csv:
csvwrite('csvlist4.csv',a,2,0);
m = csvread('csvlist4.csv');
fid = fopen('csvlist4.csv','w');
c = 'your text'
fprintf(fid, c); fprintf(fid, '\n');
fclose(fid);
dlmwrite('csvlist4.csv', m(3:end,:), '-append');
Of course you can use cell array instead c and so on and so on.
Hope it helps!
Related
I have a .mat file which contains a struct (called wiki),
in which there is a field called full_path containing data as follows:
ans =
Columns 1 through 4
{'17/10000217_198…'} {'48/10000548_192…'} {'12/100012_1948-…'} {'65/10001965_193…'}
Columns 5 through 8
{'16/10002116_197…'} {'02/10002702_196…'} {'41/10003541_193…'} {'39/100039_1904-…'}
and so on
How can I create a .csv file with the data present in the curly braces?
This is quite a common problem, which requires very basic functions:
wiki = struct2array(load('wiki.mat', 'wiki'));
fid = fopen('q52688399.csv', 'w');
fprintf(fid,'%s\n', wiki.full_path{:});
fclose(fid);
The above will produce a ~2MB text tile containing a single column of strings.
I have a deco.csv file and I only want to extract B1 to K1 (20 columns of the first rows), i.e. Deco_0001 to Deco_0020.
I first make a pre-allocation:
names = string(20,1);
and what I want is when calling S(1), it gives Deco_0001; when calling S(20), it gives Deco_0020.
I have read through textscan but I do not know how to specify the range is first row and running from column 2 to column 21 of the csv file.
Also, I want save the names individually but what I have tried just save the first line in only one cell:
fid=fopen('deco.csv');
C=textscan(fid, '%s',1);
fclose(fid);
Thanks!
It's not very elegant, but this should work for you:
fid=fopen('deco.csv');
C=textscan(fid, '%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s',1,'Delimiter',',');
fclose(fid);
S = cell(20,1);
for ii = 1:20
S{ii} = C{ii+1};
end
I am reading in a .txt-file using importdata:
MC_file = importdata('D:\Simon\Dropbox\SGM\Gigerwald\Results\Multi_SGM_10000.txt');
Data = MC_file.data
Header = MC_file.colheaders
which gives me a struct-variable with the header and the data-body:
data = 10000x52 double
colheaders = 1x52 cell
Now i'd like to change the data, i.e. add some columns to the end of the file and then print the file again with the same header + 5 new columns.
The body can be printed easily using dlmwrite(), e.g. like this:
dlmwrite('SGM.txt',Data,'precision',8,'delimiter','\t','newline','pc','-append')
...but how do I print the header into the file, of which I have a array of cells? I've seen this, but since I end up with 57 columns hard-coding is not an option...
The solution is attainable using repmat. Assuming that your file is tab separated, you can use:
NUM_COLS = 57;
A=cellstr(num2str((1:NUM_COLS)'))';
fid=fopen('newfile.txt','w');
fprintf(fid,repmat('%s\t ',[1,NUM_COLS]),A{:});
fclose(fid);
The result can be previewed with:
sprintf(repmat('%s\t ',[1,NUM_COLS]),A{:})
I'm using the following code to write the vectors sortedthresh_strain and probofdetectionanddelamprop1 into a text file. However, the text file output is as follows:
0.0030672 1.6592e-080.0033489 5.1721e-080.0034143
where 0.0033489 5.1721e-08 should be on the next line of the text file. i.e. It should be:
0.0030672 1.6592e-08
0.0033489 5.1721e-08
I am unsure of how to do this.
Edit: Using the proposed answer:
0.0049331 0.0049685 0.0049894 0.0050094 0.005156 0.0051741 0.0052139 0.0053399 0.0054486 0.0056022 7.0711e-21 3.0123e-19
The 2nd column is required to contain:
7.0711e-21
3.0123e-19
And,
dlmwrite('THRESHUNCERTAINTYFINALPLOTLSIGMA5.dat'[sortedthresh_strain,probofdetectionanddelamprop1],'delimiter', '\t');
If you have R2013b or later, see this answer. If you have an earlier version but have the statistics toolbox you can use the dataset object to do this very easily just like tables in R2013b. Using dataset:
data1 = {'a','b','c'}'
data2 = [1, 2, 3]'
ds = dataset(data1, data2)
export(ds, 'file', 'data.txt')
If you don't want the variable names in the result text file you can use 'WriteVarNames', false in your call to export.
Good luck!
I think your data is in row vectors, but should be column vectors for it to work like you want.
Just add a transpose with '.
dlmwrite('THRESHUNCERTAINTYFINALPLOTLSIGMA5.dat',[sortedthresh_strain',probofdetectionanddelamprop1'],'delimiter', '\t');
The following code generates a similar dataset to what I am currently working with:
clear all
a = rand(131400,12);
DateTime=datestr(datenum('2011-01-01 00:01','yyyy-mm-dd HH:MM'):4/(60*24):...
datenum('2011-12-31 23:57','yyyy-mm-dd HH:MM'),...
'yyyy-mm-dd HH:MM');
DateTime=cellstr(DateTime);
header={'DateTime','temp1','temp2','temp4','temp7','temp10',...
'temp13','temp16','temp19','temp22','temp25','temp30','temp35'};
I'm trying to convert the outputs into one variable (called 'Data'), i.e. have header as the first row (1,:), 'DateTime' starting from row 2 (2:end,1) and running through each row, and finally having 'a' as the data (2:end,2:end) if that makes sense. So, 'DateTime' and 'header' are used as the heading for the rows and column respectively. Following this I need to save this into a tab delimited text file.
I hope I've been clear in expressing what I'm attempting.
An easy way, but might be not the fastest:
Data = [header; DateTime, num2cell(a)];
filename = 'test.txt';
dlmwrite(filename,1); %# no create text file, not Excel
xlswrite(filename,Data);
UPDATE:
It appears that xlswrite actually changes the format of DateTime values even if it writes to a text file. If the format is important here is the better and actually faster way:
filename = 'test.txt';
out = [DateTime, num2cell(a)];
out = out'; %# our cell array will be printed by columns, so we have to transpose
fid = fopen(filename,'wt');
%# printing header
fprintf(fid,'%s\t',header{1:end-1});
fprintf(fid,'%s\n',header{end});
%# printing the data
fprintf(fid,['%s\t', repmat('%f\t',1,size(a,2)-1) '%f\n'], out{:});
fclose(fid);