how to write the results from the code into the csv file format?

Hi,
Code below is producing the result including dates format and I usually use dlmwrite to write the results in csv format but in this case I am getting an error below: (What should I change so I can save the results)
Error:
Error using sprintf
Unable to convert 'duration' value to 'double'.
Error in dlmwrite (line 161)
str = sprintf(format,m(i,:));
Error in ploting_timeoverUy (line 30)
dlmwrite('Uy_timestamps.csv',[dates,all_Uy])
Code:
close all; clear all; clc;
projectdir = 'F:\3-PIV_Experimental_Data\Outlet_110\Data_LaserSheet_D\Data_PIV\5-AverageFilter_VelocitiesExtration\Point_Velocities\Uy';
files = dir( fullfile(projectdir, '*', '*.csv') );
filenames = fullfile({files.folder}, {files.name});
num_files = length(filenames);
all_Uy = zeros(num_files,1);
all_time = zeros(num_files,1);
for i = 1:numel(files)
filename = filenames{i};
data = readmatrix(filename);
all_Uy(i) = data(1,2);
end
Location = 'F:\3-PIV_Experimental_Data\Outlet_110\Data_LaserSheet_D\Data_PIV\5-AverageFilter_VelocitiesExtration\Point_Velocities\Uy';
files = dir(Location);
dirFlags = [files.isdir];
subFolders = files(dirFlags);
subFolderNames = {subFolders(3:end).name}
for k = 1 : length(subFolderNames)
fprintf('Sub folder #%d = %s\n', k, subFolderNames{k});
end
dates = extractBetween(subFolderNames,"Run ",".");
dates = replace(dates,'-',':');
dates = duration(dates,"InputFormat","hh:mm:ss")
dates=dates'
plot(dates,all_Uy, '-*');
dlmwrite('Uy_timestamps.csv',[dates,all_Uy])

 채택된 답변

Cris LaPierre
Cris LaPierre 2022년 3월 10일

1 개 추천

I'd try using writematrix instead.

댓글 수: 6

hi chris I tried using writematrix([dates,all_Uy],'M.csv'), but it did not save the data properly please see the file M.csv attached intead data should be save as shown in the file TimeStamps_Uy.xlsx( I copy and pasted the data from the work space in this file)...... I am searching how to save it in a write format but no success so far!
Ah, you have two different data types. You'll have to use writetable instead.
Hi I have tried that as well:
writetable([dates,all_Uy],'myData.csv')
getting this error:
Error using writetable (line 183)
Unsupported type 'duration'. Use writematrix instead.
Error in ploting_timeoverUy (line 32)
writetable([dates,all_Uy],'myData.csv')
The error is because you are trying to create a matrix by concatenating a duration with a double.
[dates,all_Uy]
All values in a matrix must be of the same data type. You will need to first create a table. Try this (untested).
myT = table(dates,all_Uy,'VariableNames',["TimeStamp","Uy"])
writetable(myT,'myData.csv')
cheers, that worked and thanks for the explanation. I will remember that " all the matrix values has to be in the same datatype"
That's for dlmwrite and csvwrite(). For tables, each column can be different, or for max flexbility, you can use fprintf() like I showed below in my answer.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Image Analyst
Image Analyst 2022년 3월 10일
You could do it manually with fprintf():
fid = fopen(fileName, 'wt');
if fid == -1
warningMessage = sprintf('Error opening file for output:\n%s', fileName);
uiwait(errordlg(warningMessage));
return;
end
for k = 1 : length(all_Uy)
fprintf(fid, '%s, %f\n', dates(k), all_Uy(k));
end
fclose(fid)
I'm not sure about the dates format. You may have to make sure the dates value matches the format specifier. If it's a string, %s will work.

카테고리

도움말 센터 및 File Exchange에서 Environment and Settings에 대해 자세히 알아보기

질문:

2022년 3월 10일

댓글:

2022년 3월 10일

Community Treasure Hunt

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

Start Hunting!

Translated by