save a cell to multiple csv files in a for loop
13 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
Manan Jain
2023-7-26
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 个)
dpb
2023-7-26
编辑:dpb
2023-7-26
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Spreadsheets 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!