Write line by line a separated .csv file with a string \t an integer \t [a n*2 matrix written in line with commas and semicolon]

23 visualizzazioni (ultimi 30 giorni)
Hi,
because of previous functions I wrote and that I do not want to modify, I need to write line by line a separated .csv file with the following format:
a string \t an integer \t [a n*2 matrix written in line with commas and semicolon]
"\t" denoting a tab delimiter here.
Basically, the first column of the .csv file is a string corresponding to the name of a file, the next one is an integer, and the third column of the .csv determines an non-fixed number of time ranges: [t11, t12; t21, t22; ... ; tn1, tn2];
I guess one could say that is like saving a structure into a structure. I tried a function struct2csv available on Mathworks but it does not do quite what I want.
I also tried with more success
fileID=fopen('myfile.csv','w')
fprintf(fileID,'%s\t%d\t%s\n',structtosave.fname,structtosave.fps,structtosave.sequences)
fclose(fileID)
with structtosave a matrix with the proper fields.
But I still have an issue with the matrix corresponding to structtosave(i).sequences not being a line (I need it to be written on a single line, with commas , and semicolons ;
Does anyone knows how to do this?
Can you tell me if the use of a structure is the best way to proceed here?
Thanks a lot for your help!

Risposta accettata

LeChat
LeChat il 16 Ago 2019
Ok, thanks to Walter Roberson advice to use mat2str Matlab function, I manage to obtain what I was looking for.
One of the first thing it that if the fields are separated with tabs \t, then the file things are saved into has to be .tsv and not a .csv like I was trying to do.
Second, mat2str is pretty neat. Here is the minimal code I used:
datafilename='Data.tsv';% output file that we want
meanmatfiles = dir('mean*NEW.mat');%I am looking for particular files in this directory
fileID=fopen(datafilename,'w');%create the output file and opens it
for file = meanmatfiles'
ifile=ifile+1;
fname=meanmatfiles(ifile).name;disp(fname);%get the name of the current file
load(fname)
%% do something that computes the array 'times'
fileID=fopen(datafilename,'a');%append on each new line of the output file
videoname=[fname(strfind(fname,'2019'):strfind(fname,'4x_')+5),'.nd2'];%cropping the proper range of characters in the fname
fprintf(fileID,'%s\t%d\t%s\n',videoname,fps,mat2str(round(times)));%write the next line with the proper format
end
fclose(fileID)%close the output file
I left a couple lines that take advantage of a couple nice Matlab function which I believe would be usefull to some reader.
Hope this helps, and again, thank you Walter Robinson for your quick reply!
  3 Commenti
LeChat
LeChat il 19 Ago 2019
Thank you Stephen for these great tips. I'll keep them in mind.
PS: why is it important do load into an output variable if may .mat has one already named?
Stephen23
Stephen23 il 20 Ago 2019
"why is it important do load into an output variable...?"
More robust code. Less buggy code. Easier to debug code.
  • Does not overwrite variables without warning.
  • Makes it clear (to both humans and the JIT parser) where variables come from.
  • Prevents incorrect functions/classes/objects being referred to by the parser.
  • Allows data-files with missing/incorrect data to be easily detected and handled.

Accedi per commentare.

Più risposte (1)

Walter Roberson
Walter Roberson il 16 Ago 2019
You will find it tricky to output the variable-length data if you use fprintf() in vectorized form. It is possible, but the code would be far from clear, and using it would be mostly to prove the point that it could be done.
It is a lot easier to loop writing one line at a time, and taking a step to format the last column as a string, after which you can fprintf() with '%s\5%d\t%s' format
Probably the easiest way to create the variable length output with commas and semi-colons is to use mat2str() on the array, possibly triming off the literal '[' and ']' that mat2str adds.
  1 Commento
LeChat
LeChat il 16 Ago 2019
Thank you Walter for your advice. How shall things be done on this forum, which answer should I accept? Yours because you gave me the idea of using mat2str or mine because it provides the code and gives the whole solution?

Accedi per commentare.

Prodotti

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by