I am trying to import data from Matlab 2016a/2016b-prerelease to Mathematica 11 with local storage
Data and Quality Assurance
Steps
Challenges with Matlab's v7.3
Challenges of automatic approaches
Matlab's v7 .mat deprecated against v7.3
Some specification: specify datafile as .mat file of v7.3
Attempt 1
Matlab
# Specify test data here time=0:0.001:1; potential=sin(time); C = spectrogram(potential); C=reshape(C,1,[]); C=nthroot( abs( C(1,1:1001) ), 1); hFig=figure(); hax=axes(hFig); imagesc(time,potential,C); filename=fullfile('/home/masi/Images/test'); filenameMat=fullfile('/home/masi/Images/test.mat'); export_fig(filename, '-tif', '-q101', '-a1', '-m1', '-RGB', '-nofontswap', '-nocrop', '-transparent', '-dpng', hax); save(filenameMat,'time', 'potential', 'C', '-v7.3');
Mathematica where #1-2 both succeed with import of many variables
(* http://mathgis.blogspot.fi/2010/09/tips-import-matlab-mat-files.html *) (* https://mathematica.stackexchange.com/a/10589/9815 *) (* #1 Succeeds; select specific data sets *) mma = Import["~/Images/test.mat", {"HDF5", "Datasets", "/time"}]; (* #2 Succeeds: Out {"/C", "/potential", "/time"} *) mma = Import["~/Images/test.mat", {"HDF5", "Datasets"}]; (* Output: {{1.}, {1.5}, {2.}} *)
Output: steps (1-4) succeeds but import of datafile (step 5) fails in Mathematica 11, see the error message above.
Reading the data in Mathematica where Flatten
is used to remove one set of braces because one set too much
(* https://stackoverflow.com/a/16834090/54964 *)
SetDirectory["Desktop"]
a = Import["m.mat"] ;
(* https://mathematica.stackexchange.com/a/97252/9815 *)
a=Partition[Flatten[a], 5000]
(* Output fails: {} *)
He is using simply a square matrix. I have the data in three variables: time
, potential
and C
, fitting imagesc()
's parameters.
Do square matrix of the vectors time mx 1 and potential nx 1. How can you apply the vector C in the square matrix A ? I do not understand the mathematics here sufficiently to answer the question myself.
# time's dimensions mx 1 # (potential')'s dimensions 1 xn time=0:0.001:1; potential=sin(time); A = time' * potential; # Output: A is mxn matrix, which is as as expected. # C is vector 1 xm here. C = spectrogram(potential); C=reshape(C,1,[]); C=nthroot( abs( C(1,1:1001) ), 1);
How can you convert the square matrix A(C) back to those three variables? - - A(C) is about the square matrix where the vector C has been applied on the square matrix A . I do not understand the mathematics behind it to create the result.
Matlab: 2016a, 2016b prerelease
Mathematica: 11
OS: Debian 8.5
Related: Is there a way to import the results or data from Matlab to Mathematica automatically?
an example of raw binary file exchange from matlab to mathematica:
matlab:
mat = [ pi 2*pi 3*pi ; 1 sqrt(2) sqrt(3) ]
f=fopen('out.bin','w')
fwrite(f,size(mat))
fwrite(f,mat,'double')
... # repeat for however many matrices we need to write
fwrite(f,size(mat2))
fwrite(f,mat2,'double')
...
fclose(f)
mathematica:
f = OpenRead["out.bin", BinaryFormat -> True];
size = BinaryReadList[f, "Integer8", 2];
mat = Transpose@ArrayReshape[
BinaryReadList[f, "Real64",Times@@size],
Reverse@size];
(* repeat as needed to read multiple matrices *)
Close[f];
MatrixForm@mat
note the Reverse
and Transpose
are needed because matlab writes the data in in column major order. You could alternately do fwrite(f,transpose(mat),'double')
when you write.
note also this assumes a square array. If you wanted to handle multidimensional arrays you'd also need to write length(size)
to the file and so on.
for completeness, go back like this:
f = OpenWrite["out.bin", BinaryFormat -> True];
BinaryWrite[f, Dimensions[mat], "Integer8"];
BinaryWrite[f, Transpose[mat], "Real64"];
Close[f]
..
f=fopen('out.bin','r')
sz=transpose(fread(f,2))
mat=fread(f,sz,'double')
fclose(f)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.