save a cell to multiple csv files in a for loop
Mostrar comentarios más antiguos
I am trying to save the contents of 2 different cells into multiple CSV files using a for loop. I want sigma and epsilon to be combined into one file and then multiple files made for each column in the cells. My code currently looks like this and gives me this error:
Error using writematrix
Name not specified. Filename must include name of the file.
Error in filecallforlooptest (line 25)
writematrix([sigma{1,k}(1:end) epsilon{1,k}(1:end)],thisfile);
files = dir('ss\')
for k =1:numel(files)
thisfile = files(k).name;
writematrix([sigma{1,k}(1:end) epsilon{1,k}(1:end)],thisfile);
end
I do not know if I am taking the right approach on this code in general, or if there is a different method that will work for me better. Either way I would appreciate help in fixing this error or writing a different code that achieves my goal.
I also tried this initially which works to put each column of the cell onto a seperate sheet in one .xlsx file
for k = 1:n
filename = 'stressstrain.xlsx';
writematrix([sigma{1,k}(1:end) epsilon{1,k}(1:end)],filename,'sheet',k);
end
This might also work for my purposes if there is a method to seperate sheets into csv files.
Respuesta aceptada
Más respuestas (1)
I'd ask if it's really productive to create multiple files rather than keeping the data together in an array and process the array by desired columns instead of having to also open separate files for each. There may be a real need, but often it's counterproductive to split up data that way.
Philosophy aside, in
files = dir('ss\')
for k =1:numel(files)
thisfile = files(k).name;
writematrix([sigma{1,k} epsilon{1,k}(1:end)],thisfile);
end
since your dir() struct is returning a search on a directory and not a specific (set of) file(s), undoubtedly the first two entries are going to be the nuisance "dot and double-dot" directory entries (that I've yet to understand why are returned rather than just silently ignored, but Bill didn't ask my opinion).
You need something like
d=dir('ss\'); % it's dir() struct, not a file
d=d(~[d.isdir]); % remove the nuisance not file entries
for k =1:numel(d)
thisfile = d(k).name;
writematrix([sigma{1,k} epsilon{1,k}],thisfile);
end
Now, the above also looks a little suspicious in that it's going to overwrite already existing files in that folder and there's no guarantee that they are .csv files nor does the writematrix command as written ensure it will write them in that format.
But, it could work and be ok IFF the files do exist and are the right type and there are at least as many columns in the cell array as files. Nor does it check that there are enough files for all k columns in the other direction to ensure everything is output. But, we can't know anything about that, but again, wouldn't it be simpler to turn the cell array into a 2D array or a 2 column 3D array where each pair of columns is a plane instead? Or, use a table with an identifer or each pair.
1 comentario
Stephen Lesko
el 28 de Jul. de 2023
Categorías
Más información sobre Standard File Formats en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!