convert 47,000rows * 1000columns .mat file to .txt file
1 view (last 30 days)
Show older comments
Hi Friends,
I have totally 1000 variables, each has 47,000*1 double values. Now, I need to store all in a txt file.
Please help me to find a solution Thanks in advance
0 Comments
Accepted Answer
Image Analyst
on 31 Dec 2016
Use
fid = fopen(filename, 'wt')
% Write out variable 1
fprintf(fid, '%f, ', data1);
fprintf(fid, '\n');
% Write out variable 2
fprintf(fid, '%f, ', data2);
fprintf(fid, '\n');
% Write out variable 3
fprintf(fid, '%f, ', data3);
fprintf(fid, '\n');
.
.
.
% Write out variable 1000
fprintf(fid, '%f, ', data1000);
fprintf(fid, '\n');
fclose(fid);
You really should learn how to use arrays and not have 1000 variables. I don't think I've ever written a program in my entire life that had 1000 separate variables.
3 Comments
Image Analyst
on 1 Jan 2017
Well somehow you already have typed them all because you're using them in your code, aren't you? If it's too cumbersome to do that here while saving them, then why is it not to cumbersome to use them all in your code. I mean, let's say your variables area called var1, var2, var3, ...var1000. Is there someplace in the code where you access each one, like (for example)
var438 = 10 * var219 - var934 + log(var791);
And then you do that again for all 999 other variables? Everyone will tell you this is not a good way to program.
To not have to type them all again you'd have to use dynamic field names, as Walter showed you. But it's best to just avoid that in the first place. Don't use 1000 separate variables. Use one array instead. Then you just have one variable to worry about. What are your variable names and why do you think you need 1000 of them.
Walter Roberson
on 1 Jan 2017
Note that this does not write the data out as columns as was requested in the original question.
More Answers (1)
Walter Roberson
on 31 Dec 2016
Install https://www.mathworks.com/matlabcentral/fileexchange/10959-sort-nat--natural-order-sort . Then
data_struct = load('TheFileName.mat');
fn = fieldnames(data_struct);
sorted_fn = sort_nat(fn);
num_var = length(sorted_fn);
one_var = data_struct.(sorted_fn{1});
size_of_var = length( one_var );
all_data = zeros(size_of_var, num_var, class(one_var));
for K = 1 : num_var
all_data(:,K) = data_struct.(sorted_fn{K});
end
Now all of the data has been stored in all_data. But you have not been specific about the output format. Some of the possible outputs include
save OutputFileName.txt all_data -ASCII %space, low precision
or
save OutputFileName.txt all_data -ASCII -DOUBLE %space, higher precision
or
csvwrite('OutputFileName.csv', all_data) %commas
or
dlmwrite('OutputFileName.txt', all_data, 'delimiter', '\t') %tabs
or
xlswrite('OutputFileName.csv', all_data) %commas
9 Comments
Walter Roberson
on 20 Jan 2017
data_struct = load('TheFileName.mat');
fn = fieldnames(data_struct);
sorted_fn = sort_nat(fn);
num_var = length(sorted_fn);
one_var = data_struct.(sorted_fn{1});
size_of_var = length( one_var );
all_data = zeros(size_of_var, num_var, class(one_var));
col_names = cell(1, num_var);
used_cols = 0;
for K = 1 : num_var
this_data = data_struct.(sorted_fn{K});
if isnumeric(this_data)
used_cols = used_cols + 1;
all_data(:,used_cols) = this_data;
col_names{used_cols} = sorted_fn{K};
end
end
all_data(:,used_cols+1:end) = []; %trim out unused
col_names(used+cols+1:end) = []; %trim out unused
item_hdr_fmt = '%-23s'
item_num_fmt = '%+23.16e';
hdr_fmt = [repmat([item_hdr_fmt ' '], 1, used_cols-1), item_hdr_fmt '\n'];
item_fmt = [repmat([item_num_fmt ' '], 1, used_cols-1), item_num_fmt '\n'];
fid = fopen('TheOutputFile.txt', 'wt');
fprintf(fid, hdr_fmt, col_names{:});
fprintf(fid, item_fmt, all_data .'); %the transpose is important
fclose(fid);
Sara
on 23 Jan 2017
Thank you so very much Walter!!! :O) :OD
So Awesome!!!
Tiny typo on the line below: the+ should be an _ ;O)
col_names(used+cols+1:end) = []; %trim out unused
col_names(used_cols+1:end) = []; %trim out unused
Thank you!!!
Sara
See Also
Categories
Find more on Data Type Conversion in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!