Writing array data to file
9 次查看(过去 30 天)
显示 更早的评论
Hello everyone,
I want to write this data to a file, but the data in matlab does not match with the data in the file. Whats the problem? Thanks in advance.
for z=1:columns
tension = tension_array{1,z};
epsilon = epsilon_array{1,z};
fileIDs = fopen(strcat(file_names{z,1},'_edited_strain.TXT'),'w','n','UTF-8');
fprintf(fileIDs,'%6s %12s\n','tension','strain');
fprintf(fileIDs,'%f64 %f64\n',tension,epsilon);
fclose(fileIDs);
end
采纳的回答
Stephen23
2018-9-27
编辑:Stephen23
2018-9-27
If tension and epsilon are non-scalar and are intended to be printed as columns then your code will not work. Text files are written by row (well, they are just written as a long vector of characters, with newlines interspersed), and all languages that have fprintf print to the file by row. It is not trivially possible to print one column, and then next column, and then a next column, etc. It is trivially possible to print one row, then the next row, etc., simply by appending to the end of that long character vector and adding newlines. This is not a limitation of MATLAB, it is simply due to how text files are stored.
Note that the fprintf help states that it "applies the formatSpec to all elements of arrays A1,... An in column order, and writes the data to a text file". So the solution is to put all of your data into one matrix, transpose it (so that the data is in column order), and then supply it to fprintf like this:
M = [tension_array{1,z}(:),epsilon_array{1,z}(:)]; % two columns
fprintf(fileIDs,'%g %g\n', M.'); % note the transpose.
It is not clear what you want %f64 to do: I could not find any reference to anything like this in the fprintf help (unless you really do mean the %f format specifier followed by the literal characters 64). You need to use a format specifier that is supported by MATLAB.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Debugging and Analysis 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!