Effacer les filtres
Effacer les filtres

save a cell to multiple csv files in a for loop

5 vues (au cours des 30 derniers jours)
Stephen Lesko
Stephen Lesko le 26 Juil 2023
Commenté : Stephen Lesko le 28 Juil 2023
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.

Réponse acceptée

Manan Jain
Manan Jain le 26 Juil 2023
Hi!
It seems like you want to save the contents of two different cells (`sigma` and `epsilon`) into multiple CSV files, with each file containing the data from corresponding columns in these cells. Additionally, you mentioned that you were able to save the data to separate sheets in an Excel file, and you're wondering if there's a way to convert those sheets into individual CSV files.
Let's address both of these scenarios separately:
Scenario 1: Saving data to separate CSV files
To save each column of `sigma` and `epsilon` as separate CSV files, you can modify your code as follows:
% Assuming sigma and epsilon are cell arrays with the data
% Assuming the number of columns is the same in both sigma and epsilon
numColumns = numel(sigma);
for k = 1:numColumns
thisfile = sprintf('output%d.csv', k);
dataToSave = [sigma{:,k}; epsilon{:,k}]; % Assuming the data is vertically aligned
csvwrite(thisfile, dataToSave);
end
In this code, we are using `csvwrite` to save each column's data into separate CSV files. The `sprintf` function is used to create unique file names for each column.
Scenario 2: Converting sheets in an Excel file to separate CSV files
If you already have the data saved to separate sheets in an Excel file and you want to convert those sheets into individual CSV files, you can use the following code:
% Assuming you have 'stressstrain.xlsx' file with multiple sheets, each containing one column of data
filename = 'stressstrain.xlsx';
xlsInfo = xlsinfo(filename);
for k = 1:numel(xlsInfo.SheetNames)
sheetName = xlsInfo.SheetNames{k};
data = readmatrix(filename, 'Sheet', sheetName);
thisfile = sprintf('%s.csv', sheetName);
% Save data to CSV
csvwrite(thisfile, data);
end
In this code, we use the `xlsinfo` function to get information about the sheets in the Excel file. Then, we loop through each sheet, read its data using `readmatrix`, and save the data to individual CSV files with the sheet names as the filenames.
Choose the one that fits your data format and requirements best. I hope this helps!
Thanks
  1 commentaire
Stephen Lesko
Stephen Lesko le 28 Juil 2023
Hi,
The first scenario works very well my only change was putting stress and strain into seperate columns. Thank you for the help!

Connectez-vous pour commenter.

Plus de réponses (1)

dpb
dpb le 26 Juil 2023
Modifié(e) : dpb le 26 Juil 2023
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 commentaire
Stephen Lesko
Stephen Lesko le 28 Juil 2023
you are correct in that there may be simpler and better ways to sort the data, but for my specific case I am running them into a function that takes .csv files, and rather than adjust the function I've been given I will match what is required to run the function. Thank you for the help.

Connectez-vous pour commenter.

Produits


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by