Save data from cell array to data file

7 views (last 30 days)
Christia  Saggau
Christia Saggau on 3 Jul 2014
Edited: dpb on 4 Jul 2014
I want to reformat complex data in matlab to use it in a fortran program -> as it semmed easy i stored my data as strings in a cell array but i fail to export it as a text file. I had a look at the help about this but it didn't really help me.
my data has original the form of an 360x180 array with each element beeing a complex nr and i need for format it in Fortran style wach relement looking like (Re,Im)
I tried cell2table and export it as a table but but i got an error that cell2table cannot be used for input 'cell' The solution used is from the help but as said before it doesnt work but instead returns
Error using fprintf Function is not defined for 'cell' inputs.
Error in converttofortranstyle (line 22) fprintf(fileID,formatSpec,test{row,:});
load('field.mat')
Test=Field;
fopen('test.txt', 'w'); test = cell(360,180);
for n = 1:360 for m = 1:180 k = num2str(real(Test(n,m))); l = num2str(imag(Test(n,m))); test{n,m} = strcat('(',k,{' '},',',{' '},l,')');
end end
fileID = fopen('celldata.dat','w'); formatSpec = '%180s\n'; [nrows,ncols] = size(test); for row = 1:nrows fprintf(fileID,formatSpec,test{row,:}); end fclose(fileID);

Answers (1)

dpb
dpb on 3 Jul 2014
Edited: dpb on 4 Jul 2014
It appears your data are complex in the .mat file? If so, the simplest is to use them directly. Unfortunately, C (and hence Matlab since its i/o is based on C) doesn't have a good formatting option so you have to kinda fake it...recall that memory order in Matlab is column major, so...
load('field.mat'); % get the complex array
[nr,nc]=size(Field); % array row, column sizes
fmt=[repmat('(%.5f,%.5f) ',1,nc) '\n']; % build the format string/line
fid=fopen('output.dat','w'); % open a file for output
c=Field.'; % transpose to get in column order
c=[real(c(:)) imag(c(:))].'; % place real,imag adjacent in memory
c=reshape(c(:),2*nc,[]); % arrange by twos for number columns by row
fprintf(fid,fmt,c) % and print
fid=fclose(fid);
You can mash some of the intermediary rearrangement steps into fewer steps. It's easiest to see what the above does if you just create a small array of say 3x4 that can look at what happens on screen to see the reordering to get into sequential memory positions.
ADDENDUM
>> c=complex(rand(3,4),rand(3,4))
c =
0.7943 + 0.9133i 0.1656 + 0.5383i 0.6541 + 0.4427i 0.4505 + 0.0046i
0.3112 + 0.1524i 0.6020 + 0.9961i 0.6892 + 0.1067i 0.0838 + 0.7749i
0.5285 + 0.8258i 0.2630 + 0.0782i 0.7482 + 0.9619i 0.2290 + 0.8173i
>> t=c.'; % temporary; if don't need original can overwrite it...
>> t=[real(t(:)) imag(t(:))].'; % the intermixing of re,im
>> fprintf(fmt,reshape(t(:),2*nc,[])); % voila!!! :)
(0.794,0.913) (0.166,0.538) (0.654,0.443) (0.451,0.005)
(0.311,0.152) (0.602,0.996) (0.689,0.107) (0.084,0.775)
(0.529,0.826) (0.263,0.078) (0.748,0.962) (0.229,0.817)
>>
As noted, fixup format as desired; did this to be pretty to see w/ only the three decimal places....
ADDENDUM 2
Actually, realized this is where arrayfun can shine for ease in writing an expression...
>> fprintf(fmt,cell2mat(arrayfun(@(x) [real(x) imag(x)], c,'uniform',0)).')
(0.794,0.913) (0.166,0.538) (0.654,0.443) (0.451,0.005)
(0.311,0.152) (0.602,0.996) (0.689,0.107) (0.084,0.775)
(0.529,0.826) (0.263,0.078) (0.748,0.962) (0.229,0.817)
Just do the building of the [real imag] vector element-by-element in the anonymous function, convert the output to an array, transpose and print.

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!