How to write an array containing letters and number to file?

11 次查看(过去 30 天)
I'm very new to this so I apologize if my question seems trivial
I am trying to write a code that does the following: Imports data from a tab-deliminated dat file (got that part) Does various calculations(still ok) Then constructs a table with the results, and both prints the table to the screen and writes it to a file. The data is set up so each column is a separate data set, so I'd like to set it up so a file with any number of rows or columns can be used (e.g. with loops, but I think I can figure that part out)
I need to create a table/array/matrix, with string labels on each column and row. I've tried everything I can, but I can't seem to find any kind of array that will accept both characters and number inputs, and will let me print the table to a screen AND write them to a file. I can't use table() because my version is only R2013a. Any guidance would be greatly appreciated
  1 个评论
Tom W
Tom W 2014-1-17
I usually do a disp or fprints before a file write ( fprintf ) so I can get the output in the command window as well.

请先登录,再进行评论。

回答(2 个)

Bruno Pop-Stefanov
Bruno Pop-Stefanov 2014-1-17
Cell arrays can contain elements of different types and sizes. They work similarly to regular arrays except that they use curvy brackets {} instead of [].
Here is a simple example of how you could store data in a cell array and then print it to a text file:
myCellArray = {'2014-01-17 10:49AM', 34, 57, 74; ...
'2014-01-16 11:53AM', 12, 90, 57; ...
'2014-01-15 9:12PM' , 9, 78, 43};
fid = fopen('data_out.txt','w');
fprintf(fid, 'Date: \t\t\t\t Numbers:\n');
for i=1:size(myCellArray,1)
fprintf(fid, '%s \t %s\n', myCellArray{i,1}, sprintf('%d ', myCellArray{i,2:end}));
end
fclose(fid);

Arry Rahmadi
Arry Rahmadi 2014-1-17
Maybe you can give preview of the problem? In general, cell array can take different types of data (such as strings and integers). You can create it by using {} instead of []. But the data class will be cell instead of char/double.

类别

Help CenterFile Exchange 中查找有关 Cell Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by